Categorygithub.com/vearutop/ocsentry
modulepackage
0.1.2
Repository: https://github.com/vearutop/ocsentry.git
Documentation: pkg.go.dev

# README

ocsentry

Build Status Coverage Status GoDevDoc Time Tracker Code lines Comments

Provides OpenCensus traces support for Sentry.

Sentry Trace

Why?

OpenCensus has tracing instrumentations for a variety of technologies (databases, services, caches, etc...), this library enables those instrumentations for Sentry performance tools with minimal effort.

Also implementing traces with Sentry client directly makes it harder to change the technology in the future, as opposed to vendor-unlocked OpenCensus.

Usage

  • Replace default tracer with a wrapper: trace.DefaultTracer = ocsentry.WrapTracer(trace.DefaultTracer).
  • Use ocsentry.HTTPHandlerMiddleware instead of github.com/getsentry/sentry-go/http.(*Handler).Handle so that original sentry tracer does not duplicate the ochttp job.
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/getsentry/sentry-go"
	"github.com/vearutop/ocsentry"
	"go.opencensus.io/plugin/ochttp"
	"go.opencensus.io/trace"
)

func main() {
	// Initialize Sentry.
	err := sentry.Init(sentry.ClientOptions{
		Dsn:        "https://[email protected]/1234567",
		ServerName: "my-service",
		Release:    "v1.2.3",
	})
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		sentry.Flush(time.Second)
	}()

	// Setup OC sampling.
	trace.ApplyConfig(trace.Config{
		DefaultSampler: trace.ProbabilitySampler(0.01),
	})

	// Enable Sentry wrapper.
	trace.DefaultTracer = ocsentry.WrapTracer(trace.DefaultTracer)

	var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, err := w.Write([]byte("Hello, world!"))
		if err != nil {
			log.Print(err)

			return
		}
	})

	// Apply OpenCensus middleware and Sentry middlewares to your http.Handler.
	h = ocsentry.HTTPHandlerMiddleware(h)
	h = &ochttp.Handler{Handler: h}

	if err := http.ListenAndServe(":80", h); err != nil {
		log.Print(err)

		return
	}
}

# Functions

ErrorHandler is a handler for ErrorCatcher of github.com/swaggest/usecase.
HTTPHandlerMiddleware prepares request context without creating sentry trace span for trace span is to be created by OpenCensus instrumentation.
OnPanic handles recovered panics.
SkipTransactionNames is an option to skip transactions with provided names from sentry collection.
WrapTracer creates a tracer that manages Sentry spans together with OpenCensus spans.

# Structs

Options control trace wrapper.

# Type aliases

Option is a functional option.