Search in sources :

Example 76 with Attributes

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);
}
Also used : EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) EventType(jdk.jfr.EventType) Attributes(io.opentelemetry.api.common.Attributes) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test)

Example 77 with Attributes

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);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) LogData(io.opentelemetry.sdk.logs.data.LogData) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanContext(io.opentelemetry.api.trace.SpanContext) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) Instant(java.time.Instant) Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 78 with Attributes

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());
        }
    });
}
Also used : Attributes(io.opentelemetry.api.common.Attributes)

Example 79 with Attributes

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;
}
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 80 with Attributes

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;
    }
}
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)175 Test (org.junit.jupiter.api.Test)128 ResourceAttributes (io.opentelemetry.semconv.resource.attributes.ResourceAttributes)45 Resource (io.opentelemetry.sdk.resources.Resource)33 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)27 Context (io.opentelemetry.context.Context)25 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)24 SpanContext (io.opentelemetry.api.trace.SpanContext)22 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)22 SpanData (io.opentelemetry.sdk.trace.data.SpanData)22 HashMap (java.util.HashMap)21 Duration (java.time.Duration)20 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)18 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 AttributeKey (io.opentelemetry.api.common.AttributeKey)17 Span (io.opentelemetry.api.trace.Span)17 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)17 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)16 Map (java.util.Map)16