Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/udhos/otelconfig
otelconfig provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.
https://github.com/udhos/otelconfig
go golang jaeger jaeger-go opentelemetry opentelemetry-go tracing
Last synced: 2 days ago
JSON representation
otelconfig provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.
- Host: GitHub
- URL: https://github.com/udhos/otelconfig
- Owner: udhos
- License: mit
- Created: 2023-10-24T23:21:35.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-24T01:31:20.000Z (27 days ago)
- Last Synced: 2024-10-24T17:09:50.786Z (26 days ago)
- Topics: go, golang, jaeger, jaeger-go, opentelemetry, opentelemetry-go, tracing
- Language: Go
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/udhos/otelconfig/blob/main/LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/udhos/otelconfig)](https://goreportcard.com/report/github.com/udhos/otelconfig)
[![Go Reference](https://pkg.go.dev/badge/github.com/udhos/otelconfig.svg)](https://pkg.go.dev/github.com/udhos/otelconfig)# otelconfig
[otelconfig](https://github.com/udhos/otelconfig) provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.
# Example
See [examples/oteltrace-example/main.go](examples/oteltrace-example/main.go).
# Usage
```go
import "github.com/udhos/otelconfig/oteltrace"
import "go.opentelemetry.io/otel/trace"// NOOP env var disables tracing
noopEnv := os.Getenv("NOOP")
noop := noopEnv != ""
log.Printf("NOOP=%s noop=%t", noopEnv, noop)//
// initialize tracing
//var tracer trace.Tracer
if noop {
tracer = oteltrace.NewNoopTracer()
} else {
options := oteltrace.TraceOptions{
DefaultService: "my-program",
NoopTracerProvider: false,
Debug: true,
}tr, cancel, errTracer := oteltrace.TraceStart(options)
if errTracer != nil {
log.Fatalf("tracer: %v", errTracer)
}defer cancel()
tracer = tr
}// use tracer to create spans
work(context.TODO(), tracer)
// ...
func work(ctx context.Context, tracer trace.Tracer) {
_, span := tracer.Start(ctx, "work")
defer span.End()
}```
# Configuration
General configuration: https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/
Exporter configuration: https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/
```bash
export OTELCONFIG_EXPORTER=jaeger|grpc|http|stdout ;# Protocol default: grpc
export OTEL_TRACES_EXPORTER=jaeger|otlp ;# Data Format default: otlp
export OTEL_PROPAGATORS=b3multi ;# [1] Propagator default: tracecontext,baggage
export OTEL_EXPORTER_OTLP_ENDPOINT=http://host:port ;# Endpoint default: [2]# [1] Propagators: tracecontext,baggage,b3,b3multi,jaeger,xray,ottrace,none
#
# [2] Default endpoint: http://localhost:4317 for grpc
# http://localhost:4318 for http
#
# Service name precedence from higher to lower:
# 1. OTEL_SERVICE_NAME=mysrv
# 2. OTEL_RESOURCE_ATTRIBUTES=service.name=mysrv
# 3. TraceOptions.DefaultService="mysrv"
```Examples:
```bash
# Example for Jaeger
export OTELCONFIG_EXPORTER=jaeger
export OTEL_TRACES_EXPORTER=jaeger
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:14268
oteltrace-example# Example for gRPC and OTLP
export OTELCONFIG_EXPORTER=grpc
export OTEL_TRACES_EXPORTER=otlp
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:4317
oteltrace-example# Example for HTTP and OTLP
export OTELCONFIG_EXPORTER=http
export OTEL_TRACES_EXPORTER=otlp
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:4318
oteltrace-example
```