Search in sources :

Example 1 with InstrumentationLibraryInfo

use of io.opentelemetry.sdk.common.InstrumentationLibraryInfo in project inspectit-ocelot by inspectIT.

the class OpenTelemetryProtoConverter method toSpanData.

/**
 * @return Converts an {@link InstrumentationLibrarySpans} instance to a stream of individual {@link SpanData} instances.
 */
private Stream<SpanData> toSpanData(InstrumentationLibrarySpans librarySpans, Resource resource, Map<String, String> customSpanAttributes) {
    InstrumentationLibrary library = librarySpans.getInstrumentationLibrary();
    InstrumentationLibraryInfo libraryInfo = InstrumentationLibraryInfo.create(library.getName(), library.getVersion());
    return librarySpans.getSpansList().stream().map(protoSpan -> OcelotSpanUtils.createSpanData(protoSpan, resource, libraryInfo, customSpanAttributes)).filter(Objects::nonNull);
}
Also used : java.util(java.util) RequestUtils(rocks.inspectit.oce.eum.server.utils.RequestUtils) ImmutableMap(com.google.common.collect.ImmutableMap) Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) Autowired(org.springframework.beans.factory.annotation.Autowired) Supplier(java.util.function.Supplier) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) EumServerConfiguration(rocks.inspectit.oce.eum.server.configuration.model.EumServerConfiguration) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) HttpServletRequest(javax.servlet.http.HttpServletRequest) Stream(java.util.stream.Stream) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) OcelotSpanUtils(io.opentelemetry.sdk.trace.OcelotSpanUtils) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanData(io.opentelemetry.sdk.trace.data.SpanData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary)

Example 2 with InstrumentationLibraryInfo

use of io.opentelemetry.sdk.common.InstrumentationLibraryInfo in project opentelemetry-java by open-telemetry.

the class LoggingSpanExporter method export.

@Override
public CompletableResultCode export(Collection<SpanData> spans) {
    // We always have 32 + 16 + name + several whitespace, 60 seems like an OK initial guess.
    StringBuilder sb = new StringBuilder(60);
    for (SpanData span : spans) {
        sb.setLength(0);
        InstrumentationLibraryInfo instrumentationLibraryInfo = span.getInstrumentationLibraryInfo();
        sb.append("'").append(span.getName()).append("' : ").append(span.getTraceId()).append(" ").append(span.getSpanId()).append(" ").append(span.getKind()).append(" [tracer: ").append(instrumentationLibraryInfo.getName()).append(":").append(instrumentationLibraryInfo.getVersion() == null ? "" : instrumentationLibraryInfo.getVersion()).append("] ").append(span.getAttributes());
        logger.log(Level.INFO, sb.toString());
    }
    return CompletableResultCode.ofSuccess();
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo)

Example 3 with InstrumentationLibraryInfo

use of io.opentelemetry.sdk.common.InstrumentationLibraryInfo in project inspectit-ocelot by inspectIT.

the class OcelotSpanUtils method createSpanData.

/**
 * Creates a {@link SpanData} instance based on the given arguments.
 *
 * @param protoSpan                  the protobuf representation of the span
 * @param resource                   the span's resources
 * @param instrumentationLibraryInfo the information of the tracing library
 * @param customSpanAttributes       additional attributes which should be added to each span
 *
 * @return the created {@link SpanData} instance
 */
