Search in sources :

Example 1 with SpanSamplingPolicy

use of com.wavefront.api.agent.SpanSamplingPolicy in project java by wavefrontHQ.

the class SpanSamplerTest method testMultipleSpanSamplingPolicies.

@Test
public void testMultipleSpanSamplingPolicies() {
    long startTime = System.currentTimeMillis();
    String traceId = UUID.randomUUID().toString();
    Span spanToAllow = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("testSpanName").setSource("testsource").setSpanId("testspanid").setTraceId(traceId).build();
    Span spanToAllowWithDebugTag = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("testSpanName").setSource("testsource").setSpanId("testspanid").setTraceId(traceId).setAnnotations(ImmutableList.of(new Annotation("debug", "true"))).build();
    Span spanToDiscard = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("testSpanName").setSource("source").setSpanId("testspanid").setTraceId(traceId).build();
    List<SpanSamplingPolicy> activeSpanSamplingPolicies = ImmutableList.of(new SpanSamplingPolicy("SpanNamePolicy", "{{spanName}}='testSpanName'", 0), new SpanSamplingPolicy("SpanSourcePolicy", "{{sourceName}}='testsource'", 100));
    SpanSampler sampler = new SpanSampler(new DurationSampler(5), () -> activeSpanSamplingPolicies);
    assertTrue(sampler.sample(spanToAllow));
    assertEquals("SpanSourcePolicy", AnnotationUtils.getValue(spanToAllow.getAnnotations(), "_sampledByPolicy"));
    assertTrue(sampler.sample(spanToAllowWithDebugTag));
    assertNull(AnnotationUtils.getValue(spanToAllowWithDebugTag.getAnnotations(), "_sampledByPolicy"));
    assertFalse(sampler.sample(spanToDiscard));
    assertTrue(spanToDiscard.getAnnotations().isEmpty());
}
Also used : Span(wavefront.report.Span) Annotation(wavefront.report.Annotation) SpanSamplingPolicy(com.wavefront.api.agent.SpanSamplingPolicy) DurationSampler(com.wavefront.sdk.entities.tracing.sampling.DurationSampler) Test(org.junit.Test)

Example 2 with SpanSamplingPolicy

use of com.wavefront.api.agent.SpanSamplingPolicy in project java by wavefrontHQ.

the class SpanSampler method sample.

/**
 * Evaluates whether a span should be allowed or discarded, and increment a counter if it should
 * be discarded.
 *
 * @param span      The span to sample.
 * @param discarded The counter to increment if the decision is to discard the span.
 * @return true if the span should be allowed, false otherwise.
 */
public boolean sample(Span span, @Nullable Counter discarded) {
    if (isForceSampled(span)) {
        return true;
    }
    // Policy based span sampling
    List<SpanSamplingPolicy> activeSpanSamplingPolicies = activeSpanSamplingPoliciesSupplier.get();
    if (activeSpanSamplingPolicies != null) {
        int samplingPercent = 0;
        String policyId = null;
        for (SpanSamplingPolicy policy : activeSpanSamplingPolicies) {
            Predicate<Span> spanPredicate = spanPredicateCache.get(policy.getExpression());
            if (spanPredicate != null && spanPredicate.test(span) && policy.getSamplingPercent() > samplingPercent) {
                samplingPercent = policy.getSamplingPercent();
                policyId = policy.getPolicyId();
            }
        }
        if (samplingPercent > 0 && Math.abs(UUID.fromString(span.getTraceId()).getLeastSignificantBits()) % POLICY_BASED_SAMPLING_MOD_FACTOR <= samplingPercent) {
            if (span.getAnnotations() == null) {
                span.setAnnotations(new ArrayList<>());
            }
            span.getAnnotations().add(new Annotation(SPAN_SAMPLING_POLICY_TAG, policyId));
            return true;
        }
    }
    if (delegate.sample(span.getName(), UUID.fromString(span.getTraceId()).getLeastSignificantBits(), span.getDuration())) {
        return true;
    }
    if (discarded != null) {
        discarded.inc();
    }
    return false;
}
Also used : Span(wavefront.report.Span) Annotation(wavefront.report.Annotation) SpanSamplingPolicy(com.wavefront.api.agent.SpanSamplingPolicy)

Example 3 with SpanSamplingPolicy

use of com.wavefront.api.agent.SpanSamplingPolicy in project java by wavefrontHQ.

the class JaegerGrpcCollectorHandlerTest method testJaegerDebugOverride.

