Created
September 29, 2024 22:20
-
-
Save wolfeidau/38a8d08c8d0373822946ce483b81f6f1 to your computer and use it in GitHub Desktop.
honeycomb tracing simplified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package trace | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"go.opentelemetry.io/otel" | |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | |
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" | |
"go.opentelemetry.io/otel/propagation" | |
"go.opentelemetry.io/otel/sdk/resource" | |
sdktrace "go.opentelemetry.io/otel/sdk/trace" | |
"go.opentelemetry.io/otel/sdk/trace/tracetest" | |
semconv "go.opentelemetry.io/otel/semconv/v1.26.0" | |
"go.opentelemetry.io/otel/trace" | |
) | |
var globalProvider *Provider | |
var tracer trace.Tracer | |
type Provider struct { | |
tp *sdktrace.TracerProvider | |
} | |
func NewProvider(ctx context.Context, name, version string) (*Provider, error) { | |
exp, err := newExporter(ctx) | |
if err != nil { | |
return nil, fmt.Errorf("failed to create exporter: %w", err) | |
} | |
res, err := newResource(ctx, name, version) | |
if err != nil { | |
return nil, fmt.Errorf("failed to create resource: %w", err) | |
} | |
tp := sdktrace.NewTracerProvider( | |
sdktrace.WithBatcher(exp), | |
sdktrace.WithResource(res), | |
) | |
otel.SetTracerProvider(tp) | |
otel.SetTextMapPropagator( | |
propagation.NewCompositeTextMapPropagator( | |
propagation.TraceContext{}, | |
propagation.Baggage{}, | |
), | |
) | |
tracer = tp.Tracer(name) | |
globalProvider = &Provider{tp: tp} | |
return globalProvider, nil | |
} | |
func (p *Provider) Tracer(name string) trace.Tracer { | |
return p.tp.Tracer(name) | |
} | |
func (p *Provider) Shutdown(ctx context.Context) error { | |
return p.tp.Shutdown(ctx) | |
} | |
func Start(ctx context.Context, name string) (context.Context, trace.Span) { | |
return tracer.Start(ctx, name) | |
} | |
func newExporter(ctx context.Context) (sdktrace.SpanExporter, error) { | |
exporter := os.Getenv("TRACE_EXPORTER") | |
switch exporter { | |
case "grpc": | |
clientOTel := otlptracegrpc.NewClient() | |
return otlptrace.New(ctx, clientOTel) | |
case "stdout": | |
return stdouttrace.New() | |
default: | |
return tracetest.NewNoopExporter(), nil | |
} | |
} | |
func newResource(cxt context.Context, name, version string) (*resource.Resource, error) { | |
options := []resource.Option{ | |
resource.WithSchemaURL(semconv.SchemaURL), | |
} | |
options = append(options, resource.WithHost()) | |
options = append(options, resource.WithFromEnv()) | |
options = append(options, resource.WithAttributes( | |
semconv.TelemetrySDKNameKey.String("otelconfig"), | |
semconv.TelemetrySDKLanguageGo, | |
semconv.TelemetrySDKVersionKey.String(version), | |
)) | |
return resource.New( | |
cxt, | |
options..., | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment