Skip to content

Commit

Permalink
feat(metrics): Add traceID exemplar to scrape duration histogram (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson authored Dec 23, 2024
1 parent 6b553c1 commit 9dff72a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions cmd/sagemcom_fast_exporter/exporter_metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/trace"
)

type scrapeObserver struct {
Expand Down Expand Up @@ -40,8 +42,15 @@ func newScrapeObserver() *scrapeObserver {
}
}

func (o *scrapeObserver) Observe(duration time.Duration, success bool) {
o.scrapeDuration.Observe(duration.Seconds())
func (o *scrapeObserver) Observe(ctx context.Context, duration time.Duration, success bool) {
// add traceID as exemplar, if available
traceID := trace.SpanFromContext(ctx).SpanContext().TraceID()
if traceID.IsValid() {
o.scrapeDuration.(prometheus.ExemplarObserver).ObserveWithExemplar(duration.Seconds(),
prometheus.Labels{"traceID": traceID.String()})
} else {
o.scrapeDuration.Observe(duration.Seconds())
}

if success {
o.scrapeSuccess.Set(1)
Expand Down
4 changes: 2 additions & 2 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func initWiFiSSIDMetrics(ns string) wifiSSIDMetrics {
}

type ScrapeObserver interface {
Observe(duration time.Duration, success bool)
Observe(ctx context.Context, duration time.Duration, success bool)
}

func New(ctx context.Context, scraper client.Scraper, scrapeObserver ScrapeObserver) prometheus.Collector {
Expand Down Expand Up @@ -550,7 +550,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
slog.Default().ErrorContext(ctx, "update error", "err", err)
}

c.scrapeObserver.Observe(time.Since(start), err == nil)
c.scrapeObserver.Observe(ctx, time.Since(start), err == nil)
}

func (c *collector) update(ctx context.Context, ch chan<- prometheus.Metric) error {
Expand Down

0 comments on commit 9dff72a

Please sign in to comment.