public static SpanData createSpanData(Span protoSpan, Resource resource, InstrumentationLibraryInfo instrumentationLibraryInfo, Map<String, String> customSpanAttributes) {
    try {
        String traceId = toIdString(protoSpan.getTraceId());
        String spanId = toIdString(protoSpan.getSpanId());
        String parentSpanId = toIdString(protoSpan.getParentSpanId());
        SpanContext spanContext = createSpanContext(traceId, spanId);
        SpanContext parentSpanContext = createSpanContext(traceId, parentSpanId);
        // only create spans with valid context
        if (!spanContext.isValid()) {
            return null;
        }
        // span data
        String name = protoSpan.getName();
        long startTime = protoSpan.getStartTimeUnixNano();
        SpanLimits spanLimits = SpanLimits.getDefault();
        int totalRecordedLinks = protoSpan.getLinksCount() + protoSpan.getDroppedLinksCount();
        SpanKind spanKind = toSpanKind(protoSpan.getKind());
        List<LinkData> links = toLinkData(protoSpan.getLinksList());
        // convert attributes map to AttributesMap
        Attributes spanAttributes = toAttributes(protoSpan.getAttributesList(), customSpanAttributes);
        Map<AttributeKey<?>, Object> attributesMap = spanAttributes.asMap();
        AttributesMap spanAttributesMap = new AttributesMap(attributesMap.size());
        spanAttributesMap.putAll(attributesMap);
        // creating the actual span
        RecordEventsReadableSpan span = RecordEventsReadableSpan.startSpan(spanContext, name, instrumentationLibraryInfo, spanKind, parentSpanContext, NOOP_CONTEXT, spanLimits, NOOP_SPAN_PROCESSOR, SystemClock.getInstance(), resource, spanAttributesMap, links, totalRecordedLinks, startTime);
        // add events to the span - and filter events which occurred before the actual span
        protoSpan.getEventsList().stream().filter(event -> event.getTimeUnixNano() >= span.getStartEpochNanos()).forEach(event -> {
            Attributes attributes = toAttributes(event.getAttributesList());
            span.addEvent(event.getName(), attributes, event.getTimeUnixNano(), TimeUnit.NANOSECONDS);
        });
        // the span's status code
        Status status = protoSpan.getStatus();
        StatusCode statusCode = toStatusCode(status.getCode());
        if (statusCode != null) {
            span.setStatus(statusCode, status.getMessage());
        }
        // set end time if available
        long endTime = protoSpan.getEndTimeUnixNano();
        if (endTime > 0) {
            span.end(endTime, TimeUnit.NANOSECONDS);
        }
        return span.toSpanData();
    } catch (Exception e) {
        log.warn("Error converting OT proto span {} to span data.", protoSpan, e);
        return null;
    }
}
Also used : KeyValue(io.opentelemetry.proto.common.v1.KeyValue) Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) io.opentelemetry.api.trace(io.opentelemetry.api.trace) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Map(java.util.Map) Nullable(javax.annotation.Nullable) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) BaseEncoding(com.google.common.io.BaseEncoding) Collectors(java.util.stream.Collectors) Span(io.opentelemetry.proto.trace.v1.Span) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) ByteString(com.google.protobuf.ByteString) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) AttributeKey(io.opentelemetry.api.common.AttributeKey) SystemClock(io.opentelemetry.sdk.internal.SystemClock) SpanData(io.opentelemetry.sdk.trace.data.SpanData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AnyValue(io.opentelemetry.proto.common.v1.AnyValue) LinkData(io.opentelemetry.sdk.trace.data.LinkData) Status(io.opentelemetry.proto.trace.v1.Status) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Status(io.opentelemetry.proto.trace.v1.Status) LinkData(io.opentelemetry.sdk.trace.data.LinkData) Attributes(io.opentelemetry.api.common.Attributes) ByteString(com.google.protobuf.ByteString) AttributeKey(io.opentelemetry.api.common.AttributeKey)

Example 4 with InstrumentationLibraryInfo

use of io.opentelemetry.sdk.common.InstrumentationLibraryInfo in project opentelemetry-java by open-telemetry.

the class MetricsRequestMarshalerTest method protoResourceMetrics.

@Test
void protoResourceMetrics() {
    Resource resource = Resource.create(Attributes.of(stringKey("ka"), "va"), "http://resource.url");
    io.opentelemetry.proto.resource.v1.Resource resourceProto = io.opentelemetry.proto.resource.v1.Resource.newBuilder().addAllAttributes(singletonList(KeyValue.newBuilder().setKey("ka").setValue(stringValue("va")).build())).build();
    io.opentelemetry.proto.resource.v1.Resource emptyResourceProto = io.opentelemetry.proto.resource.v1.Resource.newBuilder().build();
    InstrumentationLibraryInfo instrumentationLibraryInfo = InstrumentationLibraryInfo.create("name", "version", "http://url");
    InstrumentationLibrary instrumentationLibraryProto = InstrumentationLibrary.newBuilder().setName("name").setVersion("version").build();
    InstrumentationLibrary emptyInstrumentationLibraryProto = InstrumentationLibrary.newBuilder().setName("").setVersion("").build();
    Metric metricDoubleSum = Metric.newBuilder().setName("name").setDescription("description").setUnit("1").setSum(Sum.newBuilder().setIsMonotonic(true).setAggregationTemporality(AGGREGATION_TEMPORALITY_CUMULATIVE).addDataPoints(NumberDataPoint.newBuilder().setStartTimeUnixNano(123).setTimeUnixNano(456).addAllAttributes(singletonList(KeyValue.newBuilder().setKey("k").setValue(stringValue("v")).build())).setAsDouble(5.0).build()).build()).build();
    assertThat(toProtoResourceMetrics(ImmutableList.of(MetricData.createDoubleSum(resource, instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
    true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(resource, instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
    true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(Resource.empty(), instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
    true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(Resource.empty(), InstrumentationLibraryInfo.empty(), "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
    true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0))))))).satisfiesExactlyInAnyOrder(resourceMetrics -> {
        assertThat(resourceMetrics.getResource()).isEqualTo(resourceProto);
        assertThat(resourceMetrics.getSchemaUrl()).isEqualTo("http://resource.url");
        assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).containsExactlyInAnyOrder(InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(instrumentationLibraryProto).addAllMetrics(ImmutableList.of(metricDoubleSum, metricDoubleSum)).setSchemaUrl("http://url").build());
    }, resourceMetrics -> {
        assertThat(resourceMetrics.getResource()).isEqualTo(emptyResourceProto);
        assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).containsExactlyInAnyOrder(InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(emptyInstrumentationLibraryProto).addAllMetrics(singletonList(metricDoubleSum)).build(), InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(instrumentationLibraryProto).addAllMetrics(singletonList(metricDoubleSum)).setSchemaUrl("http://url").build());
    });
}
Also used : InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Resource(io.opentelemetry.sdk.resources.Resource) Metric(io.opentelemetry.proto.metrics.v1.Metric) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) Test(org.junit.jupiter.api.Test)

