Search in sources :

Example 1 with Link

use of io.opentelemetry.proto.trace.v1.Span.Link 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 Link

use of io.opentelemetry.proto.trace.v1.Span.Link in project opentelemetry-java by open-telemetry.

the class OtlpExporterIntegrationTest method testTraceExport.

private static void testTraceExport(SpanExporter spanExporter) {
    SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).setResource(RESOURCE).build();
    SpanContext linkContext = SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault());
    Span span = tracerProvider.get(OtlpExporterIntegrationTest.class.getName()).spanBuilder("my span name").addLink(linkContext).startSpan();
    span.setAttribute("key", "value");
    span.addEvent("event");
    span.end();
    await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.traceRequests).hasSize(1));
    tracerProvider.close();
    ExportTraceServiceRequest request = grpcServer.traceRequests.get(0);
    assertThat(request.getResourceSpansCount()).isEqualTo(1);
    ResourceSpans resourceSpans = request.getResourceSpans(0);
    assertThat(resourceSpans.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
    assertThat(resourceSpans.getScopeSpansCount()).isEqualTo(1);
    ScopeSpans ilSpans = resourceSpans.getScopeSpans(0);
    assertThat(ilSpans.getScope().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
    assertThat(ilSpans.getSpansCount()).isEqualTo(1);
    io.opentelemetry.proto.trace.v1.Span protoSpan = ilSpans.getSpans(0);
    assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(span.getSpanContext().getTraceIdBytes());
    assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(span.getSpanContext().getSpanIdBytes());
    assertThat(protoSpan.getName()).isEqualTo("my span name");
    assertThat(protoSpan.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
    assertThat(protoSpan.getEventsCount()).isEqualTo(1);
    assertThat(protoSpan.getEvents(0).getName()).isEqualTo("event");
    assertThat(protoSpan.getLinksCount()).isEqualTo(1);
    Link link = protoSpan.getLinks(0);
    assertThat(link.getTraceId().toByteArray()).isEqualTo(linkContext.getTraceIdBytes());
    assertThat(link.getSpanId().toByteArray()).isEqualTo(linkContext.getSpanIdBytes());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanContext(io.opentelemetry.api.trace.SpanContext) ScopeSpans(io.opentelemetry.proto.trace.v1.ScopeSpans) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Span(io.opentelemetry.api.trace.Span) Link(io.opentelemetry.proto.trace.v1.Span.Link)

Example 3 with Link

use of io.opentelemetry.proto.trace.v1.Span.Link in project opentelemetry-java-instrumentation by open-telemetry.

the class AgentTestingExporterAccess method getExportedSpans.

@SuppressWarnings("unchecked")
public static List<SpanData> getExportedSpans() {
    List<byte[]> exportRequests;
    try {
        exportRequests = (List<byte[]>) getSpanExportRequests.invokeExact();
    } catch (Throwable t) {
        throw new AssertionError("Could not invoke getSpanExportRequests", t);
    }
    List<ResourceSpans> allResourceSpans = exportRequests.stream().map(serialized -> {
        try {
            return ExportTraceServiceRequest.parseFrom(serialized);
        } catch (InvalidProtocolBufferException e) {
            throw new AssertionError(e);
        }
    }).flatMap(request -> request.getResourceSpansList().stream()).collect(toList());
    List<SpanData> spans = new ArrayList<>();
    for (ResourceSpans resourceSpans : allResourceSpans) {
        Resource resource = resourceSpans.getResource();
        for (ScopeSpans ilSpans : resourceSpans.getScopeSpansList()) {
            InstrumentationScope instrumentationScope = ilSpans.getScope();
            for (Span span : ilSpans.getSpansList()) {
                String traceId = bytesToHex(span.getTraceId().toByteArray());
                spans.add(TestSpanData.builder().setSpanContext(SpanContext.create(traceId, bytesToHex(span.getSpanId().toByteArray()), TraceFlags.getDefault(), extractTraceState(span.getTraceState()))).setParentSpanContext(SpanContext.create(traceId, bytesToHex(span.getParentSpanId().toByteArray()), TraceFlags.getDefault(), TraceState.getDefault())).setResource(io.opentelemetry.sdk.resources.Resource.create(fromProto(resource.getAttributesList()))).setInstrumentationScopeInfo(InstrumentationScopeInfo.create(instrumentationScope.getName(), instrumentationScope.getVersion(), /* schemaUrl= */
                null)).setName(span.getName()).setStartEpochNanos(span.getStartTimeUnixNano()).setEndEpochNanos(span.getEndTimeUnixNano()).setAttributes(fromProto(span.getAttributesList())).setEvents(span.getEventsList().stream().map(event -> EventData.create(event.getTimeUnixNano(), event.getName(), fromProto(event.getAttributesList()), event.getDroppedAttributesCount() + event.getAttributesCount())).collect(toList())).setStatus(fromProto(span.getStatus())).setKind(fromProto(span.getKind())).setLinks(span.getLinksList().stream().map(link -> LinkData.create(SpanContext.create(bytesToHex(link.getTraceId().toByteArray()), bytesToHex(link.getSpanId().toByteArray()), TraceFlags.getDefault(), extractTraceState(link.getTraceState())), fromProto(link.getAttributesList()), link.getDroppedAttributesCount() + link.getAttributesCount())).collect(toList())).setHasEnded(true).setTotalRecordedEvents(span.getEventsCount() + span.getDroppedEventsCount()).setTotalRecordedLinks(span.getLinksCount() + span.getDroppedLinksCount()).setTotalAttributeCount(span.getAttributesCount() + span.getDroppedAttributesCount()).build());
            }
        }
    }
    return spans;
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags) LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) KeyValue(io.opentelemetry.proto.common.v1.KeyValue) EventData(io.opentelemetry.sdk.trace.data.EventData) Sum(io.opentelemetry.proto.metrics.v1.Sum) StatusCode(io.opentelemetry.api.trace.StatusCode) ImmutableSummaryPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryPointData) Attributes(io.opentelemetry.api.common.Attributes) ImmutableValueAtQuantile(io.opentelemetry.sdk.metrics.internal.data.ImmutableValueAtQuantile) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Resource(io.opentelemetry.proto.resource.v1.Resource) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) ValueAtQuantile(io.opentelemetry.sdk.metrics.data.ValueAtQuantile) AttributeKey.stringArrayKey(io.opentelemetry.api.common.AttributeKey.stringArrayKey) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SpanContext(io.opentelemetry.api.trace.SpanContext) SummaryPointData(io.opentelemetry.sdk.metrics.data.SummaryPointData) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) AttributeKey.doubleArrayKey(io.opentelemetry.api.common.AttributeKey.doubleArrayKey) ImmutableDoublePointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableDoublePointData) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) ArrayValue(io.opentelemetry.proto.common.v1.ArrayValue) ImmutableLongPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableLongPointData) InstrumentationScope(io.opentelemetry.proto.common.v1.InstrumentationScope) List(java.util.List) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanData(io.opentelemetry.sdk.trace.data.SpanData) AttributeKey.longArrayKey(io.opentelemetry.api.common.AttributeKey.longArrayKey) AnyValue(io.opentelemetry.proto.common.v1.AnyValue) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) Pattern(java.util.regex.Pattern) HistogramDataPoint(io.opentelemetry.proto.metrics.v1.HistogramDataPoint) MethodHandle(java.lang.invoke.MethodHandle) StatusData(io.opentelemetry.sdk.trace.data.StatusData) ImmutableSummaryData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData) ScopeMetrics(io.opentelemetry.proto.metrics.v1.ScopeMetrics) ArrayList(java.util.ArrayList) TraceState(io.opentelemetry.api.trace.TraceState) Metric(io.opentelemetry.proto.metrics.v1.Metric) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) InstrumentationScopeInfo(io.opentelemetry.sdk.common.InstrumentationScopeInfo) ScopeSpans(io.opentelemetry.proto.trace.v1.ScopeSpans) TraceStateBuilder(io.opentelemetry.api.trace.TraceStateBuilder) LogData(io.opentelemetry.sdk.logs.data.LogData) ImmutableGaugeData(io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData) Severity(io.opentelemetry.sdk.logs.data.Severity) ImmutableMetricData(io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData) ImmutableSumData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData) ScopeLogs(io.opentelemetry.proto.logs.v1.ScopeLogs) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) SeverityNumber(io.opentelemetry.proto.logs.v1.SeverityNumber) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Span(io.opentelemetry.proto.trace.v1.Span) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) TimeUnit(java.util.concurrent.TimeUnit) ImmutableHistogramPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramPointData) Collectors.toList(java.util.stream.Collectors.toList) MethodType(java.lang.invoke.MethodType) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) AttributeKey.booleanArrayKey(io.opentelemetry.api.common.AttributeKey.booleanArrayKey) ImmutableHistogramData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramData) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SummaryDataPoint(io.opentelemetry.proto.metrics.v1.SummaryDataPoint) Status(io.opentelemetry.proto.trace.v1.Status) TestSpanData(io.opentelemetry.sdk.testing.trace.TestSpanData) SpanData(io.opentelemetry.sdk.trace.data.SpanData) TestSpanData(io.opentelemetry.sdk.testing.trace.TestSpanData) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Resource(io.opentelemetry.proto.resource.v1.Resource) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) InstrumentationScope(io.opentelemetry.proto.common.v1.InstrumentationScope) Span(io.opentelemetry.proto.trace.v1.Span) ScopeSpans(io.opentelemetry.proto.trace.v1.ScopeSpans)

