Development Environment Configuration

To configure the Jaeger client library for development, you need to set various options that control how the library interacts with other components, especially in relation to metrics and tracing. Below are the detailed steps and code examples for performing this setup.

1. Import Libraries

Ensure that you have imported the necessary libraries in your Go code. The primary import for jaeger-lib is shown below:

import (
    "github.com/uber/jaeger-lib"
)

2. Metrics Configuration

The Jaeger library requires metrics to be configured. You can utilize different metrics backends, such as Go-Kit, Prometheus, or Expvar. Below are examples for each.

Go-Kit Metrics

To configure Go-Kit metrics, you can use the following setup:

import (
    "github.com/go-kit/kit/metrics"
    "github.com/uber/jaeger-lib/metrics/go-kit"
)

f := gokit.NewFactory(/* your options here */)
counter := f.Counter("example_counter")
counter.Add(1)

Prometheus Metrics

For Prometheus, the configuration can be done as follows:

import (
    "github.com/uber/jaeger-lib/metrics/prometheus"
)

func initMetrics() {
    options := prometheus.Options{
        Registerer: prometheus.DefaultRegisterer,
    }
    
    factory := prometheus.NewFactory(options)
    counter := factory.Counter("example_counter")
    counter.Inc() // Increment the counter
}

Expvar Metrics

For using Expvar metrics, configure as follows:

import (
    "github.com/uber/jaeger-lib/metrics/expvar"
)

func initMetrics() {
    factory := expvar.NewFactory(10)
    counter := factory.Counter("example_counter")
    counter.Add(42) // Add to the counter
}

3. Applying Options

To apply options, use the following pattern which includes functional options for customization.

options := new(options)

func applyOptions(opts []Option) *options {
    for _, o := range opts {
        o(options)
    }
    // Set default options
    if options.registerer == nil {
        options.registerer = prometheus.DefaultRegisterer
    }
    if options.separator == '\x00' {
        options.separator = SeparatorUnderscore
    }
    return options
}

4. Default Options

It’s also essential to ensure default options are set correctly:

func defaultOptions(options Options) Options {
    o := options
    if o.ScopeSep == "" {
        o.ScopeSep = "."
    }
    if o.TagsSep == "" {
        o.TagsSep = "."
    }
    if o.TagKVSep == "" {
        o.TagKVSep = "_" 
    }
    return o
}

5. Environment Specific Configuration

For environment-specific configurations, you may want to adjust your settings based on the application configuration, such as connecting to different monitoring backends or enabling/disabling traces based on the development versus production setup.

Here’s a sample code snippet for toggling metrics based on an environment variable:

import (
    "os"
)

func init() {
    if os.Getenv("ENABLE_TRACING") == "true" {
        // Enable tracing and metrics.
        initMetrics()
    } else {
        // Disable or set to dev-specific metrics.
    }
}

Conclusion

This guide outlines the configuration options for setting up Jaeger in a development environment, focusing on metrics, options application, and environment configuration. The above code snippets provide practical examples to illustrate how to achieve a tailored setup using jaeger-lib.

Source: README.md (distance: 1.3132176026960038)