Created
February 26, 2023 20:06
-
-
Save duongcongtoai/82a10f13b3ec12b5291d057a6e70acd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | |
sentry "github.com/getsentry/sentry-go" | |
sentryhttp "github.com/getsentry/sentry-go/http" | |
sentryotel "github.com/getsentry/sentry-go/otel" | |
"github.com/go-chi/chi/v5" | |
"github.com/go-chi/chi/v5/middleware" | |
"go.opentelemetry.io/otel" | |
"go.opentelemetry.io/otel/attribute" | |
"go.opentelemetry.io/otel/exporters/jaeger" | |
"go.opentelemetry.io/otel/propagation" | |
"go.opentelemetry.io/otel/sdk/resource" | |
tracesdk "go.opentelemetry.io/otel/sdk/trace" | |
semconv "go.opentelemetry.io/otel/semconv/v1.17.0" | |
) | |
const ( | |
service = "trace-demo" | |
environment = "production" | |
id = 1 | |
) | |
func init() { | |
initsentry() | |
} | |
// tracerProvider returns an OpenTelemetry TracerProvider configured to use | |
// the Jaeger exporter that will send spans to the provided url. The returned | |
// TracerProvider will also use a Resource configured with all the information | |
// about the application. | |
func tracerProvider() (*tracesdk.TracerProvider, error) { | |
// Create the Jaeger exporter | |
exp, err := jaeger.New(jaeger.WithCollectorEndpoint()) | |
if err != nil { | |
return nil, err | |
} | |
// otel.SetTextMapPropagator(sentryotel.NewSentryPropagator()) | |
tp := tracesdk.NewTracerProvider( | |
// Always be sure to batch in production. | |
tracesdk.WithBatcher(exp), | |
tracesdk.WithSpanProcessor(sentryotel.NewSentrySpanProcessor()), | |
// Record information about this application in a Resource. | |
tracesdk.WithResource(resource.NewWithAttributes( | |
semconv.SchemaURL, | |
semconv.ServiceName(service), | |
attribute.String("environment", environment), | |
attribute.Int64("ID", id), | |
)), | |
) | |
return tp, nil | |
} | |
func main() { | |
tp, err := tracerProvider() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Register our TracerProvider as the global so any imported | |
// instrumentation in the future will default to using it. | |
otel.SetTracerProvider(tp) | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
// Cleanly shutdown and flush telemetry when the application exits. | |
defer func(ctx context.Context) { | |
// Do not make the application hang when it is shutdown. | |
ctx, cancel = context.WithTimeout(ctx, time.Second*5) | |
defer cancel() | |
if err := tp.Shutdown(ctx); err != nil { | |
log.Fatal(err) | |
} | |
}(ctx) | |
r := chi.NewRouter() | |
r.Use(middleware.Logger) | |
r.Use(otelMW()) | |
r.Use(inject()) | |
r.Use(sentryhttp.New(sentryhttp.Options{}).Handle) | |
r.Get("/", func(w http.ResponseWriter, r *http.Request) { | |
hub := sentry.GetHubFromContext(r.Context()) | |
cl, scope := hub.Client(), hub.Scope() | |
cl.CaptureException(fmt.Errorf("dummy"), &sentry.EventHint{Context: r.Context()}, scope) | |
w.Write([]byte("hello")) | |
}) | |
http.ListenAndServe(":3000", r) | |
} | |
func inject() func(http.Handler) http.Handler { | |
return func(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
sentryotel.NewSentryPropagator().Inject(req.Context(), propagation.HeaderCarrier(req.Header)) | |
next.ServeHTTP(w, req) | |
}) | |
} | |
} | |
func otelMW() func(http.Handler) http.Handler { | |
return func(next http.Handler) http.Handler { | |
h := otelhttp.NewHandler(next, "", otelhttp.WithSpanNameFormatter(func(_ string, req *http.Request) string { | |
return "otelhttp mw " + req.URL.Path | |
})) | |
return h | |
} | |
} | |
func initsentry() { | |
err := sentry.Init(sentry.ClientOptions{ | |
Dsn: "http://f0a5065ea10d48ab80eeebfe167f70fc@localhost:9000/1", | |
EnableTracing: true, | |
TracesSampleRate: 1.0, | |
Debug: true, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment