Docs
Install
Golang

Golang

HyperDX uses the OpenTelemetry standard for collecting telemetry data (logs and traces). Traces are auto-generated with automatic instrumentation, so manual instrumentation isn't required to get value out of tracing.

This Guide Integrates:

✅ Logs✅ Metrics✅ Traces

Getting Started

Install OpenTelemetry Instrumentation Packages

To install the OpenTelemetry and Hyperdx Go packages, use the command below. It is recommended to check out the current instrumentation packages (opens in a new tab) and install the necessary packages to ensure that the trace information is attached correctly.

For this example, we will be using net/http/otelhttp.

go get -u go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
go get -u github.com/hyperdxio/hyperdx-go

Install Hyperdx Zapz Logging Module

To effectively correlate traces with logs in your Go application, consider replacing your existing logger with Hyperdx Zaps module, which is fully compatible with Zap (opens in a new tab), a popular logging framework for Go. To get started, you can install the module by running the following commands in your terminal:

go get -u github.com/hyperdxio/zapz
go get -u go.uber.org/zap

Native HTTP Server Example (net/http)

Refer to the commented sections to learn how to instrument your Go application.

package main
 
import (
	"io"
	"net/http"
	"os"
	"strings"
 
	"github.com/hyperdxio/hyperdx-go"
	"github.com/hyperdxio/zapz"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
	"go.uber.org/zap"
)
 
func main() {
	// Initialize otel tracing for server
	hyperdx.InitTracer()
 
	// Initialize zapz logger and use it across the entire app
	logger, err := zapz.New(strings.Split(os.Getenv("OTEL_EXPORTER_OTLP_HEADERS"), "=")[1])
	if err != nil {
		panic(err)
	}
	defer logger.Sync()
 
	logger.Warn("hello world", zap.String("foo", "bar"))
 
	http.Handle("/", otelhttp.NewHandler(wrapHandler(logger, ExampleHandler), "example-service"))
 
	port := os.Getenv("PORT")
	if port == "" {
		port = "7777"
	}
 
	logger.Info("** Service Started on Port " + port + " **")
	if err := http.ListenAndServe(":"+port, nil); err != nil {
		logger.Fatal(err.Error())
	}
}
 
// Wrap all handlers using this function to include trace metadata in the logger
func wrapHandler(logger *zap.Logger, handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logger := zapz.WithTraceMetadata(r.Context(), logger)
		logger.Info("request received", zap.String("path", r.URL.Path), zap.String("method", r.Method))
		handler(w, r)
		logger.Info("request completed", zap.String("path", r.URL.Path), zap.String("method", r.Method))
	}
}
 
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "application/json")
	io.WriteString(w, `{"status":"ok"}`)
}

Configure Environment Variables

Afterwards you'll need to configure the following environment variables in your shell to ship telemetry to HyperDX:

export OTEL_EXPORTER_OTLP_ENDPOINT=https://in-otel.hyperdx.io \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_SERVICE_NAME='<NAME_OF_YOUR_APP_OR_SERVICE>' \
OTEL_EXPORTER_OTLP_HEADERS='authorization=<YOUR_HYPERDX_API_KEY_HERE>'

The OTEL_SERVICE_NAME environment variable is used to identify your service in the HyperDX app, it can be any name you want.

The OTEL_EXPORTER_OTLP_HEADERS environment variable is used to link your telemetry to your HyperDX account, and the API Key can be grabbed from the team page (opens in a new tab)