Skip to content

Commit e1dc5ce

Browse files
authored
fix: make the exporter include resource attributes as metric labels (#38)
1 parent d8ea67f commit e1dc5ce

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

internal/metrics/metrics.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ var (
3030

3131
// Init initializes the OpenTelemetry metrics with Prometheus exporter
3232
func Init(environment string) error {
33-
exporter, err := prometheus.New()
33+
// Create a resource with the environment attribute
34+
res := resource.NewSchemaless(attribute.String("env", environment))
35+
36+
// Configure Prometheus exporter to include resource attributes as metric labels, it doesn't do it by default
37+
exporter, err := prometheus.New(
38+
prometheus.WithResourceAsConstantLabels(
39+
attribute.NewAllowKeysFilter("env"), // Only include "env" as a label
40+
),
41+
)
3442
if err != nil {
3543
return fmt.Errorf("failed to create prometheus exporter: %w", err)
3644
}
3745

38-
// Create a resource with the environment attribute
39-
res := resource.NewSchemaless(attribute.String("env", environment))
40-
4146
// Create a MeterProvider with the Prometheus exporter and resource
4247
provider := sdkmetric.NewMeterProvider(
4348
sdkmetric.WithReader(exporter),

0 commit comments

Comments
 (0)