Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default labels on exported metrics for Prometheus #3405

Closed
lucasoares opened this issue Oct 27, 2022 · 7 comments
Closed

Default labels on exported metrics for Prometheus #3405

lucasoares opened this issue Oct 27, 2022 · 7 comments
Labels
bug Something isn't working pkg:exporter:prometheus Related to the Prometheus exporter package
Milestone

Comments

@lucasoares
Copy link

Hello.

I was using metrics SDK version 0.30.0 and I'm now updating my code with the 0.33.0 to keep up with latest pre-GA version.

When I was using the version 0.30.0 all the exported metrics to (Prometheus Exporter) had my resource labels from the OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables plus SDK versions.

After the update these labels are no longer returned and a target_info metric is now exposed (without SDK labels) which I can disable with WithoutTargetInfo, but I can't manage to make my metrics returning default labels again...

Before:

foo_bar_housekeeper_task_latency_bucket{service_name="foo_bar",service_version="0.6.4",task="custom_metric",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="1.7.0",le="0"} 0

After:

foo_bar_housekeeper_task_latency_bucket{task="custom_metric",le="0"} 0
target_info{service_name="foo_bar",service_version="0.6.4"} 0

Thanks.

@lucasoares lucasoares added the bug Something isn't working label Oct 27, 2022
@MrAlias
Copy link
Contributor

MrAlias commented Oct 28, 2022

This is how OpenTelemetry as a whole has decided to export this data. What the latest version exports is compliant with the OpenTelemetry specification. The previous versions were not. Changing to the old format is not something we are able to do.

I'm pretty sure the idea is if you want you can use Prometheus rewrite rules to annotate metrics with the data as you want.

@lucasoares
Copy link
Author

This is how OpenTelemetry as a whole has decided to export this data. What the latest version exports is compliant with the OpenTelemetry specification. The previous versions were not. Changing to the old format is not something we are able to do.

I'm pretty sure the idea is if you want you can use Prometheus rewrite rules to annotate metrics with the data as you want.

There is any documentation available on how to do it? To make prometheus export to always set these metrics?

@MrAlias
Copy link
Contributor

MrAlias commented Nov 1, 2022

I don't know of any. Someone with more familiarity of Prometheus would need to speak to this.

@malud
Copy link

malud commented Dec 2, 2022

Hi,
since we use this pkg @avenga/couper and have to ensure backward compatibility we came up to a Gatherer Wrapper.

This works with v0.33 and those labels are applied to system metrics too (go, process):
(maybe os.getenv(OTEL_*) by hand)

import (
  prom "github.com/prometheus/client_golang/prometheus"
  dto "github.com/prometheus/client_model/go"
  otelprom "go.opentelemetry.io/otel/exporters/prometheus"
)

func newPromExporter() (*otelprom.Exporter, *WrappedRegistry, error) {
	const yourService = "a-name"
	const yourServiceVersion = "v1.x.x"

	strPtr := func(s string) *string { return &s }

	registry := NewWrappedRegistry(prom.NewRegistry(), &dto.LabelPair{
		Name:  strPtr("service_name"),
		Value: strPtr(yourService),
	}, &dto.LabelPair{
		Name:  strPtr("service_version"),
		Value: strPtr(yourServiceVersion),
	})

	registry.MustRegister(collectors.NewGoCollector())
	registry.MustRegister(collectors.NewProcessCollector(
		collectors.ProcessCollectorOpts{
			Namespace: "xxx", // name prefix
		},
	))

	promExporter, err := otelprom.New(otelprom.WithRegisterer(registry))

	return promExporter, registry, err
}

// registry.go

import (
	prom "github.com/prometheus/client_golang/prometheus"
	dto "github.com/prometheus/client_model/go"
)

var (
	_ prom.Gatherer   = &WrappedRegistry{}
	_ prom.Registerer = &WrappedRegistry{}
)

type WrappedRegistry struct {
	labels       []*dto.LabelPair
	promRegistry *prom.Registry
}

func NewWrappedRegistry(promRegistry *prom.Registry, labels ...*dto.LabelPair) *WrappedRegistry {
	return &WrappedRegistry{
		labels:       labels,
		promRegistry: promRegistry,
	}
}

func (wr *WrappedRegistry) Gather() ([]*dto.MetricFamily, error) {
	families, err := wr.promRegistry.Gather()
	if err != nil {
		return nil, err
	}

	// working on ptrs
	for _, f := range families {
		for _, m := range f.Metric {
			m.Label = append(m.Label, wr.labels...)
		}
	}
	return families, nil
}

func (wr *WrappedRegistry) Register(collector prom.Collector) error {
	return wr.promRegistry.Register(collector)
}

func (wr *WrappedRegistry) MustRegister(collector ...prom.Collector) {
	wr.promRegistry.MustRegister(collector...)
}

func (wr *WrappedRegistry) Unregister(collector prom.Collector) bool {
	return wr.promRegistry.Unregister(collector)
}

related commit: coupergateway/couper@17ccc70

Hope that helps @lucasoares

@lucasoares
Copy link
Author

lucasoares commented Dec 16, 2022

Thanks, I will test it...

edit: I will use your solution for now. Thanks!

@dashpole
Copy link
Contributor

Regarding #3405 (comment), the query to join a metric with target_info should look something like:

my_metric * on(instance, job) group_left(service_name, service_version, etc) target_info

It should be possible to use recording rules to do this at ingest time, if using a Prometheus server to scrape.

@dashpole
Copy link
Contributor

The exporter currently follows the opentelemetry specification for prometheus exporters. If the current behavior needs to be changed, we should open an issue in the specification repo.

The solutions discussed above are probably the best workarounds for now.

I propose we close this issue.

@dashpole dashpole moved this from Todo to In Progress in Go: Metric Prometheus Exporter (GA) Oct 20, 2023
@XSAM XSAM added this to the untracked milestone Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:exporter:prometheus Related to the Prometheus exporter package
Development

No branches or pull requests

5 participants