Interactions

The jaeger-lib library provides a set of shared infrastructure libraries used by different components of Jaeger backend and jaeger-client-go.

  • metrics: This package provides an abstraction for collecting and exporting metrics. It supports various backends, including Prometheus, InfluxDB, and Go-Kit.
  • fork: This package allows you to create a “fork” of the default metrics factory. This can be useful if you need to track metrics separately for a specific component or service.

For more information, please refer to the project’s documentation: https://godoc.org/github.com/uber/jaeger-lib

Metrics

Usage

The metrics package provides a number of different ways to interact with metrics.

  • Counter: Counter is an interface that tracks the number of times an event has occurred. You can increment a counter using the Inc method.
package main
          
          import (
              "github.com/uber/jaeger-lib/metrics"
          )
          
          func main() {
              // Create a new counter
              counter := metrics.NewCounter(metrics.Options{
                  Name: "my_counter",
              })
          
              // Increment the counter
              counter.Inc(1)
          }
          
  • Timer: Timer is an interface that accumulates observations about how long some operation took and maintains a histogram of percentiles. You can record a time duration using the Record method.
package main
          
          import (
              "time"
          
              "github.com/uber/jaeger-lib/metrics"
          )
          
          func main() {
              // Create a new timer
              timer := metrics.NewTimer(metrics.TimerOptions{
                  Name: "my_timer",
              })
          
              // Record a time duration
              timer.Record(time.Second)
          }
          
  • Gauge: Gauge is an interface that represents a single numerical value that can arbitrarily go up and down. You can update a gauge using the Update method.
package main
          
          import (
              "github.com/uber/jaeger-lib/metrics"
          )
          
          func main() {
              // Create a new gauge
              gauge := metrics.NewGauge(metrics.Options{
                  Name: "my_gauge",
              })
          
              // Update the gauge
              gauge.Update(10)
          }
          
  • Histogram: Histogram is an interface that tracks the distribution of a set of values. You can record a value using the Record method.
package main
          
          import (
              "github.com/uber/jaeger-lib/metrics"
          )
          
          func main() {
              // Create a new histogram
              histogram := metrics.NewHistogram(metrics.HistogramOptions{
                  Name: "my_histogram",
              })
          
              // Record a value
              histogram.Record(10)
          }
          
  • Factory: Factory is an interface that allows you to create new instances of Counter, Timer, Gauge, and Histogram. It also provides a method for creating nested metrics factories, which allows you to group metrics by namespace.
package main
          
          import (
              "github.com/uber/jaeger-lib/metrics"
          )
          
          func main() {
              // Create a new metrics factory
              factory := metrics.NewFactory(metrics.FactoryOptions{
                  // Specify backend
              })
          
              // Create a new counter
              counter := factory.Counter(metrics.Options{
                  Name: "my_counter",
              })
          
              // Increment the counter
              counter.Inc(1)
          }
          

Backend Options

The metrics package supports various backend options. These include:

  • Prometheus: The Prometheus backend exports metrics in a format that can be consumed by the Prometheus monitoring system.
  • InfluxDB: The InfluxDB backend exports metrics in a format that can be consumed by the InfluxDB time series database.
  • Go-Kit: The Go-Kit backend exports metrics in a format that can be consumed by the Go-Kit metrics library.
  • Fork: The Fork backend provides the ability to create a “fork” of the default metrics factory, which allows you to track metrics separately for a specific component or service.

Fork

The fork package allows you to create a “fork” of the default metrics factory.

Usage

package main
          
          import (
              "github.com/uber/jaeger-lib/metrics"
              "github.com/uber/jaeger-lib/metrics/fork"
          )
          
          func main() {
              // Create a new metrics factory
              defaultFactory := metrics.NewFactory(metrics.FactoryOptions{
                  // Specify backend
              })
          
              // Create a fork of the default factory
              forkFactory := fork.New("my_fork", defaultFactory, defaultFactory)
          
              // Create a new counter in the forked factory
              counter := forkFactory.Counter(metrics.Options{
                  Name: "my_counter",
              })
          
              // Increment the counter
              counter.Inc(1)
          }
          

