zipkin

package module
v2.0.0-...-ef9e56b Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2019 License: Apache-2.0 Imports: 18 Imported by: 1

README

monkit-zipkin

A plugin for http://github.com/spacemonkeygo/monkit that supports Zipkin.

See the docs at http://godoc.org/gopkg.in/spacemonkeygo/monkit-zipkin.v2

Example usage

Your main method gets set up something like this:

package main

import (
	"net/http"

	"gopkg.in/spacemonkeygo/monkit-zipkin.v2"
	"gopkg.in/spacemonkeygo/monkit.v2"
	"gopkg.in/spacemonkeygo/monkit.v2/environment"
	"gopkg.in/spacemonkeygo/monkit.v2/present"
)

func main() {
	environment.Register(monkit.Default)
	go http.ListenAndServe("localhost:9000", present.HTTP(monkit.Default))
	collector, err := zipkin.NewScribeCollector("zipkin.whatever:9042")
	if err != nil {
		panic(err)
	}
	zipkin.RegisterZipkin(monkit.Default, collector, zipkin.Options{
		Fraction: 1})

	...
}

Once you've done that, you need to make sure your HTTP handlers pull Zipkin Context info from the Request. That's easy to do with zipkin.ContextWrapper.

func HandleRequest(ctx context.Context, w http.ResponseWriter,
  r *http.Request) {
  defer mon.Task()(&ctx)(nil)

  ... whatever
}

func DoTheThing(ctx context.Context) (err error) {
  defer mon.Task()(&ctx)(&err)
  return http.Serve(listener, zipkin.ContextWrapper(
    zipkin.TraceHandler(zipkin.ContextHTTPHandlerFunc(HandleRequest))))
}

Last, your outbound HTTP requests need to pass through Context info:

func MakeRequest(ctx context.Context) (err error) {
  defer mon.Task()(&ctx)(&err)
  req, err := http.NewRequest(...)
  if err != nil {
    return err
  }
  resp, err := zipkin.TraceRequest(ctx, http.DefaultClient, req)
  ...
}

License

Copyright (C) 2016 Space Monkey, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package zipkin provides a monkit plugin for sending traces to Zipkin.

Example usage

Your main method gets set up something like this:

  package main

  import (
	  "net/http"

	  "gopkg.in/spacemonkeygo/monkit-zipkin.v2"
	  "gopkg.in/spacemonkeygo/monkit.v2"
	  "gopkg.in/spacemonkeygo/monkit.v2/environment"
	  "gopkg.in/spacemonkeygo/monkit.v2/present"
  )

  func main() {
	  environment.Register(monkit.Default)
	  go http.ListenAndServe("localhost:9000", present.HTTP(monkit.Default))
	  collector, err := zipkin.NewScribeCollector("zipkin.whatever:9042")
	  if err != nil {
		  panic(err)
	  }
	  zipkin.RegisterZipkin(monkit.Default, collector, zipkin.Options{
		  Fraction: 1})

		...
  }

Once you've done that, you need to make sure your HTTP handlers pull Zipkin Context info from the Request. That's easy to do with zipkin.ContextWrapper.

func HandleRequest(ctx context.Context, w http.ResponseWriter,
  r *http.Request) {
  defer mon.Task()(&ctx)(nil)

  ... whatever
}

func DoTheThing(ctx context.Context) (err error) {
  defer mon.Task()(&ctx)(&err)
  return http.Serve(listener, zipkin.ContextWrapper(
    zipkin.TraceHandler(zipkin.ContextHTTPHandlerFunc(HandleRequest))))
}

Last, your outbound HTTP requests need to pass through Context info:

func MakeRequest(ctx context.Context) (err error) {
  defer mon.Task()(&ctx)(&err)
  req, err := http.NewRequest(...)
  if err != nil {
    return err
  }
  resp, err := zipkin.TraceRequest(ctx, http.DefaultClient, req)
  ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWrapper

func ContextWrapper(h ContextHTTPHandler) http.Handler

ContextWrapper will turn a ContextHTTPHandler into an http.Handler by passing a new Context into every request.

func RedirectPackets

func RedirectPackets(listen_addr string, collector *ScribeCollector) error

RedirectPackets is a method that handles incoming packets from the UDPCollector class. RedirectPackets, when running, will listen for UDP packets containing serialized zipkin.Span objects on listen_addr, then will resend those packets to the given ScribeCollector. On any error, RedirectPackets currently aborts.

func RegisterZipkin

func RegisterZipkin(reg *monkit.Registry, collector TraceCollector,
	opts Options)

RegisterZipkin configures the given Registry reg to send the Spans from some portion of all new Traces to the given TraceCollector.

func TraceRequest

func TraceRequest(ctx context.Context, cl Client, req *http.Request) (
	resp *http.Response, err error)

TraceRequest will perform an HTTP request, creating a new Span for the HTTP request and sending the Span in the HTTP request headers. Compare to http.Client.Do.

Types

type Client

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

Client is an interface that matches an http.Client

type ContextHTTPHandler

type ContextHTTPHandler interface {
	ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)
}

