Giter VIP home page Giter VIP logo

signalfx-go-tracing's Introduction

SignalFx Tracing Library for Go

GitHub Release Go Reference go.mod CircleCI

The SignalFx Tracing Library for Go helps you automatically instrument Go applications with instrumented library helpers and a tracer to capture and report distributed traces to SignalFx. You can also use the library to manually instrument Go applications with the OpenTracing API.

Requirements and supported software

These are the requirements to use the SignalFx Tracing Library for Go:

These are the supported libraries you can instrument:

Library Version
database/sql Standard Library
github.com/gin-gonic/gin v1.6.2
github.com/gorilla/mux v1.7.4
github.com/labstack/echo v4.2.1
go.mongodb.org/mongo-driver v1.3.2
net/http Standard Library

Other libraries are available to instrument, but are in beta and aren't officially supported. You can view all the libraries in the contrib directory.

Configuration values

If the default configuration values don't apply for your environment, override them before running the process you instrument.

Code Environment Variable Default Value Notes
WithServiceName SIGNALFX_SERVICE_NAME SignalFx-Tracing The name of the service.
WithEndpointURL SIGNALFX_ENDPOINT_URL http://localhost:9080/v1/trace The URL to send traces to. Send spans to a Smart Agent, OpenTelemetry Collector, or a SignalFx ingest endpoint.
WithAccessToken SIGNALFX_ACCESS_TOKEN none The access token for your SignalFx organization.
WithGlobalTag SIGNALFX_SPAN_TAGS none Comma-separated list of tags included in every reported span. For example, "key1:val1,key2:val2". Use only string values for tags.
WithRecordedValueMaxLength SIGNALFX_RECORDED_VALUE_MAX_LENGTH 1200 The maximum number of characters for any Zipkin-encoded tagged or logged value. Behaviour disabled when set to -1.
- SIGNALFX_TRACE_RESPONSE_HEADER_ENABLED true Adds Server-Timing header to HTTP responses for net/http and github.com/gorilla/mux instrumentations.

Instrument a Go application

Follow these steps to instrument target libraries with provided instrumentors.

For more information about how to instrument a Go application, see the examples.

  1. Import github.com/signalfx/signalfx-go-tracing to your go mod.

  2. Import the instrumentor for the target library you want to instrument and replace utilities with their traced equivalents.

    Find an instrumentor for each supported target library in the contrib directory. Each instrumentor has an example_test.go file that demonstrates how to instrument a target library. Don't import examples directly in your application.

  3. Enable tracing globally with tracing.Start(). This creates a tracer and registers it as the OpenTracing global tracer.

Inject trace context in HTTP requests

Link individual log entries with trace IDs and span IDs associated with corresponding events by passing metadata in HTTP reqeusts.

To inject trace context in HTTP requests, follow these steps:

  1. Replace the default HTTP multiplexer:

    import (
      httptrace "github.com/signalfx/signalfx-go-tracing/contrib/net/http"
      "github.com/signalfx/signalfx-go-tracing/ddtrace/tracer" // global tracer
      "github.com/signalfx/signalfx-go-tracing/tracing" // helper
    )
  2. Create the new HTTP multiplexer and attach it to your HTTP instance:

    mux := httptrace.NewServeMux()
    mux.HandleFunc("/", handler) // handler to your function
    // add all other routes
    http.ListenAndServe(":8888", mux)
  3. Use a helper for the tracer to get the span ID and trace ID from each request:

    func TraceIdFromCtx(ctx context.Context) (result string) {
      if span, ok := tracer.SpanFromContext(ctx); ok {
        result = tracer.TraceIDHex(span.Context())
      }
      return
    }

    It cooperates with the global tracer instance.

Example log injection configuration

This is an example of what injecting span IDs and trace IDs can look like in your environment:

package main

import (
  httptrace "github.com/signalfx/signalfx-go-tracing/contrib/net/http"
  "github.com/signalfx/signalfx-go-tracing/ddtrace/tracer" // global tracer
  "github.com/signalfx/signalfx-go-tracing/tracing" // helper

  "context"
  "fmt"
  "log"
  "net/http"
)

func TraceIdFromCtx(ctx context.Context) (result string) {
  if span, ok := tracer.SpanFromContext(ctx); ok {
    result = tracer.TraceIDHex(span.Context())
  }
  return
}

func handler(w http.ResponseWriter, r *http.Request) {
  ctx := r.Context()
  log.Print("Recevied request for ", r.URL.Path[1:], " ", TraceIdFromCtx(ctx))
}

func main() {
  tracing.Start()
  defer tracing.Stop()
  mux.HandleFunc("/", handler)
  http.ListenAndServe(":8888", mux)
}

Testing

To run integration tests locally, you should set the INTEGRATION environment variable. The dependencies of the integration tests are best run via Docker. To get an idea about the versions and the set-up take a look at our CI config.

The best way to run the entire test suite is using the CircleCI CLI. Simply run circleci build in the repository root. Note that you might have to increase the resources dedicated to Docker to around 4GB.

signalfx-go-tracing's People

Contributors

awang avatar bmermet avatar brianashby avatar ccaraman avatar cgilmour avatar clutchski avatar conorbranagan avatar dd-caleb avatar dependabot[bot] avatar ejholmes avatar elijahandrews avatar flands avatar galdor avatar gbbr avatar hawknewton avatar jchengsfx avatar leocavaille avatar lotharsee avatar mogigoma avatar owais avatar pellared avatar pierrre avatar raphaelgavache avatar rmfitzpatrick avatar samsaggace avatar seonsfx avatar talwai avatar ufoot avatar vlad-mh avatar willgittoes-dd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.