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