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

prometheus: unit is not appended to name #4372

Closed
gouthamve opened this issue Jul 26, 2023 · 1 comment · Fixed by #4374
Closed

prometheus: unit is not appended to name #4372

gouthamve opened this issue Jul 26, 2023 · 1 comment · Fixed by #4374
Assignees
Labels
bug Something isn't working

Comments

@gouthamve
Copy link
Member

Description

The Prometheus compatibility spec specifies:

The resulting unit SHOULD be added to the metric as OpenMetrics UNIT metadata and as a suffix to the metric name unless the metric name already contains the unit, or the unit MUST be omitted. The unit suffix comes before any type-specific suffixes.

But this doesn't happen.

counter, err := meter.Float64Counter("foo", api.WithDescription("a simple counter"), api.WithUnit("s")) results in

# HELP foo_total a simple counter
# TYPE foo_total counter
foo_total{A="B",C="D",otel_scope_name="github.com/open-telemetry/opentelemetry-go/example/prometheus",otel_scope_version=""} 5

Environment

  • OS: MacOS
  • Architecture: darwin/arm64
  • Go Version: 1.20.6
  • opentelemetry-go version: 1.16.0

Steps To Reproduce

The exporter embeds a default OpenTelemetry Reader and
	// implements prometheus.Collector, allowing it to be used as
	// both a Reader and Collector.
	exporter, err := prometheus.New()
	if err != nil {
		log.Fatal(err)
	}
	provider := metric.NewMeterProvider(metric.WithReader(exporter))
	meter := provider.Meter("github.com/open-telemetry/opentelemetry-go/example/prometheus")

	// Start the prometheus HTTP server and pass the exporter Collector to it
	go serveMetrics()

	opt := api.WithAttributes(
		attribute.Key("A").String("B"),
		attribute.Key("C").String("D"),
	)

	// This is the equivalent of prometheus.NewCounterVec
	counter, err := meter.Float64Counter("foo", api.WithDescription("a simple counter"), api.WithUnit("s"))
	if err != nil {
		log.Fatal(err)
	}
	counter.Add(ctx, 5, opt)

	ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
	<-ctx.Done()
}

func serveMetrics() {
	log.Printf("serving metrics at localhost:2223/metrics")
	http.Handle("/metrics", promhttp.Handler())
	err := http.ListenAndServe(":2223", nil)
	if err != nil {
		fmt.Printf("error serving http: %v", err)
		return
	}
}
@dashpole
Copy link
Contributor

We do support units, but decided originally to only support the units defined by the Go SDK. I think we've since removed our constants for units, so it makes sense to expand what the prom exporter supports.

@MrAlias MrAlias added this to the v1.17.0/v0.40.0 milestone Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants