Metric - Global Attributes Example
Code Source: https://github.com/gogf/examples/tree/main/observability/metric/global_attributes
Description
This example demonstrates how to work with global metric attributes in GoFrame
using OpenTelemetry
and Prometheus
integration. It shows how to:
- Set and manage global attributes
- Apply global attributes across multiple metrics
- Configure attribute scope and patterns
- Combine global attributes with local ones
Structure
- `go.mod`: The Go module file for dependency management
- `go.sum`: The Go module checksums file
- `main.go`: The main application demonstrating global metric attributes
Features
The example showcases the following features:
-
Global Attributes
- Global attribute configuration
- Attribute inheritance
- Pattern-based application
-
Metric Types with Global Attributes
- Counter with global attributes
- Observable Counter with global attributes
- Local attribute combination
-
Attribute Management
- Global attribute scoping
- Version-based filtering
- Pattern-based filtering
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",global_attr_1="1"} 11
# HELP goframe_metric_demo_observable_counter This is a simple demo for ObservableCounter usage
goframe_metric_demo_observable_counter{const_attr_2="2",global_attr_1="1"} 10
Implementation Details
The example demonstrates:
- Setting up global attributes
- Configuring attribute patterns
- Version-based attribute filtering
- Attribute inheritance and precedence
- Global attribute scoping
Notes
- Global attributes apply to all matching metrics
- Local attributes take precedence over global ones
- Pattern matching controls attribute application
- Version filtering enables targeted attribute sets
- Consider attribute cardinality impact
- Default port is 8000
- Metrics endpoint is at /metrics