Example

package main
          
          import (
              "time"
          
              "github.com/uber/jaeger-lib/metrics"
              "github.com/uber/jaeger-lib/metrics/fork"
          )
          
          func main() {
              // Create a new metrics factory
              defaultFactory := metrics.NewFactory(metrics.FactoryOptions{
                  // Specify backend
              })
          
              // Create a fork of the default factory
              forkFactory := fork.New("internal", defaultFactory, defaultFactory)
          
              // Get default namespaced factory
              defaultNamespacedFactory := forkFactory.Namespace(metrics.NSOptions{
                  Name: "default",
              })
          
              // Add some metrics
              defaultNamespacedFactory.Counter(metrics.Options{
                  Name: "somenamespacedcounter",
              }).Inc(111)
              defaultNamespacedFactory.Gauge(metrics.Options{
                  Name: "somenamespacedgauge",
              }).Update(222)
              defaultNamespacedFactory.Histogram(metrics.HistogramOptions{
                  Name: "somenamespacedhist",
              }).Record(1)
              defaultNamespacedFactory.Timer(metrics.TimerOptions{
                  Name: "somenamespacedtimer",
              }).Record(time.Millisecond)
          
              // Get factory with forkNamespace and add some metrics
              internalFactory := forkFactory.Namespace(metrics.NSOptions{
                  Name: "internal",
              })
              internalFactory.Gauge(metrics.Options{
                  Name: "someinternalgauge",
              }).Update(20)
              internalFactory.Counter(metrics.Options{
                  Name: "someinternalcounter",
              }).Inc(50)
          }
          

In this example, forkFactory creates a fork of the default factory with the namespace internal. Metrics added to the internal namespace will be sent to the fork factory, while metrics added to other namespaces will be sent to the default factory.

Additional Information

For more detailed information about using the jaeger-lib library, please refer to the following resources:


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="metrics/" href="#metrics/">metrics/</a> - This directory contains the metrics implementation for the Jaeger Tracing library.
          
          <a class='local-link directory-link' data-ref="metrics/adapters/" href="#metrics/adapters/">metrics/adapters/</a> - This subdirectory contains the various metric adapters for different backends.
          
          <a class='local-link directory-link' data-ref="metrics/expvar/" href="#metrics/expvar/">metrics/expvar/</a> - This subdirectory contains the implementation for exposing metrics as HTTP endpoints using the expvar package.
          
          <a class='local-link directory-link' data-ref="metrics/fork/" href="#metrics/fork/">metrics/fork/</a> - This subdirectory contains the implementation for forking metrics between processes.
          
          <a class='local-link directory-link' data-ref="metrics/go-kit/" href="#metrics/go-kit/">metrics/go-kit/</a> - This subdirectory contains the Go-Kit integration for the Jaeger Tracing library.
          
          <a class='local-link directory-link' data-ref="metrics/go-kit/influx/" href="#metrics/go-kit/influx/">metrics/go-kit/influx/</a> - This subdirectory contains the InfluxDB implementation for the Go-Kit integration.
          
          <a class='local-link directory-link' data-ref="metrics/metricstest/" href="#metrics/metricstest/">metrics/metricstest/</a> - This subdirectory contains the testing implementation for the metrics.
          
          <a class='local-link directory-link' data-ref="metrics/multi/" href="#metrics/multi/">metrics/multi/</a> - This subdirectory contains the implementation for collecting metrics from multiple sources.
          
          <a class='local-link directory-link' data-ref="metrics/prometheus/" href="#metrics/prometheus/">metrics/prometheus/</a> - This subdirectory contains the Prometheus implementation for the Jaeger Tracing library.
          
          <a class='local-link directory-link' data-ref="metrics/tally/" href="#metrics/tally/">metrics/tally/</a> - This subdirectory contains the implementation for counting metrics using the tally package.