Skip to main content
Version: 2.8.x(Latest)

Metric - Meter Attributes Example

Code Source: https://github.com/gogf/examples/tree/main/observability/metric/meter_attributes

Description

This example demonstrates how to work with meter-level attributes in GoFrame using OpenTelemetry and Prometheus integration. It shows how to:

  • Configure meter-level attributes
  • Apply attributes across all metrics
  • Combine meter and metric attributes
  • Manage attribute inheritance

Structure

- `go.mod`: The Go module file for dependency management
- `go.sum`: The Go module checksums file
- `main.go`: The main application demonstrating meter attributes

Features

The example showcases the following features:

  1. Meter Attributes

    • Global meter configuration
    • Meter-level attributes
    • Attribute inheritance
  2. Metric Types with Inherited Attributes

    • Counter with combined attributes
    • Observable Counter with combined attributes
    • Attribute precedence rules
  3. Attribute Management

    • Meter attribute configuration
    • Version-based attributes
    • Attribute combination

Requirements

Usage

  1. Run the example:

    go run main.go
  2. Access the metrics:

    # Using curl
    curl http://localhost:8000/metrics

    # Or open in browser
    http://localhost:8000/metrics
  3. Example metrics output:

    # HELP goframe_metric_demo_counter This is a simple demo for Counter usage
    goframe_metric_demo_counter{const_attr_1="1",meter_label_1="1",meter_label_2="2"} 11

    # HELP goframe_metric_demo_observable_counter This is a simple demo for ObservableCounter usage
    goframe_metric_demo_observable_counter{const_attr_2="2",meter_label_1="1",meter_label_2="2"} 10

Implementation Details

The example demonstrates:

  1. Setting up meter attributes
  2. Attribute inheritance rules
  3. Version-based configuration
  4. Attribute combination logic
  5. Proper attribute scoping

Notes

  • Meter attributes apply to all metrics
  • Metric attributes take precedence
  • Consider attribute cardinality
  • Version filtering available
  • Attributes are immutable
  • Default port is 8000
  • Metrics endpoint is at /metrics
  • Consider performance impact of many attributes