Example 5 with InstrumentationLibraryInfo

use of io.opentelemetry.sdk.common.InstrumentationLibraryInfo in project opentelemetry-java by open-telemetry.

the class ZipkinSpanExporter method generateSpan.

Span generateSpan(SpanData spanData) {
    Endpoint endpoint = getEndpoint(spanData);
    long startTimestamp = toEpochMicros(spanData.getStartEpochNanos());
    long endTimestamp = toEpochMicros(spanData.getEndEpochNanos());
    Span.Builder spanBuilder = Span.newBuilder().traceId(spanData.getTraceId()).id(spanData.getSpanId()).kind(toSpanKind(spanData)).name(spanData.getName()).timestamp(toEpochMicros(spanData.getStartEpochNanos())).duration(Math.max(1, endTimestamp - startTimestamp)).localEndpoint(endpoint);
    if (spanData.getParentSpanContext().isValid()) {
        spanBuilder.parentId(spanData.getParentSpanId());
    }
    Attributes spanAttributes = spanData.getAttributes();
    spanAttributes.forEach((key, value) -> spanBuilder.putTag(key.getKey(), valueToString(key, value)));
    int droppedAttributes = spanData.getTotalAttributeCount() - spanAttributes.size();
    if (droppedAttributes > 0) {
        spanBuilder.putTag(OTEL_DROPPED_ATTRIBUTES_COUNT, String.valueOf(droppedAttributes));
    }
    StatusData status = spanData.getStatus();
    // include status code & error.
    if (status.getStatusCode() != StatusCode.UNSET) {
        spanBuilder.putTag(OTEL_STATUS_CODE, status.getStatusCode().toString());
        // add the error tag, if it isn't already in the source span.
        if (status.getStatusCode() == StatusCode.ERROR && spanAttributes.get(STATUS_ERROR) == null) {
            spanBuilder.putTag(STATUS_ERROR.getKey(), nullToEmpty(status.getDescription()));
        }
    }
    InstrumentationLibraryInfo instrumentationLibraryInfo = spanData.getInstrumentationLibraryInfo();
    if (!instrumentationLibraryInfo.getName().isEmpty()) {
        spanBuilder.putTag(KEY_INSTRUMENTATION_LIBRARY_NAME, instrumentationLibraryInfo.getName());
    }
    if (instrumentationLibraryInfo.getVersion() != null) {
        spanBuilder.putTag(KEY_INSTRUMENTATION_LIBRARY_VERSION, instrumentationLibraryInfo.getVersion());
    }
    for (EventData annotation : spanData.getEvents()) {
        spanBuilder.addAnnotation(toEpochMicros(annotation.getEpochNanos()), annotation.getName());
    }
    int droppedEvents = spanData.getTotalRecordedEvents() - spanData.getEvents().size();
    if (droppedEvents > 0) {
        spanBuilder.putTag(OTEL_DROPPED_EVENTS_COUNT, String.valueOf(droppedEvents));
    }
    return spanBuilder.build();
}
Also used : Endpoint(zipkin2.Endpoint) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Span(zipkin2.Span) Endpoint(zipkin2.Endpoint) StatusData(io.opentelemetry.sdk.trace.data.StatusData) EventData(io.opentelemetry.sdk.trace.data.EventData)

Aggregations

InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)8 Resource (io.opentelemetry.sdk.resources.Resource)4 SpanData (io.opentelemetry.sdk.trace.data.SpanData)4 Attributes (io.opentelemetry.api.common.Attributes)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 InstrumentationLibrary (io.opentelemetry.proto.common.v1.InstrumentationLibrary)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Test (org.junit.jupiter.api.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BaseEncoding (com.google.common.io.BaseEncoding)1 ByteString (com.google.protobuf.ByteString)1 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)1 io.opentelemetry.api.trace (io.opentelemetry.api.trace)1 Context (io.opentelemetry.context.Context)1 ContextKey (io.opentelemetry.context.ContextKey)1 ExportTraceServiceRequest (io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest)1 AnyValue (io.opentelemetry.proto.common.v1.AnyValue)1 KeyValue (io.opentelemetry.proto.common.v1.KeyValue)1 Metric (io.opentelemetry.proto.metrics.v1.Metric)1