ContextHTTPHandler is like http.Handler, but expects a Context object as the first parameter.

func TraceHandler

func TraceHandler(c ContextHTTPHandler) ContextHTTPHandler

TraceHandler wraps a ContextHTTPHandler with a Span pulled from incoming requests, possibly starting new Traces if necessary.

type ContextHTTPHandlerFunc

type ContextHTTPHandlerFunc func(
	ctx context.Context, w http.ResponseWriter, r *http.Request)

ContextHTTPHandlerFunc is like http.HandlerFunc but for ContextHTTPHandlers

func (ContextHTTPHandlerFunc) ServeHTTP

type HeaderGetter

type HeaderGetter interface {
	Get(string) string
}

HeaderGetter is an interface that http.Header matches for RequestFromHeader

type HeaderSetter

type HeaderSetter interface {
	Set(string, string)
}

HeaderSetter is an interface that http.Header matches for Request.SetHeader

type Options

type Options struct {
	Fraction  float64          // The Fraction of traces to observe.
	Debug     bool             // Whether to set the debug flag on new traces.
	LocalHost *zipkin.Endpoint // What set as the local zipkin.Endpoint
	// contains filtered or unexported fields
}

type Request

type Request struct {
	TraceId  *int64
	SpanId   *int64
	ParentId *int64
	Sampled  *bool
	Flags    *int64
}

Request is a structure representing an incoming RPC request. Every field is optional.

func RequestFromHeader

func RequestFromHeader(header HeaderGetter) (rv Request)

RequestFromHeader will create a Request object given an http.Header or anything that matches the HeaderGetter interface.

func RequestFromSpan

func RequestFromSpan(s *monkit.Span) Request

func (Request) SetHeader

func (r Request) SetHeader(header HeaderSetter)

SetHeader will take a Request and fill out an http.Header, or anything that matches the HeaderSetter interface.

func (Request) Trace

func (zipreq Request) Trace() (trace *monkit.Trace, spanId int64)

type ScribeCollector

type ScribeCollector struct {
	// contains filtered or unexported fields
}

ScribeCollector matches the TraceCollector interface, but writes directly to a connected Scribe socket.

func NewScribeCollector

func NewScribeCollector(scribe_addr string) (*ScribeCollector, error)

NewScribeCollector creates a ScribeCollector. scribe_addr is the address of the Scribe endpoint, typically "127.0.0.1:9410"

func (*ScribeCollector) Close

func (s *ScribeCollector) Close() error

Close closes an existing ScribeCollector

func (*ScribeCollector) Collect

func (c *ScribeCollector) Collect(s *zipkin.Span)

Collect will serialize and send a zipkin.Span to the configured Scribe endpoint

func (*ScribeCollector) CollectSerialized

func (c *ScribeCollector) CollectSerialized(serialized []byte) error

CollectSerialized buffers a serialized zipkin.Span to be sent to the Scribe endpoint. It returns an error and loses the log entry if the buffer is full.

type TraceCollector

type TraceCollector interface {
	// Collect gets called with a Span whenever a Span is completed on a
	// SpanManager.
	Collect(span *zipkin.Span)
}

TraceCollector is an interface dealing with completed Spans on a SpanManager. See RegisterZipkin.

type UDPCollector

type UDPCollector struct {
	// contains filtered or unexported fields
}

UDPCollector matches the TraceCollector interface, but sends serialized zipkin.Span objects over UDP, instead of the Scribe protocol. See RedirectPackets for the UDP server-side code.

func NewUDPCollector

func NewUDPCollector(collector_addr string, buffer_size int) (
	*UDPCollector, error)

NewUDPCollector creates a UDPCollector that sends packets to collector_addr. buffer_size is how many outstanding unsent zipkin.Span objects can exist before Spans start getting dropped.

func (*UDPCollector) Collect

func (c *UDPCollector) Collect(span *zipkin.Span)

Collect takes a zipkin.Span object, serializes it, and sends it to the configured collector_addr.

Directories

Path Synopsis
gen-go

Jump to

Keyboard shortcuts

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