Search in sources :

Example 26 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method setAttribute_afterEnd.

@Test
void setAttribute_afterEnd() {
    SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME);
    spanBuilder.setAttribute("string", "value");
    spanBuilder.setAttribute("long", 12345L);
    spanBuilder.setAttribute("double", .12345);
    spanBuilder.setAttribute("boolean", true);
    spanBuilder.setAttribute(stringKey("stringAttribute"), "attrvalue");
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    try {
        Attributes attrs = span.toSpanData().getAttributes();
        assertThat(attrs.size()).isEqualTo(5);
        assertThat(attrs.get(stringKey("string"))).isEqualTo("value");
        assertThat(attrs.get(longKey("long"))).isEqualTo(12345L);
        assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345);
        assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true);
        assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue");
    } finally {
        span.end();
    }
    span.setAttribute("string2", "value");
    span.setAttribute("long2", 12345L);
    span.setAttribute("double2", .12345);
    span.setAttribute("boolean2", true);
    span.setAttribute(stringKey("stringAttribute2"), "attrvalue");
    Attributes attrs = span.toSpanData().getAttributes();
    assertThat(attrs.size()).isEqualTo(5);
    assertThat(attrs.get(stringKey("string2"))).isNull();
    assertThat(attrs.get(longKey("long2"))).isNull();
    assertThat(attrs.get(doubleKey("double2"))).isNull();
    assertThat(attrs.get(booleanKey("boolean2"))).isNull();
    assertThat(attrs.get(stringKey("stringAttribute2"))).isNull();
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 27 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class MetricAdapter method convertSummaryPoints.

static Collection<SummaryPointData> convertSummaryPoints(Metric censusMetric) {
    List<SummaryPointData> result = new ArrayList<>();
    for (TimeSeries ts : censusMetric.getTimeSeriesList()) {
        long startTimestamp = mapTimestamp(ts.getStartTimestamp());
        Attributes attributes = mapAttributes(censusMetric.getMetricDescriptor().getLabelKeys(), ts.getLabelValues());
        for (Point point : ts.getPoints()) {
            SummaryPointData otelPoint = point.getValue().match(dv -> null, lv -> null, distribution -> null, summary -> ImmutableSummaryPointData.create(startTimestamp, mapTimestamp(point.getTimestamp()), attributes, summary.getCount(), summary.getSum(), mapValueAtPercentiles(summary.getSnapshot().getValueAtPercentiles())), defaultValue -> null);
            if (otelPoint != null) {
                result.add(otelPoint);
            }
        }
    }
    return result;
}
Also used : ImmutableSummaryPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryPointData) SummaryPointData(io.opentelemetry.sdk.metrics.data.SummaryPointData) TimeSeries(io.opencensus.metrics.export.TimeSeries) ArrayList(java.util.ArrayList) Attributes(io.opentelemetry.api.common.Attributes) Point(io.opencensus.metrics.export.Point)

Example 28 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class MetricAdapter method convertHistogramPoints.

static Collection<HistogramPointData> convertHistogramPoints(Metric censusMetric) {
    boolean isGauge = censusMetric.getMetricDescriptor().getType() == MetricDescriptor.Type.GAUGE_DISTRIBUTION;
    // TODO - preallocate array to correct size.
    List<HistogramPointData> result = new ArrayList<>();
    for (TimeSeries ts : censusMetric.getTimeSeriesList()) {
        long startTimestamp = mapTimestamp(ts.getStartTimestamp());
        Attributes attributes = mapAttributes(censusMetric.getMetricDescriptor().getLabelKeys(), ts.getLabelValues());
        for (Point point : ts.getPoints()) {
            long endTimestamp = mapTimestamp(point.getTimestamp());
            HistogramPointData otelPoint = point.getValue().match(doubleValue -> null, longValue -> null, distribution -> ImmutableHistogramPointData.create(// Report Gauge histograms as DELTA with "instantaneous" time window.
            isGauge ? endTimestamp : startTimestamp, endTimestamp, attributes, distribution.getSum(), mapBoundaries(distribution.getBucketOptions()), mapCounts(distribution.getBuckets()), mapExemplars(distribution.getBuckets())), summary -> null, defaultValue -> null);
            if (otelPoint != null) {
                result.add(otelPoint);
            }
        }
    }
    return result;
}
Also used : TimeSeries(io.opencensus.metrics.export.TimeSeries) ArrayList(java.util.ArrayList) Attributes(io.opentelemetry.api.common.Attributes) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) ImmutableHistogramPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramPointData) Point(io.opencensus.metrics.export.Point)

Example 29 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class MetricAdapter method convertDoublePoints.

static Collection<DoublePointData> convertDoublePoints(Metric censusMetric) {
    // TODO - preallocate array to correct size.
    List<DoublePointData> result = new ArrayList<>();
    for (TimeSeries ts : censusMetric.getTimeSeriesList()) {
        long startTimestamp = mapTimestamp(ts.getStartTimestamp());
        Attributes attributes = mapAttributes(censusMetric.getMetricDescriptor().getLabelKeys(), ts.getLabelValues());
        for (Point point : ts.getPoints()) {
            result.add(ImmutableDoublePointData.create(startTimestamp, mapTimestamp(point.getTimestamp()), attributes, doubleValue(point)));
        }
    }
    return result;
}
Also used : TimeSeries(io.opencensus.metrics.export.TimeSeries) ArrayList(java.util.ArrayList) Attributes(io.opentelemetry.api.common.Attributes) Point(io.opencensus.metrics.export.Point) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) ImmutableDoublePointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableDoublePointData)

Example 30 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SpanShim method logInternal.

private void logInternal(long timestampMicroseconds, Map<String, ?> fields) {
    String name = getEventNameFromFields(fields);
    Throwable throwable = null;
    boolean isError = false;
    if (name.equals(ERROR)) {
        throwable = findThrowable(fields);
        isError = true;
        if (throwable == null) {
            name = SemanticAttributes.EXCEPTION_EVENT_NAME;
        }
    }
    Attributes attributes = convertToAttributes(fields, isError, throwable != null);
    if (throwable != null) {
        // timestamp is not recorded if specified
        span.recordException(throwable, attributes);
    } else if (timestampMicroseconds != -1) {
        span.addEvent(name, attributes, timestampMicroseconds, TimeUnit.MICROSECONDS);
    } else {
        span.addEvent(name, attributes);
    }
}
Also used : SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes)

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