Search in sources :

Example 1 with AttributeValue

use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.

the class SpanDataTest method spanDataEquals.

@Test
public void spanDataEquals() {
    SpanData allSpanData1 = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, Kind.CLIENT, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
    SpanData allSpanData2 = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, Kind.CLIENT, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
    SpanData emptySpanData = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, null, startTimestamp, Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0), TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0), TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0), Links.create(Collections.<Link>emptyList(), 0), 0, status, endTimestamp);
    new EqualsTester().addEqualityGroup(allSpanData1, allSpanData2).addEqualityGroup(emptySpanData).testEquals();
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) EqualsTester(com.google.common.testing.EqualsTester) TimedEvent(io.opencensus.trace.export.SpanData.TimedEvent) Link(io.opencensus.trace.Link) Test(org.junit.Test)

Example 2 with AttributeValue

use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.

the class RecordEventsSpanImplTest method droppingAndAddingAttributes.

@Test
public void droppingAndAddingAttributes() {
    final int maxNumberOfAttributes = 8;
    TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setMaxNumberOfAttributes(maxNumberOfAttributes).build();
    RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, traceParams, startEndHandler, timestampConverter, testClock);
    for (int i = 0; i < 2 * maxNumberOfAttributes; i++) {
        Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
        attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
        span.putAttributes(attributes);
    }
    SpanData spanData = span.toSpanData();
    assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
    assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
    for (int i = 0; i < maxNumberOfAttributes; i++) {
        assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
    }
    for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
        Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
        attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
        span.putAttributes(attributes);
    }
    spanData = span.toSpanData();
    assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes * 3 / 2);
    assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
    // Test that we still have in the attributes map the latest maxNumberOfAttributes / 2 entries.
    for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
        assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes * 3 / 2))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes * 3 / 2));
    }
    // Test that we have the newest re-added initial entries.
    for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
        assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + i)).isEqualTo(AttributeValue.longAttributeValue(i));
    }
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) SpanData(io.opencensus.trace.export.SpanData) HashMap(java.util.HashMap) TraceParams(io.opencensus.trace.config.TraceParams) Test(org.junit.Test)

Example 3 with AttributeValue

use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.

the class RecordEventsSpanImplTest method droppingAttributes.

@Test
public void droppingAttributes() {
    final int maxNumberOfAttributes = 8;
    TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setMaxNumberOfAttributes(maxNumberOfAttributes).build();
    RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, traceParams, startEndHandler, timestampConverter, testClock);
    for (int i = 0; i < 2 * maxNumberOfAttributes; i++) {
        Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
        attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
        span.putAttributes(attributes);
    }
    SpanData spanData = span.toSpanData();
    assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
    assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
    for (int i = 0; i < maxNumberOfAttributes; i++) {
        assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
    }
    span.end();
    spanData = span.toSpanData();
    assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
    assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
    for (int i = 0; i < maxNumberOfAttributes; i++) {
        assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
    }
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) SpanData(io.opencensus.trace.export.SpanData) HashMap(java.util.HashMap) TraceParams(io.opencensus.trace.config.TraceParams) Test(org.junit.Test)

Example 4 with AttributeValue

use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.

the class DatadogExporterHandler method convertToJson.

