make otel generator actually generate traces

otelq v0.1.0-rc0
William Perron 2 years ago
parent cbb733693f
commit b873d848a1
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
@ -13,19 +14,40 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
) )
var (
tracer = otel.Tracer("otelgen")
)
func main() { func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop() defer stop()
res := makeResource(ctx) 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() span.AddEvent("example span event")
shutdown(context.TODO())
} }
func initOpenTelemetry(ctx context.Context, res *resource.Resource, endpoint string) func(ctx context.Context) error { 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 { if err != nil {
panic(fmt.Errorf("failed to create OTLP exporter: %w", err)) panic(fmt.Errorf("failed to create OTLP exporter: %w", err))
} }

Loading…
Cancel
Save