Namespaces

Namespaces are used to group metrics, providing a logical organization and enabling the attachment of constant labels (like version and commit) to all metrics within a namespace.

Example

// Namespace is a convenient way to group metrics.
          // The metrics "requests" and "errors" will all be labeled with namespace="myapp"
          ns := NewNamespace("myapp")
          requests := ns.NewCounter("requests")
          errors := ns.NewCounter("errors")
          

Usage

Namespaces are created using the NewNamespace function.

Source: namespace.go

The following example shows how to create a namespace called “myapp”.

// Namespace is a convenient way to group metrics.
          // The metrics "requests" and "errors" will all be labeled with namespace="myapp"
          ns := NewNamespace("myapp")
          requests := ns.NewCounter("requests")
          errors := ns.NewCounter("errors")
          

Source: namespace.go

Namespaces can be used to group metrics that belong to the same application, service, or component.

Source: namespace.go

Options

The NewNamespace function also accepts an optional opts argument.

Source: namespace.go

The opts argument can be used to specify additional options, such as:

  • Labels: A map of key-value pairs that will be attached to all metrics within the namespace.
  • Prefix: A string that will be prepended to all metric names within the namespace.

Source: namespace.go

Examples

Example: Adding Labels

// Namespace is a convenient way to group metrics.
          // The metrics "requests" and "errors" will all be labeled with namespace="myapp", version="1.0"
          ns := NewNamespace("myapp", WithLabels(map[string]string{"version": "1.0"}))
          requests := ns.NewCounter("requests")
          errors := ns.NewCounter("errors")
          

Source: namespace.go

Example: Adding Prefix

// Namespace is a convenient way to group metrics.
          // The metrics "requests" and "errors" will all be labeled with namespace="myapp" and have names "myapp.requests" and "myapp.errors".
          ns := NewNamespace("myapp", WithPrefix("myapp"))
          requests := ns.NewCounter("requests")
          errors := ns.NewCounter("errors")
          

Source: namespace.go

Notes

Namespaces can be nested to create a hierarchical structure for metrics.

Source: namespace.go

Namespaces can be used to group metrics for different purposes, such as:

  • Application: Group metrics for a specific application.
  • Service: Group metrics for a specific service within an application.
  • Component: Group metrics for a specific component within a service.

Source: namespace.go

Namespaces are a powerful tool for organizing and labeling metrics.

Source: namespace.go