client_java icon indicating copy to clipboard operation
client_java copied to clipboard

Introduce Sampling in SpanContextSupplier

Open fscellos opened this issue 3 years ago • 3 comments

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 :

  • A new method boolean isSampled() to SpanContextSupplier interface
  • An implementation of this methode in OpenTelemetrySpanContextSupplier (that send back Span.current().getSpanContext().isSampled())
  • Use of this methode in io.prometheus.client.exemplar.DefaultExemplarSampler in method "doSample" with something like this :
 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;
  }

fscellos avatar Feb 16 '22 09:02 fscellos

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

fstab avatar Feb 16 '22 11:02 fstab

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

fscellos avatar Feb 16 '22 11:02 fscellos

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).

fscellos avatar Feb 16 '22 18:02 fscellos