twirp

package module
v0.0.0-...-7ebff8f Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0, BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package twirp provides tracing functions for tracing clients and servers generated by the twirp framework (https://github.com/twitchtv/twirp).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServerHooks

func NewServerHooks(opts ...Option) *twirp.ServerHooks

NewServerHooks creates the callback hooks for a twirp server to perform tracing. It is used in conjunction with WrapServer.

func WrapServer

func WrapServer(h http.Handler, opts ...Option) http.Handler

WrapServer wraps an http.Handler to add distributed tracing to a Twirp server.

Example
package main

import (
	"context"
	"net/http"

	twirptrace "github.com/DataDog/dd-trace-go/v2/contrib/twitchtv/twirp"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

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

type hatmaker struct{}

func (hatmaker) MakeHat(_ context.Context, _ *example.Size) (*example.Hat, error) {
	return &example.Hat{
		Size:  42,
		Color: "cornflower blue",
		Name:  "oversized blue hat",
	}, nil
}

func main() {
	tracer.Start()
	defer tracer.Stop()

	server := example.NewHaberdasherServer(hatmaker{}, twirptrace.NewServerHooks())
	traced := twirptrace.WrapServer(server)
	http.ListenAndServe(":8080", traced)
}
Output:

Types

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is duplicated from twirp's generated service code. It is declared in this package so that the client can be wrapped to initiate traces.

func WrapClient

func WrapClient(c HTTPClient, opts ...Option) HTTPClient

WrapClient wraps an HTTPClient to add distributed tracing to its requests.

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	twirptrace "github.com/DataDog/dd-trace-go/v2/contrib/twitchtv/twirp"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

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

func main() {
	tracer.Start()
	defer tracer.Stop()

	client := example.NewHaberdasherJSONClient("http://localhost:8080", twirptrace.WrapClient(&http.Client{}))
	for i := 0; i < 10; i++ {
		hat, err := client.MakeHat(context.Background(), &example.Size{Inches: 6})
		if err != nil {
			fmt.Println("error making hat:", err)
			continue
		}
		fmt.Println("made hat:", hat)
	}
}
Output:

type Option

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

Option describes options for the Twirp integration.

type OptionFn

type OptionFn func(*config)

OptionFn represents options applicable to NewServerHooks, WrapClient and WrapServer.

func WithAnalytics

func WithAnalytics(on bool) OptionFn

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) OptionFn

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func WithService

func WithService(name string) OptionFn

WithService sets the given service name for the dialled connection. When the service name is not explicitly set, it will be inferred based on the request to the twirp service.

Jump to

Keyboard shortcuts

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