Tracking system request with Open telemetry, sentry and Go-in Golang language
Use Open Telemetry Tracing system in sentry to understand what’s going on in the backend.
Special thanks to Mohsen Mottaghi.
Go-Micro Plugin for Using Opentelemetry in Go-Micro
Add tracking open telemetry in Go-micro
import SDKTrace "go.opentelemetry.io/otel/sdk/trace" sentryOtel
"github.com/getsentry/sentry-go/otel"Add Tracer in main.go
tp := SDKTrace.NewTracerProvider( SDKTrace.WithSpanProcessor(sentryOtel.NewSentrySpanProcessor()),
) otel.SetTracerProvider(tp) otel.SetTextMapPropagator(sentryOtel.NewSentryPropagator())
entity.TracerMain = tp.Tracer(service)Add Tracking system in service
srv := micro.NewService( ... //Tracking
micro.WrapClient(opentelemetry.NewClientWrapper(opentelemetry.WithTraceProvider(tp))),
micro.WrapCall(opentelemetry.NewCallWrapper(opentelemetry.WithTraceProvider(tp))),
micro.WrapHandler(opentelemetry.NewHandlerWrapper(opentelemetry.WithTraceProvider(tp))),
micro.WrapSubscriber(opentelemetry.NewSubscriberWrapper(opentelemetry.WithTraceProvider(tp))),)Sentry support OpenTelemetry
With Sentry’s OpenTelemetry SDK, an OpenTelemetry Span becomes a Sentry Transaction or Span. The first Span sent through the Sentry SpanProcessor is a Transaction, and any child Span gets attached to the first Transaction upon checking the parent Span context. This is true for the OpenTelemetry root Span and any top level Span in the system. For example, a request sent from frontend to backend will create an OpenTelemetry root Span with a corresponding Sentry Transaction. The backend request will create a new Sentry Transaction for the OpenTelemetry Span. The Sentry Transaction and Span are linked as a trace for navigation and error tracking purposes.
import (
"go.opentelemetry.io/otel" sdktrace "go.opentelemetry.io/otel/sdk/trace"
"github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go/otel" //...)sentry.Init(sentry.ClientOptions{
Dsn: "__DSN__", EnableTracing:
true, TracesSampleRate: 1.0, Debug: true,
})tp :=
sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sentryotel.NewSentrySpanProcessor()),)otel.SetTracerProvider(tp)otel.SetTextMapPropagator(sentryotel.NewSentryPropagator()Capture data with open telemetry
Creating Spans
ctx, span := tracer.Start(ctx, "hello-span")defer span.End()Span Attributes
Attributes are keys and values that are applied as metadata to your spans and are useful for aggregating, filtering, and grouping traces. Attributes can be added at span creation, or at any other time during the lifecycle of a span before it has completed.
span.SetAttributes(attribute.Bool("isTrue", true), attribute.String("stringAttr", "hi!"))Events
An event is a human-readable message on a span that represents “something happening” during it’s lifetime.
span.AddEvent("some things happened here!")In this case we can easily use sentry capture message too.
Set span status
A status can be set on a span, typically used to specify that there was an error in the operation a span is tracking — .Error
span.SetStatus(codes.Error, "operationThatCouldFail failed")