String convertToJson(Collection<SpanData> spanDataList) {
    final ArrayList<DatadogSpan> datadogSpans = new ArrayList<>();
    for (SpanData sd : spanDataList) {
        SpanContext sc = sd.getContext();
        final long startTime = timestampToNanos(sd.getStartTimestamp());
        final Timestamp endTimestamp = Optional.ofNullable(sd.getEndTimestamp()).orElseGet(() -> Tracing.getClock().now());
        final long endTime = timestampToNanos(endTimestamp);
        final long duration = endTime - startTime;
        final Long parentId = Optional.ofNullable(sd.getParentSpanId()).map(DatadogExporterHandler::convertSpanId).orElse(null);
        final Map<String, AttributeValue> attributes = sd.getAttributes().getAttributeMap();
        final Map<String, String> meta = attributes.isEmpty() ? new HashMap<>() : attributesToMeta(attributes);
        final String resource = meta.getOrDefault("resource", "UNKNOWN");
        final DatadogSpan span = new DatadogSpan(sc.getTraceId().getLowerLong(), convertSpanId(sc.getSpanId()), sd.getName(), resource, this.service, this.type, startTime, duration, parentId, errorCode(sd.getStatus()), meta);
        datadogSpans.add(span);
    }
    final Collection<List<DatadogSpan>> traces = datadogSpans.stream().collect(Collectors.groupingBy(DatadogSpan::getTraceId, Collectors.toList())).values();
    return gson.toJson(traces);
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Timestamp(io.opencensus.common.Timestamp) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with AttributeValue

use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.

the class JsonConversionUtils method convertToJson.

/**
 * Converts a collection of {@link SpanData} to a Collection of json string.
 *
 * @param appName the name of app to include in traces.
 * @param spanDataList Collection of {@code SpanData} to be converted to json.
 * @return Collection of {@code SpanData} converted to JSON to be indexed.
 */
static List<String> convertToJson(String appName, Collection<SpanData> spanDataList) {
    List<String> spanJson = new ArrayList<String>();
    if (spanDataList == null) {
        return spanJson;
    }
    StringBuilder sb = new StringBuilder();
    for (final SpanData span : spanDataList) {
        final SpanContext spanContext = span.getContext();
        final SpanId parentSpanId = span.getParentSpanId();
        final Timestamp startTimestamp = span.getStartTimestamp();
        final Timestamp endTimestamp = span.getEndTimestamp();
        final Status status = span.getStatus();
        if (endTimestamp == null) {
            continue;
        }
        sb.append('{');
        sb.append("\"appName\":\"").append(appName).append("\",");
        sb.append("\"spanId\":\"").append(encodeSpanId(spanContext.getSpanId())).append("\",");
        sb.append("\"traceId\":\"").append(encodeTraceId(spanContext.getTraceId())).append("\",");
        if (parentSpanId != null) {
            sb.append("\"parentId\":\"").append(encodeSpanId(parentSpanId)).append("\",");
        }
        sb.append("\"timestamp\":").append(toMillis(startTimestamp)).append(',');
        sb.append("\"duration\":").append(toMillis(startTimestamp, endTimestamp)).append(',');
        sb.append("\"name\":\"").append(toSpanName(span)).append("\",");
        sb.append("\"kind\":\"").append(toSpanKind(span)).append("\",");
        sb.append("\"dateStarted\":\"").append(formatDate(startTimestamp)).append("\",");
        sb.append("\"dateEnded\":\"").append(formatDate(endTimestamp)).append('"');
        if (status == null) {
            sb.append(",\"status\":").append("\"ok\"");
        } else if (!status.isOk()) {
            sb.append(",\"error\":").append("true");
        }
        Map<String, AttributeValue> attributeMap = span.getAttributes().getAttributeMap();
        if (attributeMap.size() > 0) {
            StringBuilder builder = new StringBuilder();
            builder.append('{');
            for (Entry<String, AttributeValue> entry : attributeMap.entrySet()) {
                if (builder.length() > 1) {
                    builder.append(',');
                }
                builder.append("\"").append(entry.getKey()).append("\":\"").append(attributeValueToString(entry.getValue())).append("\"");
            }
            builder.append('}');
            sb.append(",\"data\":").append(builder);
        }
        sb.append('}');
        spanJson.add(sb.toString());
    }
    return spanJson;
}
Also used : Status(io.opencensus.trace.Status) AttributeValue(io.opencensus.trace.AttributeValue) SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Timestamp(io.opencensus.common.Timestamp) SpanId(io.opencensus.trace.SpanId)

Aggregations

AttributeValue (io.opencensus.trace.AttributeValue)15 SpanData (io.opencensus.trace.export.SpanData)11 Test (org.junit.Test)8 Status (io.opencensus.trace.Status)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Link (io.opencensus.trace.Link)4 SpanContext (io.opencensus.trace.SpanContext)4 List (java.util.List)4 Timestamp (io.opencensus.common.Timestamp)3 AttributeValue.stringAttributeValue (io.opencensus.trace.AttributeValue.stringAttributeValue)3 SpanId (io.opencensus.trace.SpanId)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Scope (org.apache.ignite.spi.tracing.Scope)3 TracingConfigurationCoordinates (org.apache.ignite.spi.tracing.TracingConfigurationCoordinates)3 TracingConfigurationParameters (org.apache.ignite.spi.tracing.TracingConfigurationParameters)3 Tag (io.jaegertracing.thriftjava.Tag)2 Span (io.opencensus.trace.Span)2 TraceParams (io.opencensus.trace.config.TraceParams)2