prometheus / client_java

Prometheus instrumentation library for JVM applications
http://prometheus.github.io/client_java/
Apache License 2.0
2.18k stars 798 forks source link

Introduce Sampling in SpanContextSupplier #765

Open fscellos opened 2 years ago

fscellos commented 2 years ago

I work on an PR on micrometer project for prometheus registry to support examplars.

In this PR i have to integrate a new SpanContextSupplier because actual one of prometheus client does not support "isSampled" method from OpenTelemetry. Without this method to sample examplars, some trace id that will be keep for examplar sampling will not be recorded in backend storage (ie. TraceFlags set to "00" to indicate service that come above to not take account of trace for sampling decision).

This issue is to add :

 private Exemplar doSample(double value, Exemplar previous) {
    long timestampMs = clock.currentTimeMillis();
    if ((previous == null || previous.getTimestampMs() == null || timestampMs - previous.getTimestampMs() > minRetentionIntervalMs) && spanContextSupplier.isSampled()) {
      String spanId = spanContextSupplier.getSpanId();
      String traceId = spanContextSupplier.getTraceId();
      if (traceId != null && spanId != null) {
        return new Exemplar(value, timestampMs, SPAN_ID, spanId, TRACE_ID, traceId);
      }
    }
    return null;
  }
fstab commented 2 years ago

Thanks a lot, that totally makes sense. Would you like to open a PR for client_java?

fscellos commented 2 years ago

Hello @fstab. Thank you for your quick answer. I'll do that next week after my holidays.

fscellos commented 2 years ago

Ok @fstab , finally done. Notice that i have to fix also some dependencies on IT Test as they rely on a fix version (0.12.0) of the io.prometheus.simpleclient_tracer version (i simply add project version of theses dependencies in IT test's pom).