@Test
public void testJaegerDebugOverride() throws Exception {
    reset(mockTraceHandler, mockTraceLogsHandler);
    mockTraceHandler.report(Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(9000).setName("HTTP GET /").setSource(DEFAULT_SOURCE).setSpanId("00000000-0000-0000-0000-00000023cace").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setAnnotations(ImmutableList.of(new Annotation("ip", "10.0.0.1"), new Annotation("service", "frontend"), new Annotation("debug", "true"), new Annotation("application", "Jaeger"), new Annotation("cluster", "none"), new Annotation("shard", "none"), new Annotation("parent", "00000000-0000-0000-0000-00000012d687"))).build());
    expectLastCall();
    mockTraceHandler.report(Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4000).setName("HTTP GET").setSource(DEFAULT_SOURCE).setSpanId("00000000-0000-0000-0000-00000012d687").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setAnnotations(ImmutableList.of(new Annotation("ip", "10.0.0.1"), new Annotation("service", "frontend"), new Annotation("sampling.priority", "0.3"), new Annotation("application", "Jaeger"), new Annotation("cluster", "none"), new Annotation("shard", "none"), new Annotation("_sampledByPolicy", "test"))).build());
    expectLastCall();
    replay(mockTraceHandler, mockTraceLogsHandler);
    JaegerGrpcCollectorHandler handler = new JaegerGrpcCollectorHandler("9876", mockTraceHandler, mockTraceLogsHandler, null, () -> false, () -> false, null, new SpanSampler(new DurationSampler(10 * 1000), () -> ImmutableList.of(new SpanSamplingPolicy("test", "{{sampling.priority}}='0.3'", 100))), null, null);
    Model.KeyValue ipTag = Model.KeyValue.newBuilder().setKey("ip").setVStr("10.0.0.1").setVType(Model.ValueType.STRING).build();
    Model.KeyValue debugTag = Model.KeyValue.newBuilder().setKey("debug").setVStr("true").setVType(Model.ValueType.STRING).build();
    Model.KeyValue samplePriorityTag = Model.KeyValue.newBuilder().setKey("sampling.priority").setVFloat64(0.3).setVType(Model.ValueType.FLOAT64).build();
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2);
    buffer.putLong(1234567890L);
    buffer.putLong(1234567890123L);
    ByteString traceId = ByteString.copyFrom(buffer.array());
    buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.putLong(2345678L);
    ByteString span1Id = ByteString.copyFrom(buffer.array());
    buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.putLong(1234567L);
    ByteString span2Id = ByteString.copyFrom(buffer.array());
    Model.Span span1 = Model.Span.newBuilder().setTraceId(traceId).setSpanId(span1Id).setDuration(Duration.newBuilder().setSeconds(9L).build()).setOperationName("HTTP GET /").addTags(debugTag).addReferences(Model.SpanRef.newBuilder().setRefType(Model.SpanRefType.CHILD_OF).setSpanId(span2Id).setTraceId(traceId).build()).setStartTime(fromMillis(startTime)).build();
    Model.Span span2 = Model.Span.newBuilder().setTraceId(traceId).setSpanId(span2Id).setDuration(Duration.newBuilder().setSeconds(4L).build()).setOperationName("HTTP GET").addTags(samplePriorityTag).setStartTime(fromMillis(startTime)).build();
    Model.Batch testBatch = Model.Batch.newBuilder().setProcess(Model.Process.newBuilder().setServiceName("frontend").addTags(ipTag).build()).addAllSpans(ImmutableList.of(span1, span2)).build();
    Collector.PostSpansRequest batches = Collector.PostSpansRequest.newBuilder().setBatch(testBatch).build();
    handler.postSpans(batches, emptyStreamObserver);
    verify(mockTraceHandler, mockTraceLogsHandler);
}
Also used : ByteString(com.google.protobuf.ByteString) SpanSampler(com.wavefront.agent.sampler.SpanSampler) ByteBuffer(java.nio.ByteBuffer) Annotation(wavefront.report.Annotation) DurationSampler(com.wavefront.sdk.entities.tracing.sampling.DurationSampler) SpanSamplingPolicy(com.wavefront.api.agent.SpanSamplingPolicy) Model(io.opentelemetry.exporters.jaeger.proto.api_v2.Model) Collector(io.opentelemetry.exporters.jaeger.proto.api_v2.Collector) Test(org.junit.Test)

Example 4 with SpanSamplingPolicy

use of com.wavefront.api.agent.SpanSamplingPolicy in project java by wavefrontHQ.

the class JaegerTChannelCollectorHandlerTest method testJaegerDebugOverride.

@Test
public void testJaegerDebugOverride() throws Exception {
    reset(mockTraceHandler, mockTraceLogsHandler);
    Span expectedSpan1 = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(9).setName("HTTP GET /").setSource(DEFAULT_SOURCE).setSpanId("00000000-0000-0000-0000-00000023cace").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setAnnotations(ImmutableList.of(new Annotation("ip", "10.0.0.1"), new Annotation("jaegerSpanId", "23cace"), new Annotation("jaegerTraceId", "499602d20000011f71fb04cb"), new Annotation("service", "frontend"), new Annotation("parent", "00000000-0000-0000-0000-00000012d687"), new Annotation("debug", "true"), new Annotation("application", "Jaeger"), new Annotation("cluster", "none"), new Annotation("shard", "none"), new Annotation("_spanLogs", "true"))).build();
    mockTraceHandler.report(expectedSpan1);
    expectLastCall();
    mockTraceLogsHandler.report(SpanLogs.newBuilder().setCustomer("default").setSpanId("00000000-0000-0000-0000-00000023cace").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setLogs(ImmutableList.of(SpanLog.newBuilder().setTimestamp(startTime * 1000).setFields(ImmutableMap.of("event", "error", "exception", "NullPointerException")).build())).build());
    expectLastCall();
    Span expectedSpan2 = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("HTTP GET").setSource(DEFAULT_SOURCE).setSpanId("00000000-0000-0000-0000-00000012d687").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setAnnotations(ImmutableList.of(new Annotation("ip", "10.0.0.1"), new Annotation("jaegerSpanId", "12d687"), new Annotation("jaegerTraceId", "499602d20000011f71fb04cb"), new Annotation("service", "frontend"), new Annotation("sampling.priority", "0.3"), new Annotation("application", "Jaeger"), new Annotation("cluster", "none"), new Annotation("shard", "none"), new Annotation("_spanLogs", "true"), new Annotation("_sampledByPolicy", "test"))).build();
    mockTraceHandler.report(expectedSpan2);
    expectLastCall();
    mockTraceLogsHandler.report(SpanLogs.newBuilder().setCustomer("default").setSpanId("00000000-0000-0000-0000-00000012d687").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setLogs(ImmutableList.of(SpanLog.newBuilder().setTimestamp(startTime * 1000).setFields(ImmutableMap.of("event", "error", "exception", "NullPointerException")).build())).build());
    expectLastCall();
    replay(mockTraceHandler, mockTraceLogsHandler);
    JaegerTChannelCollectorHandler handler = new JaegerTChannelCollectorHandler("9876", mockTraceHandler, mockTraceLogsHandler, null, () -> false, () -> false, null, new SpanSampler(new DurationSampler(10), () -> ImmutableList.of(new SpanSamplingPolicy("test", "{{sampling.priority}}='0.3'", 100))), null, null);
    Tag ipTag = new Tag("ip", TagType.STRING);
    ipTag.setVStr("10.0.0.1");
    Tag debugTag = new Tag("debug", TagType.STRING);
    debugTag.setVStr("true");
    io.jaegertracing.thriftjava.Span span1 = new io.jaegertracing.thriftjava.Span(1234567890123L, 1234567890L, 2345678L, 1234567L, "HTTP GET /", 1, startTime * 1000, 9 * 1000);
    span1.setTags(ImmutableList.of(debugTag));
    Tag samplePriorityTag = new Tag("sampling.priority", TagType.DOUBLE);
    samplePriorityTag.setVDouble(0.3);
    io.jaegertracing.thriftjava.Span span2 = new io.jaegertracing.thriftjava.Span(1234567890123L, 1234567890L, 1234567L, 0L, "HTTP GET", 1, startTime * 1000, 4 * 1000);
    span2.setTags(ImmutableList.of(samplePriorityTag));
    Tag tag1 = new Tag("event", TagType.STRING);
    tag1.setVStr("error");
    Tag tag2 = new Tag("exception", TagType.STRING);
    tag2.setVStr("NullPointerException");
    span1.setLogs(ImmutableList.of(new Log(startTime * 1000, ImmutableList.of(tag1, tag2))));
    span2.setLogs(ImmutableList.of(new Log(startTime * 1000, ImmutableList.of(tag1, tag2))));
    Batch testBatch = new Batch();
    testBatch.process = new Process();
    testBatch.process.serviceName = "frontend";
    testBatch.process.setTags(ImmutableList.of(ipTag));
    testBatch.setSpans(ImmutableList.of(span1, span2));
    Collector.submitBatches_args batches = new Collector.submitBatches_args();
    batches.addToBatches(testBatch);
    ThriftRequest<Collector.submitBatches_args> request = new ThriftRequest.Builder<Collector.submitBatches_args>("jaeger-collector", "Collector::submitBatches").setBody(batches).build();
    handler.handleImpl(request);
    verify(mockTraceHandler, mockTraceLogsHandler);
}
Also used : SpanLog(wavefront.report.SpanLog) Log(io.jaegertracing.thriftjava.Log) SpanSampler(com.wavefront.agent.sampler.SpanSampler) Process(io.jaegertracing.thriftjava.Process) Span(wavefront.report.Span) Annotation(wavefront.report.Annotation) DurationSampler(com.wavefront.sdk.entities.tracing.sampling.DurationSampler) SpanSamplingPolicy(com.wavefront.api.agent.SpanSamplingPolicy) Batch(io.jaegertracing.thriftjava.Batch) Collector(io.jaegertracing.thriftjava.Collector) Tag(io.jaegertracing.thriftjava.Tag) Test(org.junit.Test)

Example 5 with SpanSamplingPolicy

use of com.wavefront.api.agent.SpanSamplingPolicy in project java by wavefrontHQ.

the class SpanSamplerTest method testSpanSamplingPolicySamplingPercent.

@Test
public void testSpanSamplingPolicySamplingPercent() {
    long startTime = System.currentTimeMillis();
    Span span = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("testSpanName").setSource("testsource").setSpanId("testspanid").setTraceId(UUID.randomUUID().toString()).build();
    List<SpanSamplingPolicy> activeSpanSamplingPolicies = new ArrayList<>();
    activeSpanSamplingPolicies.add(new SpanSamplingPolicy("SpanNamePolicy", "{{spanName}}='testSpanName'", 50));
    SpanSampler sampler = new SpanSampler(new DurationSampler(5), () -> activeSpanSamplingPolicies);
    int sampledSpans = 0;
    for (int i = 0; i < 1000; i++) {
        if (sampler.sample(Span.newBuilder(span).setTraceId(UUID.randomUUID().toString()).build())) {
            sampledSpans++;
        }
    }
    assertTrue(sampledSpans < 1000 && sampledSpans > 0);
    activeSpanSamplingPolicies.clear();
    activeSpanSamplingPolicies.add(new SpanSamplingPolicy("SpanNamePolicy", "{{spanName" + "}}='testSpanName'", 100));
    sampledSpans = 0;
    for (int i = 0; i < 1000; i++) {
        if (sampler.sample(Span.newBuilder(span).setTraceId(UUID.randomUUID().toString()).build())) {
            sampledSpans++;
        }
    }
    assertEquals(1000, sampledSpans);
    activeSpanSamplingPolicies.clear();
    activeSpanSamplingPolicies.add(new SpanSamplingPolicy("SpanNamePolicy", "{{spanName" + "}}='testSpanName'", 0));
    sampledSpans = 0;
    for (int i = 0; i < 1000; i++) {
        if (sampler.sample(Span.newBuilder(span).setTraceId(UUID.randomUUID().toString()).build())) {
            sampledSpans++;
        }
    }
    assertEquals(0, sampledSpans);
}
Also used : ArrayList(java.util.ArrayList) Span(wavefront.report.Span) SpanSamplingPolicy(com.wavefront.api.agent.SpanSamplingPolicy) DurationSampler(com.wavefront.sdk.entities.tracing.sampling.DurationSampler) Test(org.junit.Test)

Aggregations

SpanSamplingPolicy (com.wavefront.api.agent.SpanSamplingPolicy)5 DurationSampler (com.wavefront.sdk.entities.tracing.sampling.DurationSampler)4 Test (org.junit.Test)4 Annotation (wavefront.report.Annotation)4 Span (wavefront.report.Span)4 SpanSampler (com.wavefront.agent.sampler.SpanSampler)2 ByteString (com.google.protobuf.ByteString)1 Batch (io.jaegertracing.thriftjava.Batch)1 Collector (io.jaegertracing.thriftjava.Collector)1 Log (io.jaegertracing.thriftjava.Log)1 Process (io.jaegertracing.thriftjava.Process)1 Tag (io.jaegertracing.thriftjava.Tag)1 Collector (io.opentelemetry.exporters.jaeger.proto.api_v2.Collector)1 Model (io.opentelemetry.exporters.jaeger.proto.api_v2.Model)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 SpanLog (wavefront.report.SpanLog)1