zipkintracing

package
v0.0.0-...-c96edae Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: MIT Imports: 12 Imported by: 0

README

Tracing Library for Go

This library provides tracing for go using Zipkin

Usage

Server Tracing Middleware & http client tracing
package main

import (
	"github.com/labstack/echo-contrib/zipkintracing"
	"github.com/labstack/echo/v4"
	"github.com/openzipkin/zipkin-go"
	zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
	zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http"
	"io/ioutil"
	"net/http"
)

func main() {
	e := echo.New()
	endpoint, err := zipkin.NewEndpoint("echo-service", "")
	if err != nil {
		e.Logger.Fatalf("error creating zipkin endpoint: %s", err.Error())
	}
	reporter := zipkinHttpReporter.NewReporter("http://localhost:9411/api/v2/spans")
	traceTags := make(map[string]string)
	traceTags["availability_zone"] = "us-east-1"
	tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint), zipkin.WithTags(traceTags))
	client, _ := zipkinhttp.NewClient(tracer, zipkinhttp.ClientTrace(true))
	if err != nil {
		e.Logger.Fatalf("tracing init failed: %s", err.Error())
	}
	//Wrap & Use trace server middleware, this traces all server calls
	e.Use(zipkintracing.TraceServer(tracer))
	//....
	e.GET("/echo", func(c echo.Context) error {
		//trace http request calls.
		req, _ := http.NewRequest("GET", "https://echo.labstack.com/", nil)
		resp, _ := zipkintracing.DoHTTP(c, req, client)
		body, _ := ioutil.ReadAll(resp.Body)
		return c.String(http.StatusOK, string(body))
	})

	defer reporter.Close() //defer close reporter
	e.Logger.Fatal(e.Start(":8080"))
}
Reverse Proxy Tracing
package main

import (
	"github.com/labstack/echo-contrib/zipkintracing"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/openzipkin/zipkin-go"
	zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	e := echo.New()
	//new tracing instance
	endpoint, err := zipkin.NewEndpoint("echo-service", "")
	if err != nil {
		e.Logger.Fatalf("error creating zipkin endpoint: %s", err.Error())
	}
	reporter := zipkinHttpReporter.NewReporter("http://localhost:9411/api/v2/spans")
	traceTags := make(map[string]string)
	traceTags["availability_zone"] = "us-east-1"
	tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint), zipkin.WithTags(traceTags))
	if err != nil {
		e.Logger.Fatalf("tracing init failed: %s", err.Error())
	}
	//....
	e.GET("/echo",  func(c echo.Context) error {
		proxyURL, _ := url.Parse("https://echo.labstack.com/")
		httputil.NewSingleHostReverseProxy(proxyURL)
		return nil
	}, zipkintracing.TraceProxy(tracer))

	defer reporter.Close() //close reporter
	e.Logger.Fatal(e.Start(":8080"))

}
Trace function calls

To trace function calls e.g. to trace s3Func

package main

import (
	"github.com/labstack/echo-contrib/zipkintracing"
	"github.com/labstack/echo/v4"
	"github.com/openzipkin/zipkin-go"
)

func s3Func(c echo.Context, tracer *zipkin.Tracer) {
	defer zipkintracing.TraceFunc(c, "s3_read", zipkintracing.DefaultSpanTags, tracer)()
	//s3Func logic here...
}
Create Child Span
package main

import (
	"github.com/labstack/echo-contrib/zipkintracing"
	"github.com/labstack/echo/v4"
	"github.com/openzipkin/zipkin-go"
)

func traceWithChildSpan(c echo.Context, tracer *zipkin.Tracer) {
	span := zipkintracing.StartChildSpan(c, "someMethod", tracer)
	//func logic.....
	span.Finish()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultSpanTags default span tags
	DefaultSpanTags = func(c echo.Context) map[string]string {
		return make(map[string]string)
	}

	//DefaultTraceProxyConfig default config for Trace Proxy
	DefaultTraceProxyConfig = TraceProxyConfig{Skipper: middleware.DefaultSkipper, SpanTags: DefaultSpanTags}

	//DefaultTraceServerConfig default config for Trace Server
	DefaultTraceServerConfig = TraceServerConfig{Skipper: middleware.DefaultSkipper, SpanTags: DefaultSpanTags}
)

Functions

func DoHTTP

func DoHTTP(c echo.Context, r *http.Request, client *zipkinhttp.Client) (*http.Response, error)

DoHTTP is a http zipkin tracer implementation of HTTPDoer

func StartChildSpan

func StartChildSpan(c echo.Context, spanName string, tracer *zipkin.Tracer) (childSpan zipkin.Span)

StartChildSpan starts a new child span as child of parent span from context user must call defer childSpan.Finish()

func TraceFunc

func TraceFunc(c echo.Context, spanName string, spanTags Tags, tracer *zipkin.Tracer) func()

TraceFunc wraps function call with span so that we can trace time taken by func, eventContext only provided if we want to store trace headers

func TraceProxy

func TraceProxy(tracer *zipkin.Tracer) echo.MiddlewareFunc

TraceProxy middleware that traces reverse proxy

func TraceProxyWithConfig

func TraceProxyWithConfig(config TraceProxyConfig) echo.MiddlewareFunc

TraceProxyWithConfig middleware that traces reverse proxy

func TraceServer

func TraceServer(tracer *zipkin.Tracer) echo.MiddlewareFunc

TraceServer middleware that traces server calls

func TraceServerWithConfig

func TraceServerWithConfig(config TraceServerConfig) echo.MiddlewareFunc

TraceServerWithConfig middleware that traces server calls

Types

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has
	// not been written
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a response writer if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type Tags

type Tags func(c echo.Context) map[string]string

Tags func to adds span tags

type TraceProxyConfig

type TraceProxyConfig struct {
	Skipper  middleware.Skipper
	Tracer   *zipkin.Tracer
	SpanTags Tags
}

TraceProxyConfig config for TraceProxyWithConfig

type TraceServerConfig

type TraceServerConfig struct {
	Skipper  middleware.Skipper
	Tracer   *zipkin.Tracer
	SpanTags Tags
}

TraceServerConfig config for TraceServerWithConfig

Jump to

Keyboard shortcuts

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