use of io.opentelemetry.api.common.Attributes in project splunk-otel-java by signalfx.
the class LogDataCommonAttributesTest method testBuild.
@Test
void testBuild() {
String sourceType = "otel.profiling";
String eventName = "core.core.unicorn";
Attributes expected = Attributes.builder().put(SOURCE_TYPE, sourceType).put(SOURCE_EVENT_NAME, eventName).put(SOURCE_EVENT_PERIOD, 999L).build();
EventPeriods periods = mock(EventPeriods.class);
RecordedEvent event = mock(RecordedEvent.class);
EventType eventType = mock(EventType.class);
when(event.getEventType()).thenReturn(eventType);
when(eventType.getName()).thenReturn(eventName);
when(periods.getDuration(eventName)).thenReturn(Duration.ofMillis(999));
LogDataCommonAttributes logDataAttributes = new LogDataCommonAttributes(periods);
Attributes result = logDataAttributes.builder(event.getEventType().getName()).build();
assertEquals(expected, result);
}
use of io.opentelemetry.api.common.Attributes in project splunk-otel-java by signalfx.
the class LogDataCreatorTest method testCreate.
@Test
void testCreate() {
Instant time = Instant.now();
String stack = "the.stack";
SpanContext spanContext = SpanContext.create("deadbeefdeadbeefdeadbeefdeadbeef", "0123012301230123", TraceFlags.getSampled(), TraceState.getDefault());
Context context = Context.root().with(Span.wrap(spanContext));
long threadId = 987L;
String eventName = "GoodEventHere";
EventPeriods periods = mock(EventPeriods.class);
when(periods.getDuration(eventName)).thenReturn(Duration.ofMillis(606));
SpanLinkage linkage = new SpanLinkage(spanContext, threadId);
Resource resource = Resource.getDefault();
Attributes attributes = Attributes.of(SOURCE_EVENT_NAME, eventName, SOURCE_EVENT_PERIOD, 606L, SOURCE_TYPE, "otel.profiling");
LogData expected = LogDataBuilder.create(resource, INSTRUMENTATION_LIBRARY_INFO).setContext(context).setBody(stack).setEpoch(time).setAttributes(attributes).build();
StackToSpanLinkage linkedSpan = new StackToSpanLinkage(time, "the.stack", eventName, linkage);
LogDataCreator creator = new LogDataCreator(new LogDataCommonAttributes(periods), Resource.getDefault());
LogData result = creator.apply(linkedSpan);
assertEquals(expected, result);
}
use of io.opentelemetry.api.common.Attributes in project splunk-otel-java by signalfx.
the class ResourceAttributesToSystemProperties method afterAgent.
@Override
public void afterAgent(Config config, AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
Attributes attributes = autoConfiguredOpenTelemetrySdk.getResource().getAttributes();
attributes.forEach((k, v) -> {
if (k.getType() == AttributeType.STRING) {
System.setProperty("otel.resource." + k.getKey(), v.toString());
}
});
}
use of io.opentelemetry.api.common.Attributes 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.api.common.Attributes 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