You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.4 KiB
61 lines
1.4 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
|
"go.opentelemetry.io/otel/propagation"
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
)
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
res := makeResource(ctx)
|
|
shutdown := initOpenTelemetry(ctx, res, "http://localhost:4317")
|
|
|
|
<-ctx.Done()
|
|
shutdown(context.TODO())
|
|
}
|
|
|
|
func initOpenTelemetry(ctx context.Context, res *resource.Resource, endpoint string) func(ctx context.Context) error {
|
|
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithEndpoint(endpoint))
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to create OTLP exporter: %w", err))
|
|
}
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(exporter)
|
|
tp := sdktrace.NewTracerProvider(
|
|
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.AlwaysSample())),
|
|
sdktrace.WithResource(res),
|
|
sdktrace.WithSpanProcessor(bsp),
|
|
)
|
|
|
|
otel.SetTracerProvider(tp)
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
|
|
|
return tp.Shutdown
|
|
}
|
|
|
|
func makeResource(ctx context.Context) *resource.Resource {
|
|
res, err := resource.New(ctx,
|
|
resource.WithFromEnv(),
|
|
resource.WithHost(),
|
|
resource.WithOS(),
|
|
resource.WithProcess(),
|
|
resource.WithTelemetrySDK(),
|
|
)
|
|
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to create resource: %w", err))
|
|
}
|
|
|
|
return res
|
|
}
|