Jaeger

Jaeger - 图1note

Echo community contribution

Trace requests on Echo framework with Jaeger Tracing Middleware.

Usage

  1. package main
  2. import (
  3. "github.com/labstack/echo-contrib/jaegertracing"
  4. "github.com/labstack/echo/v4"
  5. )
  6. func main() {
  7. e := echo.New()
  8. // Enable tracing middleware
  9. c := jaegertracing.New(e, nil)
  10. defer c.Close()
  11. e.Logger.Fatal(e.Start(":1323"))
  12. }

Enabling the tracing middleware creates a tracer and a root tracing span for every request.

Custom Configuration

By default, traces are sent to localhost Jaeger agent instance. To configure an external Jaeger, start your application with environment variables.

Usage

  1. $ JAEGER_AGENT_HOST=192.168.1.10 JAEGER_AGENT_PORT=6831 ./myserver

The tracer can be initialized with values coming from environment variables. None of the env vars are required and all of them can be overriden via direct setting of the property on the configuration object.

PropertyDescription
JAEGER_SERVICE_NAMEThe service name
JAEGER_AGENT_HOSTThe hostname for communicating with agent via UDP
JAEGER_AGENT_PORTThe port for communicating with agent via UDP
JAEGER_ENDPOINTThe HTTP endpoint for sending spans directly to a collector, i.e. http://jaeger-collector:14268/api/traces
JAEGER_USERUsername to send as part of “Basic” authentication to the collector endpoint
JAEGER_PASSWORDPassword to send as part of “Basic” authentication to the collector endpoint
JAEGER_REPORTER_LOG_SPANSWhether the reporter should also log the spans
JAEGER_REPORTER_MAX_QUEUE_SIZEThe reporter’s maximum queue size
JAEGER_REPORTER_FLUSH_INTERVALThe reporter’s flush interval, with units, e.g. “500ms” or “2s” ([valid units][timeunits])
JAEGER_SAMPLER_TYPEThe sampler type
JAEGER_SAMPLER_PARAMThe sampler parameter (number)
JAEGER_SAMPLER_MANAGER_HOST_PORTThe HTTP endpoint when using the remote sampler, i.e. http://jaeger-agent:5778/sampling
JAEGER_SAMPLER_MAX_OPERATIONSThe maximum number of operations that the sampler will keep track of
JAEGER_SAMPLER_REFRESH_INTERVALHow often the remotely controlled sampler will poll jaeger-agent for the appropriate sampling strategy, with units, e.g. “1m” or “30s” ([valid units][timeunits])
JAEGER_TAGSA comma separated list of name = value tracer level tags, which get added to all reported spans. The value can also refer to an environment variable using the format ${envVarName:default}, where the :default is optional, and identifies a value to be used if the environment variable cannot be found
JAEGER_DISABLEDWhether the tracer is disabled or not. If true, the default opentracing.NoopTracer is used.
JAEGER_RPC_METRICSWhether to store RPC metrics

By default, the client sends traces via UDP to the agent at localhost:6831. Use JAEGER_AGENT_HOST and JAEGER_AGENT_PORT to send UDP traces to a different host:port. If JAEGER_ENDPOINT is set, the client sends traces to the endpoint via HTTP, making the JAEGER_AGENT_HOST and JAEGER_AGENT_PORT unused. If JAEGER_ENDPOINT is secured, HTTP basic authentication can be performed by setting the JAEGER_USER and JAEGER_PASSWORD environment variables.

Skipping URL(s)

A middleware skipper can be passed to avoid tracing spans to certain URL(s).

Usage

  1. package main
  2. import (
  3. "strings"
  4. "github.com/labstack/echo-contrib/jaegertracing"
  5. "github.com/labstack/echo/v4"
  6. )
  7. // urlSkipper ignores metrics route on some middleware
  8. func urlSkipper(c echo.Context) bool {
  9. if strings.HasPrefix(c.Path(), "/testurl") {
  10. return true
  11. }
  12. return false
  13. }
  14. func main() {
  15. e := echo.New()
  16. // Enable tracing middleware
  17. c := jaegertracing.New(e, urlSkipper)
  18. defer c.Close()
  19. e.Logger.Fatal(e.Start(":1323"))
  20. }

TraceFunction

This is a wrapper function that can be used to seamlessly add a span for the duration of the invoked function. There is no need to change function arguments.

Usage

  1. package main
  2. import (
  3. "github.com/labstack/echo-contrib/jaegertracing"
  4. "github.com/labstack/echo/v4"
  5. "net/http"
  6. "time"
  7. )
  8. func main() {
  9. e := echo.New()
  10. // Enable tracing middleware
  11. c := jaegertracing.New(e, nil)
  12. defer c.Close()
  13. e.GET("/", func(c echo.Context) error {
  14. // Wrap slowFunc on a new span to trace it's execution passing the function arguments
  15. jaegertracing.TraceFunction(c, slowFunc, "Test String")
  16. return c.String(http.StatusOK, "Hello, World!")
  17. })
  18. e.Logger.Fatal(e.Start(":1323"))
  19. }
  20. // A function to be wrapped. No need to change it's arguments due to tracing
  21. func slowFunc(s string) {
  22. time.Sleep(200 * time.Millisecond)
  23. return
  24. }

CreateChildSpan

For more control over the Span, the function CreateChildSpan can be called giving control on data to be appended to the span like log messages, baggages and tags.

Usage

  1. package main
  2. import (
  3. "github.com/labstack/echo-contrib/jaegertracing"
  4. "github.com/labstack/echo/v4"
  5. )
  6. func main() {
  7. e := echo.New()
  8. // Enable tracing middleware
  9. c := jaegertracing.New(e, nil)
  10. defer c.Close()
  11. e.GET("/", func(c echo.Context) error {
  12. // Do something before creating the child span
  13. time.Sleep(40 * time.Millisecond)
  14. sp := jaegertracing.CreateChildSpan(c, "Child span for additional processing")
  15. defer sp.Finish()
  16. sp.LogEvent("Test log")
  17. sp.SetBaggageItem("Test baggage", "baggage")
  18. sp.SetTag("Test tag", "New Tag")
  19. time.Sleep(100 * time.Millisecond)
  20. return c.String(http.StatusOK, "Hello, World!")
  21. })
  22. e.Logger.Fatal(e.Start(":1323"))
  23. }

References