ott

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 18 Imported by: 0

README

OTT: Opentelemetry Tracing Utilities

PkgGoDev

Golang provider library that provides tracers for auto instrumentation of tracing providers based on services using http handlers/routers.

Providers

Providers are distributed tracing backends that provides and interface that is compatible with OpenTelemetry specification. Currently this tracer support the following providers

  • Zipkin
  • Jaeger
  • noop
  • stdout

Usage

Using the library is simple and straightforward.

  1. Import the library on your router module/package.
import tl "github.com/mainak90/ott"
  1. Initialize and empty Config struct.
cfg := tl.Config{AppName: "Test-App",Provider: "stdout"}
  1. Use the config struct to initialize the tracer utility.
trace, err := tl.InitTracing(cfg, "Test-App")
if err != nil {
	fmt.Errorf(err)
}
  1. Get the router options
opts := tl.GetMuxOptions(trace)
  1. Get the http transport config
tr := tl.NewTransport(trace)
  1. Update your mux router with the updated config. In application
r := mux.NewRouter()
client := &http.Client{Transport: tr}
r.Use(otelmux.Middleware("Test-App", opts...), tl.EchoFirstTraceNodeInfo(trace.Propagator))
r.HandleFunc("/", requestHandlerFunc)
  1. EchoFirstTraceInfo injects trace info into the response path as long as the request is part of the parent span(first one in the request chain).

  2. While running the app, the library can be tested by using checking the traceID and spanID of each request.

traceID, spanID, _ := tl.ExtractTraceInfo(r.Context())
  1. If the trace_ids and span_ids is not printed out from the helper function like below.
log.Println(fmt.Sprintf("Trace ID for this request in %s is: %s and Span Id is: %s", "Test-App", traceID, spanID))
  1. Throws generic output like
2023/04/23 21:48:49 Trace ID for this request in ServiceA is: 9ede35c46c2dc8297e5d3f15283c59c8 and Span Id is: 33837d83b34cfabb
  1. This can also output the full span context in the response(if its stdout provider). A typical span context would look like this.
{
        "Name": "HTTP POST",
        "SpanContext": {
                "TraceID": "9ede35c46c2dc8297e5d3f15283c59c8",
                "SpanID": "d28674755081cfd9",
                "TraceFlags": "01",
                "TraceState": "",
                "Remote": false
        },
        "Parent": {
                "TraceID": "9ede35c46c2dc8297e5d3f15283c59c8",
                "SpanID": "33837d83b34cfabb",
                "TraceFlags": "01",
                "TraceState": "",
                "Remote": false
        },
        "SpanKind": 3,
        "StartTime": "2023-04-23T21:48:49.825656+02:00",
        "EndTime": "2023-04-23T21:48:49.827174434+02:00",
        "Attributes": [
                {
                        "Key": "http.method",
                        "Value": {
                                "Type": "STRING",
                                "Value": "POST"
                        }
                },
                {
                        "Key": "http.flavor",
                        "Value": {
                                "Type": "STRING",
                                "Value": "1.1"
                        }
                },
                {
                        "Key": "http.url",
                        "Value": {
                                "Type": "STRING",
                                "Value": "http://localhost:8001"
                        }
                },
                {
                        "Key": "net.peer.name",
                        "Value": {
                                "Type": "STRING",
                                "Value": "localhost"
                        }
                },
                {
                        "Key": "net.peer.port",
                        "Value": {
                                "Type": "INT64",
                                "Value": 8001
                        }
                },
                {
                        "Key": "http.status_code",
                        "Value": {
                                "Type": "INT64",
                                "Value": 200
                        }
                }
        ],
        "Events": null,
        "Links": null,
        "Status": {
                "Code": "Unset",
                "Description": ""
        },
        "DroppedAttributes": 0,
        "DroppedEvents": 0,
        "DroppedLinks": 0,
        "ChildSpanCount": 0,
        "Resource": [
                {
                        "Key": "service.name",
                        "Value": {
                                "Type": "STRING",
                                "Value": "unknown_service:___go_build_github_com_mainak90_ott_test_serviceA"
                        }
                },
                {
                        "Key": "telemetry.sdk.language",
                        "Value": {
                                "Type": "STRING",
                                "Value": "go"
                        }
                },
                {
                        "Key": "telemetry.sdk.name",
                        "Value": {
                                "Type": "STRING",
                                "Value": "opentelemetry"
                        }
                },
                {
                        "Key": "telemetry.sdk.version",
                        "Value": {
                                "Type": "STRING",
                                "Value": "1.14.0"
                        }
                }
        ],
        "InstrumentationLibrary": {
                "Name": "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp",
                "Version": "semver:0.40.0",
                "SchemaURL": ""
        }
}

License

Uses the MIT license. Please check out LICENSE.md for more details.

Documentation

Index

Constants

View Source
const (
	SpanIDLogKeyName  = "span-id"
	TraceIdLogKeyName = "trace-id"
)
View Source
const DefaultTracerProvider = "noop"

Variables

View Source
var (
	ErrTracerProviderNotFound    = errors.New("TracerProvider could not be found")
	ErrTracerProviderBuildFailed = errors.New("Failed building TracerProvider")
)

Functions

func ConfigureTracerProvider

func ConfigureTracerProvider(config Config) (trace.TracerProvider, error)

func EchoFirstTraceNodeInfo

func EchoFirstTraceNodeInfo(propagator propagation.TextMapPropagator) func(http.Handler) http.Handler

EchoFirstNodeTraceInfo captures the trace information from a request and writes it back in the response headers if the request is the first one in the trace path.

func ExtractTraceInfo

func ExtractTraceInfo(ctx context.Context) (string, string, bool)

ExtractTraceInfo returns the ID of the trace flowing through the context as well as ID the current active span. The third boolean return value represents whether the returned IDs are valid and safe to use. OpenTelemetry's noop tracer provider, for instance, returns zero value trace information that's considered invalid and should be ignored.

func GetMuxOptions

func GetMuxOptions(tr Tracing) []otelmux.Option

func NewTransport

func NewTransport(tr Tracing) *otelhttp.Transport

Types

type Config

type Config struct {
	AppName    string                         `json:"appName"`
	Provider   string                         `json:"provider"`
	Endpoint   string                         `json:"endpoint"`
	SkipExport bool                           `json:"skipExport"`
	Providers  map[string]ProviderConstructor `json"-"`
}

type ProviderConstructor

type ProviderConstructor func(config Config) (trace.TracerProvider, error)

ProviderConstructor is useful when client wants to add their own custom TracerProvider.

type TraceConfig

type TraceConfig struct {
	TraceProvider trace.TracerProvider
}

type Tracing

type Tracing struct {
	// TracerProvider helps create trace spans.
	TracerProvider trace.TracerProvider
	// Propagator helps propagate trace context across API boundaries as a cross-cutting concern.
	Propagator propagation.TextMapPropagator
}

func InitTracing

func InitTracing(c Config, appName string) (Tracing, error)

Jump to

Keyboard shortcuts

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