octwirp

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

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

Go to latest
Published: May 15, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

GoDoc

OpenCensus Tracing and Metrics for Twirp in Go

octwirp provides server hooks for twirp servers for metrics and tracing.

octwirp also provides an http roundtripper for twirp clients for metrics and tracing.

Usage

To install locally, go get -u github.com/bakins/octwirp or use a Go dependency manager to install.

Server

To enable tracing and metrics for twirp servers:

import (
    "net/http"

    "github.com/twitchtv/twirp/example"
    "github.com/bakins/octwirp"
    "go.opencensus.io/stats/view"
    "go.opencensus.io/plugin/ochttp"
)

func main() {
    if err := view.Register(octwirp.ServerLatencyView, octwirp.ServerResponseView); err != nil {
		log.Fatalf("failed to register metrics views: %v", err)
    }
    
    t := &octwirp.Tracer{}
    server := example.NewHaberdasherServer(&randomHaberdasher{}, t.ServerHooks())
    handler := t.WrapHandler(server)
    http.Handle(server.PathPrefix(), handler)
	log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
}

You also need to register Opencensus exporters for metrics and tracing.

For a more complete server example, see ./examples/server/main.go

Client

To enable metrics and tracing for a twirp client:

import (
    "net/http"

    "github.com/bakins/octwirp"
    "go.opencensus.io/stats/view"
    "github.com/twitchtv/twirp/example"
)

// see https://godoc.org/go.opencensus.io/plugin/ochttp
t := ochttp.Transport{}

c := http.Client{
		Transport: octwirp.WrapTransport(&t),
    }
    
client := example.NewHaberdasherProtobufClient("http://localhost:8080", &c)

For a more complete client example, see ./examples/client/main.go

Status

This project is under active development. I have only recently began using twirp, so I am sure there are better ways to do things.

LICENSE

See LICENSE

Documentation

Overview

Package octwirp provides opencensus metrics and tracing for twirp services

Index

Constants

This section is empty.

Variables

View Source
var (
	// PackageName is the twirp package
	PackageName, _ = tag.NewKey("twirp.package")

	// ServiceName is the twirp service
	ServiceName, _ = tag.NewKey("twirp.service")

	// MethodName is the twirp method
	MethodName, _ = tag.NewKey("twirp.method")

	// StatusCode is the twirp code
	StatusCode, _ = tag.NewKey("twirp.status")

	// ServerLatency measures server side latency
	ServerLatency = stats.Float64(
		"twirp/server/latency",
		"End-to-end latency",
		stats.UnitMilliseconds)

	// ServerLatencyView measures the latency distribution of HTTP requests
	ServerLatencyView = &view.View{
		Name:        "twirp/server/latency",
		Description: "Latency distribution of HTTP requests",
		Measure:     ServerLatency,
		Aggregation: ochttp.DefaultLatencyDistribution,
		TagKeys:     []tag.Key{PackageName, ServiceName, MethodName, StatusCode},
	}

	// ServerResponseView measures the server response count
	ServerResponseView = &view.View{
		Name:        "twirp/server/response_count",
		Description: "Server response count",
		TagKeys:     []tag.Key{PackageName, ServiceName, MethodName, StatusCode},
		Measure:     ServerLatency,
		Aggregation: view.Count(),
	}

	// ClientRoundtripLatency measures end to end latency from the client perspective.
	ClientRoundtripLatency = &view.View{
		Name:        "twirp/client/roundtrip_latency",
		Measure:     ochttp.ClientRoundtripLatency,
		Aggregation: ochttp.DefaultLatencyDistribution,
		Description: "End-to-end latency",
		TagKeys:     []tag.Key{PackageName, ServiceName, MethodName, ochttp.StatusCode},
	}
)

Functions

func WrapTransport

func WrapTransport(base *ochttp.Transport) http.RoundTripper

WrapTransport wraps the ochttp transport to inject twirp metadata.

Types

type Tracer

type Tracer struct {
	Propagation  propagation.HTTPFormat
	StartOptions trace.StartOptions
}

Tracer adds Opencensus tracing and metrics to twirp servers.

func (*Tracer) ServerHooks

func (t *Tracer) ServerHooks() *twirp.ServerHooks

ServerHooks creates twrip server hooks for Opencensus tracing and metrics.

func (*Tracer) WrapHandler

func (t *Tracer) WrapHandler(handler http.Handler) http.Handler

WrapHandler wraps an http handler to inject Opencensus tracing and metrics.

Directories

Path Synopsis
examples
client
based on https://github.com/twitchtv/twirp/blob/337e90237d72193bf7f9fa387b5b9946436b7733/example/cmd/client/main.go Copyright 2018 Twitch Interactive, Inc.
based on https://github.com/twitchtv/twirp/blob/337e90237d72193bf7f9fa387b5b9946436b7733/example/cmd/client/main.go Copyright 2018 Twitch Interactive, Inc.

Jump to

Keyboard shortcuts

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