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:
-
Meter Attributes
- Global meter configuration
- Meter-level attributes
- Attribute inheritance
-
Metric Types with Inherited Attributes
- Counter with combined attributes
- Observable Counter with combined attributes
- Attribute precedence rules
-
Attribute Management
- Meter attribute configuration
- Version-based attributes
- Attribute combination
Requirements
- Go 1.22 or higher
- Git
- GoFrame
- GoFrame OpenTelemetry Metric
Usage
-
Run the example:
go run main.go
-
Access the metrics:
# Using curl
curl http://localhost:8000/metrics
# Or open in browser
http://localhost:8000/metrics -
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:
- Setting up meter attributes
- Attribute inheritance rules
- Version-based configuration
- Attribute combination logic
- 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