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

Support for Examplar Metrics #207

Open
kirankumar-grootan opened this issue Jul 19, 2024 · 3 comments
Open

Support for Examplar Metrics #207

kirankumar-grootan opened this issue Jul 19, 2024 · 3 comments

Comments

@kirankumar-grootan
Copy link

kirankumar-grootan commented Jul 19, 2024

It should be a good to have support for examplar metrics

func (ps *FiberPrometheus) RegisterAt(app fiber.Router, url string, handlers ...fiber.Handler) {
	ps.defaultURL = url

	h := append(handlers, adaptor.HTTPHandler(promhttp.HandlerFor(ps.gatherer, promhttp.HandlerOpts{
		EnableOpenMetrics: true,
	})))
	app.Get(ps.defaultURL, h...)
}

func (ps *FiberPrometheus) Middleware(ctx *fiber.Ctx) error {
        ...

	// Update the request duration histogram
	elapsed := float64(time.Since(start).Nanoseconds()) / 1e9

        // Extract the trace ID from the user context to use it as an exemplar if available.
        traceID := trace.SpanContextFromContext(ctx.UserContext()).TraceID()

	// Retrieve the histogram metric for request duration, labeled with status code, method, and path.
	histogram := ps.requestDuration.WithLabelValues(statusCode, method, path)

	// Check if the trace ID is valid to potentially use it as an exemplar.
	if traceID.IsValid() {
	// If the histogram supports exemplars, record the observed duration with the trace ID as an exemplar.
		if histogramExemplar, ok := histogram.(prometheus.ExemplarObserver); ok {
			histogramExemplar.ObserveWithExemplar(elapsed, prometheus.Labels{"traceID": traceID.String()})
		}
	}

	// Record the observed duration in the histogram.
	histogram.Observe(elapsed)

	return err
}

IDK if there will be a performance hit on this. But it's working. Happy to hear better way of handling this

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@kirankumar-grootan
Copy link
Author

kirankumar-grootan commented Sep 25, 2024

Not stale

@gaby
Copy link
Contributor

gaby commented Oct 6, 2024

@kirankumar-grootan Doesn't this require also having opentelemetry as a dependency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants