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);
}
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());
}
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;
}
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;
}
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;
}
}
Aggregations