From b873d848a138ac9b9888b9ddbd4c557fd8a5ad9e Mon Sep 17 00:00:00 2001 From: William Perron Date: Sat, 18 Mar 2023 17:34:30 -0400 Subject: [PATCH] make otel generator actually generate traces --- observability/otelgen/main.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/observability/otelgen/main.go b/observability/otelgen/main.go index 7b894ef..0fa6866 100644 --- a/observability/otelgen/main.go +++ b/observability/otelgen/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os/signal" "syscall" + "time" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" @@ -13,19 +14,40 @@ import ( sdktrace "go.opentelemetry.io/otel/sdk/trace" ) +var ( + tracer = otel.Tracer("otelgen") +) + func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() res := makeResource(ctx) - shutdown := initOpenTelemetry(ctx, res, "http://localhost:4317") + shutdown := initOpenTelemetry(ctx, res, "localhost:4317") + + ticker := time.NewTicker(10 * time.Second) + +loop: + for { + select { + case <-ctx.Done(): + shutdown(context.TODO()) + break loop + case <-ticker.C: + genSpan(ctx) + } + } +} + +func genSpan(ctx context.Context) { + ctx, span := tracer.Start(ctx, "generate") + defer span.End() - <-ctx.Done() - shutdown(context.TODO()) + span.AddEvent("example span event") } func initOpenTelemetry(ctx context.Context, res *resource.Resource, endpoint string) func(ctx context.Context) error { - exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithEndpoint(endpoint)) + exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithEndpoint(endpoint), otlptracegrpc.WithInsecure()) if err != nil { panic(fmt.Errorf("failed to create OTLP exporter: %w", err)) }