twirpotel

package module
v0.0.0-...-bfa7bdb Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 6 Imported by: 1

README

Twirp Opentelemetry Tracing for Go

PkgGoDev

twirpotel provides client and server interceptors for twirp.

See documentation for example usage.

LICENSE

See LICENSE

This project is not an official project of any employer past or present. This project was done using personal equipment, accounts, and time.

Documentation

Overview

Package twirpotel provides tracing for twirp servers and clients.

Index

Examples

Constants

View Source
const (

	// InstrumentationName is the name used for intrumentation in traces.
	InstrumentationName = "github.com/bakins/twirpotel"

	// PackageNameKey is the twirp package name
	PackageNameKey = attribute.Key("twirp.package")

	// ServiceNameKey is the twirp service name
	ServiceNameKey = attribute.Key("twirp.service")

	// MethodNameKey is the twirp method name
	MethodNameKey = attribute.Key("twirp.method")

	// ErrorCodeKey is the twirp error code
	ErrorCodeKey = attribute.Key("twirp.error_code")

	// ErrorMessageKey is the twirp error message
	ErrorMessageKey = attribute.Key("twirp.error_message")
)

Variables

View Source
var NoErrorCode = attribute.StringValue("ok")

NoErrorCode is the ErrorCode used when there is no error

Functions

func ClientInterceptor

func ClientInterceptor(options ...Option) twirp.Interceptor

ClientInterceptor creates interceptors suitable to be used with twirp client.

func ServerInterceptor

func ServerInterceptor(options ...Option) twirp.Interceptor

ServerInterceptor creates interceptors suitable to be used with a twirp server.

Example
package main

import (
	"context"
	"math/rand"
	"net/http"

	"github.com/twitchtv/twirp"
	"github.com/twitchtv/twirp/example"

	"github.com/bakins/twirpotel"
)

func main() {
	// Add the server interceptor to your twirp server
	ts := example.NewHaberdasherServer(
		&randomHaberdasher{}, // implements example.Haberdasher
		twirp.WithServerInterceptors(twirpotel.ServerInterceptor()),
	)

	http.Handle(ts.PathPrefix(), ts)
}

type randomHaberdasher struct{}

func (h *randomHaberdasher) MakeHat(ctx context.Context, size *example.Size) (*example.Hat, error) {
	if size.Inches <= 0 {
		return nil, twirp.InvalidArgumentError("Inches", "I can't make a hat that small!")
	}
	colors := []string{"white", "black", "brown", "red", "blue"}
	names := []string{"bowler", "baseball cap", "top hat", "derby"}
	return &example.Hat{
		Size:  size.Inches,
		Color: colors[rand.Intn(len(colors))],
		Name:  names[rand.Intn(len(names))],
	}, nil
}
Output:

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option applies an option value for a config.

func WithTracerProvider

func WithTracerProvider(provider trace.TracerProvider) Option

WithTracerProvider returns an Option to use the TracerProvider when creating a Tracer.

Default is to attempt to get from parent span and then otel.GetTracerProvider().

Jump to

Keyboard shortcuts

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