Search in sources :

Example 46 with Attributes

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

the class SdkObservableLongCounterTest method collectMetrics_DeltaSumAggregator.

@Test
void collectMetrics_DeltaSumAggregator() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_COUNTER).build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
    sdkMeterProvider.get(getClass().getName()).counterBuilder("testObserver").buildWithCallback(result -> result.record(12, Attributes.builder().put("k", "v").build()));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(0).attributes().hasSize(1).containsEntry("k", "v")));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) View(io.opentelemetry.sdk.metrics.view.View) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Example 47 with Attributes

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

the class FixedSizeExemplarReservoirTest method oneMeasurement_includesTraceAndSpanIds.

@Test
public void oneMeasurement_includesTraceAndSpanIds() {
    Attributes all = Attributes.builder().put("one", 1).put("two", "two").put("three", true).build();
    Context context = Context.root().with(Span.wrap(SpanContext.createFromRemoteParent(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault())));
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 1, RandomSupplier.platformDefault());
    reservoir.offerMeasurement(1L, all, context);
    assertThat(reservoir.collectAndReset(Attributes.empty())).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasValue(1).hasFilteredAttributes(all).hasTraceId(TRACE_ID).hasSpanId(SPAN_ID));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) TestClock(io.opentelemetry.sdk.testing.time.TestClock) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 48 with Attributes

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

the class SdkObservableLongUpDownCounterTest method collectMetrics_DeltaSumAggregator.

@Test
void collectMetrics_DeltaSumAggregator() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER).build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
    sdkMeterProvider.get(getClass().getName()).upDownCounterBuilder("testObserver").buildWithCallback(result -> result.record(12, Attributes.builder().put("k", "v").build()));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isNotMonotonic().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isNotMonotonic().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(0).attributes().hasSize(1).containsEntry("k", "v")));
}
Also used : ObservableLongUpDownCounter(io.opentelemetry.api.metrics.ObservableLongUpDownCounter) Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) View(io.opentelemetry.sdk.metrics.view.View) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Example 49 with Attributes

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

the class AggregatorHandleTest method testOfferMeasurementLongToExemplar.

@Test
void testOfferMeasurementLongToExemplar() {
    TestAggregatorHandle testAggregator = new TestAggregatorHandle(reservoir);
    Attributes attributes = Attributes.builder().put("test", "value").build();
    Context context = Context.root();
    testAggregator.recordLong(1L, attributes, context);
    Mockito.verify(reservoir).offerMeasurement(1L, attributes, context);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 50 with Attributes

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

the class DoubleLastValueAggregatorTest method mergeAccumulation.

@Test
void mergeAccumulation() {
    Attributes attributes = Attributes.builder().put("test", "value").build();
    ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    List<ExemplarData> exemplars = Collections.singletonList(exemplar);
    List<ExemplarData> previousExemplars = Collections.singletonList(DoubleExemplarData.create(attributes, 1L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 2));
    DoubleAccumulation result = aggregator.merge(DoubleAccumulation.create(1, previousExemplars), DoubleAccumulation.create(2, exemplars));
    // Assert that latest measurement is kept.
    assertThat(result).isEqualTo(DoubleAccumulation.create(2, exemplars));
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

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