Example 4 with Link

use of io.opentelemetry.proto.trace.v1.Span.Link in project inspectit-ocelot by inspectIT.

the class OpenTelemetryProtoConverter method convert.

/**
 * Converts open-telemetry proto data to the open-telemetry SDK span data.
 *
 * @param data data to convert
 *
 * @return Non-null collection of {@link SpanData}
 */
public Collection<SpanData> convert(ExportTraceServiceRequest data) {
    List<SpanData> result = new ArrayList<>();
    for (ResourceSpans resourceSpans : data.getResourceSpansList()) {
        // create general span resources, e.g. sdk-version, service-name, ...
        Attributes attributes = OcelotSpanUtils.toAttributes(resourceSpans.getResource().getAttributesList());
        final Resource resource = Resource.create(attributes);
        Map<String, String> customSpanAttributes = getCustomSpanAttributes();
        resourceSpans.getInstrumentationLibrarySpansList().stream().flatMap(librarySpans -> toSpanData(librarySpans, resource, customSpanAttributes)).forEach(result::add);
    }
    return result;
}
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) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Attributes(io.opentelemetry.api.common.Attributes) Resource(io.opentelemetry.sdk.resources.Resource) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans)

Example 5 with Link

use of io.opentelemetry.proto.trace.v1.Span.Link 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)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)4 ExportTraceServiceRequest (io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest)4 ResourceSpans (io.opentelemetry.proto.trace.v1.ResourceSpans)4 SpanData (io.opentelemetry.sdk.trace.data.SpanData)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)3 Resource (io.opentelemetry.sdk.resources.Resource)3 Slf4j (lombok.extern.slf4j.Slf4j)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)2 SpanContext (io.opentelemetry.api.trace.SpanContext)2 AnyValue (io.opentelemetry.proto.common.v1.AnyValue)2 KeyValue (io.opentelemetry.proto.common.v1.KeyValue)2 ScopeSpans (io.opentelemetry.proto.trace.v1.ScopeSpans)2 Span (io.opentelemetry.proto.trace.v1.Span)2 Status (io.opentelemetry.proto.trace.v1.Status)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2