@@ -142,84 +121,175 @@ NewTracer creates a new Tracer with Option\(s\). ```go -package main - -import ( - "context" - "os" - "os/signal" - "syscall" +tp := trace.NewTracerProvider() +defer tp.Shutdown(context.Background()) - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/sdk/trace" - - courier "github.com/gojek/courier-go" - "github.com/gojek/courier-go/otelcourier" +exporter, err := prometheus.New( +/* Add a non-default prometheus registry here with `prometheus.WithRegisterer` option, if needed. */ ) +if err != nil { + panic(err) +} +mp := metric.NewMeterProvider(metric.WithReader(exporter)) -func main() { - tp := trace.NewTracerProvider() - defer tp.Shutdown(context.Background()) +otel.SetTracerProvider(tp) +otel.SetMeterProvider(mp) +otel.SetTextMapPropagator(&propagation.TraceContext{}) - otel.SetTracerProvider(tp) +metricLabelMapper := otelcourier.TopicAttributeTransformer(func(ctx context.Context, topic string) string { + if strings.HasPrefix(topic, "test") { + return "test" + } - c, _ := courier.NewClient() - otelcourier.NewTracer("service-name").ApplyTraceMiddlewares(c) + return "other" +}) - if err := c.Start(); err != nil { - panic(err) - } +c, _ := courier.NewClient() +otelcourier.New( + "service-name", + // Use this to also track active connections. + otelcourier.WithInfoHandlerFrom(c), + metricLabelMapper, +).ApplyMiddlewares(c) - stopCh := make(chan os.Signal, 1) - signal.Notify(stopCh, []os.Signal{os.Interrupt, syscall.SIGTERM}...) +if err := c.Start(); err != nil { + panic(err) +} - if err := c.Publish( - context.Background(), "test-topic", "message", courier.QOSOne); err != nil { - panic(err) - } - <-stopCh +ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) + +if err := c.Publish( + context.Background(), "test-topic", "message", courier.QOSOne); err != nil { + panic(err) +} - c.Stop() +if err := c.Publish( + context.Background(), "other-topic", "message", courier.QOSOne); err != nil { + panic(err) } + +// Here, you can expose the metrics at /metrics endpoint for prometheus.DefaultRegisterer. + +<-ctx.Done() + +c.Stop() ```