splunkhttp

package module
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

README

Splunk specific instrumentation for net/http

Example

Simplified example:

package main

import (
	"net/http"

	"github.com/signalfx/splunk-otel-go/distro"
	"github.com/signalfx/splunk-otel-go/instrumentation/net/http/splunkhttp"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func main() {
	distro.Run()

	var handler http.Handler = http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("Hello"))
		}
	)
	handler = splunkhttp.NewHandler(handler)
	handler = otelhttp.NewHandler(handler, "my-service")

	http.ListenAndServe(":9090", handler)
}

Configuration

Splunk distribution configuration
Environment variable Default value Purpose
SPLUNK_TRACE_RESPONSE_HEADER_ENABLED true Adds Server-Timing header to HTTP responses. More

Features

Trace linkage between the APM and RUM products

NewHandler wraps the passed handler, functioning like middleware. It adds trace context in traceparent form as Server-Timing header to the HTTP response:

Access-Control-Expose-Headers: Server-Timing
Server-Timing: traceparent;desc="00-<serverTraceId>-<serverSpanId>-01"

This information can be later consumed by the splunk-otel-js-web library.

Documentation

Overview

Package splunkhttp provides functions that add additional Splunk specific instrumentation by wrapping existing handlers.

Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
	"go.opentelemetry.io/otel/sdk/trace"

	"github.com/signalfx/splunk-otel-go/instrumentation/net/http/splunkhttp"
)

func main() {
	var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		io.WriteString(w, "Hello world")
	})
	handler = splunkhttp.NewHandler(handler)
	handler = otelhttp.NewHandler(handler, "server", otelhttp.WithTracerProvider(trace.NewTracerProvider()))

	ts := httptest.NewServer(handler)
	defer ts.Close()
	resp, err := ts.Client().Get(ts.URL)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()

	fmt.Println("Access-Control-Expose-Headers:", resp.Header.Get("Access-Control-Expose-Headers"))
	fmt.Println("Server-Timing:", resp.Header.Get("Server-Timing"))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(handler http.Handler) http.Handler

NewHandler wraps the passed handler in a span named after the operation and with any provided Options. This will also enable all the Splunk specific defaults for HTTP tracing.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL