splunkhttp

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 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
Code Environment variable Default value Purpose
WithTraceResponseHeader, TraceResponseHeaderMiddleware SPLUNK_TRACE_RESPONSE_HEADER_ENABLED true Adds Server-Timing header to HTTP responses. More

Features

Trace linkage between the APM and RUM products

TraceResponseHeaderMiddleware 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, r *http.Request) {
		io.WriteString(w, "Hello world") //nolint:errcheck
	})
	handler = splunkhttp.NewHandler(handler)
	handler = otelhttp.NewHandler(handler, "server", otelhttp.WithTracerProvider(trace.NewTracerProvider()))

	ts := httptest.NewServer(handler)
	defer ts.Close()
	resp, _ := ts.Client().Get(ts.URL) //nolint

	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, opts ...Option) 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.

func TraceResponseHeaderMiddleware

func TraceResponseHeaderMiddleware(handler http.Handler) http.Handler

TraceResponseHeaderMiddleware wraps the passed handler, functioning like middleware. It adds trace context in traceparent form (https://www.w3.org/TR/trace-context/#traceparent-header) as Server-Timing header (https://www.w3.org/TR/server-timing/) to the HTTP response.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is used for setting optional config properties.

func WithTraceResponseHeader

func WithTraceResponseHeader(v bool) Option

WithTraceResponseHeader enables or disables the TraceResponseHeaderMiddleware.

The default is to enable the TraceResponseHeaderMiddleware if this option is not passed. Additionally, the SPLUNK_TRACE_RESPONSE_HEADER_ENABLED environment variable can be set to TRUE or FALSE to specify this option. This option value will be given precedence if both it and the environment variable are set.

Jump to

Keyboard shortcuts

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