ELK Stack · Docs
Sending data

Go

Send an authenticated event using the template example.

Prepare the environment

Set LOGSTASH_URL and LOGSTASH_PASSWORD as described in Sending data. Run inside the same Railway environment as Logstash.

Example

Source: examples/go/main.go in the template repository. The code below is identical to that file.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"time"
)

func main() {
	if err := send(); err != nil {
		fmt.Fprintln(os.Stderr, "Send failed. Check variables, connectivity and intake credentials.")
		os.Exit(1)
	}
	fmt.Println("Event accepted. Confirm indexing in Kibana.")
}

func send() error {
	url, password := os.Getenv("LOGSTASH_URL"), os.Getenv("LOGSTASH_PASSWORD")
	if url == "" || password == "" {
		return fmt.Errorf("missing variables")
	}
	body, err := json.Marshal(map[string]interface{}{
		"message": "hello from Go", "service": "example-go", "level": "info",
		"attributes": map[string]string{"language": "go"},
	})
	if err != nil {
		return err
	}
	request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
	if err != nil {
		return err
	}
	request.SetBasicAuth("shipper", password)
	request.Header.Set("Content-Type", "application/json")
	client := &http.Client{Timeout: 10 * time.Second,
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}
	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	if response.StatusCode < 200 || response.StatusCode >= 300 {
		return fmt.Errorf("intake rejected event")
	}
	return nil
}

Run and verify

From the template repository:

go run examples/go/main.go

Search service : "example-go" in Kibana's elk-logs data view. Widen the time range if needed. The program's timeout is configured in the source above. A successful exit confirms acceptance; finding the event confirms indexing.

The template CI uses Node 22, Python 3.12, and Go 1.23. These are selected test runtimes, not minimum supported versions. The Node example requires built-in fetch and AbortSignal.timeout.

Failure behavior

Missing variables, rejected credentials, redirects, and connection errors cause failure. There is no automatic retry. A retry after a timeout may duplicate an event. Do not log the password when debugging.

On this page