ocsentry

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 5 Imported by: 1

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://abc123abc123abc123abc123@o123456.ingest.sentry.io/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
	}
}

Documentation

Overview

Package ocsentry provides OpenCensus instrumentation for Sentry.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(ctx context.Context, input interface{}, err error)

ErrorHandler is a handler for ErrorCatcher of github.com/swaggest/usecase.

func HTTPHandlerMiddleware

func HTTPHandlerMiddleware(h http.Handler) http.Handler

HTTPHandlerMiddleware prepares request context without creating sentry trace span for trace span is to be created by OpenCensus instrumentation.

func OnPanic

func OnPanic(ctx context.Context, rcv interface{})

OnPanic handles recovered panics.

func WrapTracer

func WrapTracer(tracer trace.Tracer, options ...Option) trace.Tracer

WrapTracer creates a tracer that manages Sentry spans together with OpenCensus spans.

Example
// Initialize Sentry.
err := sentry.Init(sentry.ClientOptions{
	Dsn:        "https://abc123abc123abc123abc123@o123456.ingest.sentry.io/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
}
Output:

Types

type Option added in v0.1.1

type Option func(o *Options)

Option is a functional option.

func SkipTransactionNames added in v0.1.1

func SkipTransactionNames(names ...string) Option

SkipTransactionNames is an option to skip transactions with provided names from sentry collection.

Can be useful to filter out automated requests that do not bring value, e.g. "GET /health", "GET /metrics".

type Options added in v0.1.1

type Options struct {
	SkipTransactionNames []string
}

Options control trace wrapper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL