Search in sources :

Example 21 with Span

use of wavefront.report.Span in project java by wavefrontHQ.

the class ZipkinPortUnificationHandlerTest method testZipkinPreprocessedDerivedMetrics.

/**
 * Test for derived metrics emitted from Zipkin trace listeners. Derived metrics should report
 * tag values post applying preprocessing rules to the span.
 */
@Test
public void testZipkinPreprocessedDerivedMetrics() throws Exception {
    Supplier<ReportableEntityPreprocessor> preprocessorSupplier = () -> {
        ReportableEntityPreprocessor preprocessor = new ReportableEntityPreprocessor();
        PreprocessorRuleMetrics preprocessorRuleMetrics = new PreprocessorRuleMetrics(null, null, null);
        preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(APPLICATION_TAG_KEY, "^Zipkin.*", PREPROCESSED_APPLICATION_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
        preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(SERVICE_TAG_KEY, "^test.*", PREPROCESSED_SERVICE_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
        preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer("sourceName", "^zipkin.*", PREPROCESSED_SOURCE_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
        preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(CLUSTER_TAG_KEY, "^none.*", PREPROCESSED_CLUSTER_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
        preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(SHARD_TAG_KEY, "^none.*", PREPROCESSED_SHARD_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
        return preprocessor;
    };
    ZipkinPortUnificationHandler handler = new ZipkinPortUnificationHandler("9411", new NoopHealthCheckManager(), mockTraceHandler, mockTraceSpanLogsHandler, mockWavefrontSender, () -> false, () -> false, preprocessorSupplier, new SpanSampler(new RateSampler(1.0D), () -> null), null, null);
    Endpoint localEndpoint1 = Endpoint.newBuilder().serviceName("testService").ip("10.0.0.1").build();
    zipkin2.Span spanServer1 = zipkin2.Span.newBuilder().traceId("2822889fe47043bd").id("2822889fe47043bd").kind(zipkin2.Span.Kind.SERVER).name("getservice").timestamp(startTime * 1000).duration(1234 * 1000).localEndpoint(localEndpoint1).build();
    List<zipkin2.Span> zipkinSpanList = ImmutableList.of(spanServer1);
    // Reset mock
    reset(mockTraceHandler, mockWavefrontSender);
    // Set Expectation
    mockTraceHandler.report(Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(1234).setName("getservice").setSource(PREPROCESSED_SOURCE_VALUE).setSpanId("00000000-0000-0000-2822-889fe47043bd").setTraceId("00000000-0000-0000-2822-889fe47043bd").setAnnotations(ImmutableList.of(new Annotation("zipkinSpanId", "2822889fe47043bd"), new Annotation("zipkinTraceId", "2822889fe47043bd"), new Annotation("span.kind", "server"), new Annotation("service", PREPROCESSED_SERVICE_TAG_VALUE), new Annotation("application", PREPROCESSED_APPLICATION_TAG_VALUE), new Annotation("cluster", PREPROCESSED_CLUSTER_TAG_VALUE), new Annotation("shard", PREPROCESSED_SHARD_TAG_VALUE), new Annotation("ipv4", "10.0.0.1"))).build());
    expectLastCall();
    Capture<HashMap<String, String>> tagsCapture = EasyMock.newCapture();
    mockWavefrontSender.sendMetric(eq(HEART_BEAT_METRIC), eq(1.0), anyLong(), eq(PREPROCESSED_SOURCE_VALUE), EasyMock.capture(tagsCapture));
    expectLastCall().anyTimes();
    replay(mockTraceHandler, mockWavefrontSender);
    ChannelHandlerContext mockCtx = createNiceMock(ChannelHandlerContext.class);
    doMockLifecycle(mockCtx);
    ByteBuf content = Unpooled.copiedBuffer(SpanBytesEncoder.JSON_V2.encodeList(zipkinSpanList));
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost:9411/api/v2/spans", content, true);
    handler.handleHttpMessage(mockCtx, httpRequest);
    handler.run();
    verifyWithTimeout(500, mockTraceHandler, mockWavefrontSender);
    HashMap<String, String> tagsReturned = tagsCapture.getValue();
    assertEquals(PREPROCESSED_APPLICATION_TAG_VALUE, tagsReturned.get(APPLICATION_TAG_KEY));
    assertEquals(PREPROCESSED_SERVICE_TAG_VALUE, tagsReturned.get(SERVICE_TAG_KEY));
    assertEquals(PREPROCESSED_CLUSTER_TAG_VALUE, tagsReturned.get(CLUSTER_TAG_KEY));
    assertEquals(PREPROCESSED_SHARD_TAG_VALUE, tagsReturned.get(SHARD_TAG_KEY));
}
Also used : ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) RateSampler(com.wavefront.sdk.entities.tracing.sampling.RateSampler) HashMap(java.util.HashMap) SpanSampler(com.wavefront.agent.sampler.SpanSampler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Span(wavefront.report.Span) Annotation(wavefront.report.Annotation) PreprocessorRuleMetrics(com.wavefront.agent.preprocessor.PreprocessorRuleMetrics) Endpoint(zipkin2.Endpoint) NoopHealthCheckManager(com.wavefront.agent.channel.NoopHealthCheckManager) SpanReplaceRegexTransformer(com.wavefront.agent.preprocessor.SpanReplaceRegexTransformer) Test(org.junit.Test)

Example 22 with Span

use of wavefront.report.Span in project java by wavefrontHQ.

the class SpanSamplerTest method testAlwaysSampleDebug.

@Test
public void testAlwaysSampleDebug() {
    long startTime = System.currentTimeMillis();
    String traceId = UUID.randomUUID().toString();
    Span span = Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4).setName("testSpanName").setSource("testsource").setSpanId("testspanid").setTraceId(traceId).setAnnotations(ImmutableList.of(new Annotation("debug", "true"))).build();
    SpanSampler sampler = new SpanSampler(new DurationSampler(5), () -> null);
    assertTrue(sampler.sample(span));
}
Also used : Span(wavefront.report.Span) Annotation(wavefront.report.Annotation) DurationSampler(com.wavefront.sdk.entities.tracing.sampling.DurationSampler) Test(org.junit.Test)

Example 23 with Span

use of wavefront.report.Span 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 24 with Span

use of wavefront.report.Span in project java by wavefrontHQ.

the class WavefrontPortUnificationHandler method processLine.

/**
 * @param ctx      ChannelHandler context (to retrieve remote client's IP in case of errors)
 * @param message  line being processed
 */
@Override
protected void processLine(final ChannelHandlerContext ctx, @Nonnull String message, @Nullable DataFormat format) {
    DataFormat dataFormat = format == null ? DataFormat.autodetect(message) : format;
    switch(dataFormat) {
        case SOURCE_TAG:
            ReportableEntityHandler<ReportSourceTag, SourceTag> sourceTagHandler = sourceTagHandlerSupplier.get();
            if (sourceTagHandler == null || sourceTagDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "sourceTag-formatted data!");
                return;
            }
            List<ReportSourceTag> output = new ArrayList<>(1);
            try {
                sourceTagDecoder.decode(message, output, "dummy");
                for (ReportSourceTag tag : output) {
                    sourceTagHandler.report(tag);
                }
            } catch (Exception e) {
                sourceTagHandler.reject(message, formatErrorMessage("WF-300 Cannot parse sourceTag: \"" + message + "\"", e, ctx));
            }
            return;
        case EVENT:
            ReportableEntityHandler<ReportEvent, ReportEvent> eventHandler = eventHandlerSupplier.get();
            if (eventHandler == null || eventDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept event data!");
                return;
            }
            List<ReportEvent> events = new ArrayList<>(1);
            try {
                eventDecoder.decode(message, events, "dummy");
                for (ReportEvent event : events) {
                    eventHandler.report(event);
                }
            } catch (Exception e) {
                eventHandler.reject(message, formatErrorMessage("WF-300 Cannot parse event: \"" + message + "\"", e, ctx));
            }
            return;
        case SPAN:
            ReportableEntityHandler<Span, String> spanHandler = spanHandlerSupplier.get();
            if (spanHandler == null || spanDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "tracing data (spans)!");
                return;
            }
            message = annotator == null ? message : annotator.apply(ctx, message);
            receivedSpansTotal.get().inc();
            preprocessAndHandleSpan(message, spanDecoder, spanHandler, spanHandler::report, preprocessorSupplier, ctx, span -> sampler.sample(span, discardedSpansBySampler.get()));
            return;
        case SPAN_LOG:
            if (isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, discardedSpanLogs.get()))
                return;
            ReportableEntityHandler<SpanLogs, String> spanLogsHandler = spanLogsHandlerSupplier.get();
            if (spanLogsHandler == null || spanLogsDecoder == null || spanDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "tracing data (span logs)!");
                return;
            }
            handleSpanLogs(message, spanLogsDecoder, spanDecoder, spanLogsHandler, preprocessorSupplier, ctx, span -> sampler.sample(span, discardedSpanLogsBySampler.get()));
            return;
        case HISTOGRAM:
            if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get()))
                return;
            ReportableEntityHandler<ReportPoint, String> histogramHandler = histogramHandlerSupplier.get();
            if (histogramHandler == null || histogramDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "histogram-formatted data!");
                return;
            }
            message = annotator == null ? message : annotator.apply(ctx, message);
            preprocessAndHandlePoint(message, histogramDecoder, histogramHandler, preprocessorSupplier, ctx, "histogram");
            return;
        default:
            message = annotator == null ? message : annotator.apply(ctx, message);
            preprocessAndHandlePoint(message, wavefrontDecoder, wavefrontHandler, preprocessorSupplier, ctx, "metric");
    }
}
Also used : ArrayList(java.util.ArrayList) ReportSourceTag(wavefront.report.ReportSourceTag) SourceTag(com.wavefront.dto.SourceTag) SpanLogs(wavefront.report.SpanLogs) SpanUtils.handleSpanLogs(com.wavefront.agent.listeners.tracing.SpanUtils.handleSpanLogs) SpanUtils.preprocessAndHandleSpan(com.wavefront.agent.listeners.tracing.SpanUtils.preprocessAndHandleSpan) Span(wavefront.report.Span) ReportEvent(wavefront.report.ReportEvent) DataFormat(com.wavefront.agent.formatter.DataFormat) ReportSourceTag(wavefront.report.ReportSourceTag) ReportPoint(wavefront.report.ReportPoint)

Example 25 with Span

use of wavefront.report.Span in project java by wavefrontHQ.

the class CustomTracingPortUnificationHandler method report.

@Override
protected void report(Span object) {
    // report converted metrics/histograms from the span
    String applicationName = null;
    String serviceName = null;
    String cluster = NULL_TAG_VAL;
    String shard = NULL_TAG_VAL;
    String componentTagValue = NULL_TAG_VAL;
    String isError = "false";
    List<Annotation> annotations = object.getAnnotations();
    for (Annotation annotation : annotations) {
        switch(annotation.getKey()) {
            case APPLICATION_TAG_KEY:
                applicationName = annotation.getValue();
                continue;
            case SERVICE_TAG_KEY:
                serviceName = annotation.getValue();
                continue;
            case CLUSTER_TAG_KEY:
                cluster = annotation.getValue();
                continue;
            case SHARD_TAG_KEY:
                shard = annotation.getValue();
                continue;
            case COMPONENT_TAG_KEY:
                componentTagValue = annotation.getValue();
                continue;
            case ERROR_TAG_KEY:
                isError = annotation.getValue();
                continue;
        }
    }
    if (applicationName == null || serviceName == null) {
        logger.warning("Ingested spans discarded because span application/service name is " + "missing.");
        discardedSpans.inc();
        return;
    }
    handler.report(object);
    // update application and service for red metrics
    applicationName = firstNonNull(applicationName, proxyLevelApplicationName);
    serviceName = firstNonNull(serviceName, proxyLevelServiceName);
    if (wfInternalReporter != null) {
        List<Pair<String, String>> spanTags = annotations.stream().map(a -> new Pair<>(a.getKey(), a.getValue())).collect(Collectors.toList());
        discoveredHeartbeatMetrics.add(reportWavefrontGeneratedData(wfInternalReporter, object.getName(), applicationName, serviceName, cluster, shard, object.getSource(), componentTagValue, Boolean.parseBoolean(isError), millisToMicros(object.getDuration()), traceDerivedCustomTagKeys, spanTags, true));
        try {
            reportHeartbeats(wfSender, discoveredHeartbeatMetrics, "wavefront-generated");
        } catch (IOException e) {
            logger.log(Level.WARNING, "Cannot report heartbeat metric to wavefront");
        }
    }
}
Also used : Annotation(wavefront.report.Annotation) StringUtils(org.apache.commons.lang.StringUtils) HealthCheckManager(com.wavefront.agent.channel.HealthCheckManager) SpanSampler(com.wavefront.agent.sampler.SpanSampler) NULL_TAG_VAL(com.wavefront.sdk.common.Constants.NULL_TAG_VAL) SpanLogs(wavefront.report.SpanLogs) ERROR_TAG_KEY(com.wavefront.sdk.common.Constants.ERROR_TAG_KEY) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) APPLICATION_TAG_KEY(com.wavefront.sdk.common.Constants.APPLICATION_TAG_KEY) TokenAuthenticator(com.wavefront.agent.auth.TokenAuthenticator) ReportableEntityHandler(com.wavefront.agent.handlers.ReportableEntityHandler) COMPONENT_TAG_KEY(com.wavefront.sdk.common.Constants.COMPONENT_TAG_KEY) Map(java.util.Map) SpanDerivedMetricsUtils.reportWavefrontGeneratedData(com.wavefront.internal.SpanDerivedMetricsUtils.reportWavefrontGeneratedData) HandlerKey(com.wavefront.agent.handlers.HandlerKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) WavefrontInternalReporter(com.wavefront.internal.reporter.WavefrontInternalReporter) Nullable(javax.annotation.Nullable) SHARD_TAG_KEY(com.wavefront.sdk.common.Constants.SHARD_TAG_KEY) WavefrontSender(com.wavefront.sdk.common.WavefrontSender) ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) Set(java.util.Set) IOException(java.io.IOException) Span(wavefront.report.Span) ObjectUtils.firstNonNull(org.apache.commons.lang3.ObjectUtils.firstNonNull) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SpanDerivedMetricsUtils.reportHeartbeats(com.wavefront.internal.SpanDerivedMetricsUtils.reportHeartbeats) ReportableEntityType(com.wavefront.data.ReportableEntityType) List(java.util.List) SERVICE_TAG_KEY(com.wavefront.sdk.common.Constants.SERVICE_TAG_KEY) ReportableEntityHandlerFactory(com.wavefront.agent.handlers.ReportableEntityHandlerFactory) Pair(com.wavefront.sdk.common.Pair) ReportableEntityDecoder(com.wavefront.ingester.ReportableEntityDecoder) ChannelHandler(io.netty.channel.ChannelHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CLUSTER_TAG_KEY(com.wavefront.sdk.common.Constants.CLUSTER_TAG_KEY) IOException(java.io.IOException) Annotation(wavefront.report.Annotation) Pair(com.wavefront.sdk.common.Pair)

Aggregations

Span (wavefront.report.Span)49 Test (org.junit.Test)35 Annotation (wavefront.report.Annotation)34 SpanSampler (com.wavefront.agent.sampler.SpanSampler)18 TestUtils.parseSpan (com.wavefront.agent.TestUtils.parseSpan)15 Collectors (java.util.stream.Collectors)13 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 ReportableEntityPreprocessor (com.wavefront.agent.preprocessor.ReportableEntityPreprocessor)10 Batch (io.jaegertracing.thriftjava.Batch)10 ImmutableList (com.google.common.collect.ImmutableList)9 Tag (io.jaegertracing.thriftjava.Tag)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 Assert.assertFalse (org.junit.Assert.assertFalse)9 Assert.assertTrue (org.junit.Assert.assertTrue)9 BeforeClass (org.junit.BeforeClass)9 RateSampler (com.wavefront.sdk.entities.tracing.sampling.RateSampler)8 List (java.util.List)8 Map (java.util.Map)8