Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…y-java into multiple-callbacks
  • Loading branch information
jack-berg committed Feb 25, 2022
2 parents b3881bf + c053393 commit 7dd81c7
Show file tree
Hide file tree
Showing 40 changed files with 758 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/** A builder of {@link Attributes} supporting an arbitrary number of key-value pairs. */
Expand Down Expand Up @@ -100,6 +101,19 @@ default AttributesBuilder put(String key, String... value) {
return put(stringArrayKey(key), Arrays.asList(value));
}

/**
* Puts a List attribute into this.
*
* @return this Builder
*/
@SuppressWarnings("unchecked")
default <T> AttributesBuilder put(AttributeKey<List<T>> key, T... value) {
if (value == null) {
return this;
}
return put(key, Arrays.asList(value));
}

/**
* Puts a Long array attribute into this.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,52 @@ void builder() {
assertThat(attributes).isEqualTo(wantAttributes);
}

@Test
void builderWithAttributeKeyList() {
Attributes attributes =
Attributes.builder()
.put("string", "value1")
.put(longKey("long"), 10)
.put(stringArrayKey("anotherString"), "value1", "value2", "value3")
.put(longArrayKey("anotherLong"), 10L, 20L, 30L)
.put(booleanArrayKey("anotherBoolean"), true, false, true)
.build();

Attributes wantAttributes =
Attributes.of(
stringKey("string"),
"value1",
longKey("long"),
10L,
stringArrayKey("anotherString"),
Arrays.asList("value1", "value2", "value3"),
longArrayKey("anotherLong"),
Arrays.asList(10L, 20L, 30L),
booleanArrayKey("anotherBoolean"),
Arrays.asList(true, false, true));
assertThat(attributes).isEqualTo(wantAttributes);

AttributesBuilder newAttributes = attributes.toBuilder();
newAttributes.put("newKey", "newValue");
assertThat(newAttributes.build())
.isEqualTo(
Attributes.of(
stringKey("string"),
"value1",
longKey("long"),
10L,
stringArrayKey("anotherString"),
Arrays.asList("value1", "value2", "value3"),
longArrayKey("anotherLong"),
Arrays.asList(10L, 20L, 30L),
booleanArrayKey("anotherBoolean"),
Arrays.asList(true, false, true),
stringKey("newKey"),
"newValue"));
// Original not mutated.
assertThat(attributes).isEqualTo(wantAttributes);
}

@Test
void builder_arrayTypes() {
Attributes attributes =
Expand Down
3 changes: 3 additions & 0 deletions docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Comparing source compatibility of against
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.common.AttributesBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++! NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.AttributesBuilder put(io.opentelemetry.api.common.AttributeKey, java.lang.Object[])
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.metrics.ObservableDoubleCounter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW INTERFACE: java.lang.AutoCloseable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.exporter.internal.otlp.metrics;

import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.ProtoFieldInfo;
Expand All @@ -15,7 +16,6 @@
import io.opentelemetry.sdk.metrics.data.LongExemplarData;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nullable;

final class ExemplarMarshaler extends MarshalerWithSize {

Expand All @@ -24,8 +24,7 @@ final class ExemplarMarshaler extends MarshalerWithSize {
private final ExemplarData value;
private final ProtoFieldInfo valueField;

@Nullable private final String spanId;
@Nullable private final String traceId;
private final SpanContext spanContext;

private final KeyValueMarshaler[] filteredAttributeMarshalers;

Expand Down Expand Up @@ -54,26 +53,21 @@ private static ExemplarMarshaler create(ExemplarData exemplar) {
exemplar.getEpochNanos(),
exemplar,
valueField,
exemplar.getSpanId(),
exemplar.getTraceId(),
exemplar.getSpanContext(),
attributeMarshalers);
}

private ExemplarMarshaler(
long timeUnixNano,
ExemplarData value,
ProtoFieldInfo valueField,
@Nullable String spanId,
@Nullable String traceId,
SpanContext spanContext,
KeyValueMarshaler[] filteredAttributeMarshalers) {
super(
calculateSize(
timeUnixNano, valueField, value, spanId, traceId, filteredAttributeMarshalers));
super(calculateSize(timeUnixNano, valueField, value, spanContext, filteredAttributeMarshalers));
this.timeUnixNano = timeUnixNano;
this.value = value;
this.valueField = valueField;
this.spanId = spanId;
this.traceId = traceId;
this.spanContext = spanContext;
this.filteredAttributeMarshalers = filteredAttributeMarshalers;
}

Expand All @@ -86,8 +80,12 @@ public void writeTo(Serializer output) throws IOException {
} else {
output.serializeDoubleOptional(valueField, ((DoubleExemplarData) value).getValue());
}
output.serializeSpanId(io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanId);
output.serializeTraceId(io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID, traceId);
if (spanContext.isValid()) {
output.serializeSpanId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanContext.getSpanId());
output.serializeTraceId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID, spanContext.getTraceId());
}
output.serializeRepeatedMessage(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES,
filteredAttributeMarshalers);
Expand All @@ -97,8 +95,7 @@ private static int calculateSize(
long timeUnixNano,
ProtoFieldInfo valueField,
ExemplarData value,
@Nullable String spanId,
@Nullable String traceId,
SpanContext spanContext,
KeyValueMarshaler[] filteredAttributeMarshalers) {
int size = 0;
size +=
Expand All @@ -109,12 +106,15 @@ private static int calculateSize(
} else {
size += MarshalerUtil.sizeDoubleOptional(valueField, ((DoubleExemplarData) value).getValue());
}
size +=
MarshalerUtil.sizeSpanId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanId);
size +=
MarshalerUtil.sizeTraceId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID, traceId);
if (spanContext.isValid()) {
size +=
MarshalerUtil.sizeSpanId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanContext.getSpanId());
size +=
MarshalerUtil.sizeTraceId(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID,
spanContext.getTraceId());
}
size +=
MarshalerUtil.sizeRepeatedMessage(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.google.protobuf.util.JsonFormat;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.common.v1.AnyValue;
Expand All @@ -39,7 +42,6 @@
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoubleExemplarData;
import io.opentelemetry.sdk.metrics.data.DoubleGaugeData;
import io.opentelemetry.sdk.metrics.data.DoubleHistogramData;
import io.opentelemetry.sdk.metrics.data.DoubleHistogramPointData;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
Expand All @@ -50,12 +52,12 @@
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramData;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramPointData;
import io.opentelemetry.sdk.metrics.data.LongExemplarData;
import io.opentelemetry.sdk.metrics.data.LongGaugeData;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.LongSumData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.PointData;
import io.opentelemetry.sdk.metrics.data.ValueAtPercentile;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -92,8 +94,11 @@ void dataPoint_withDefaultValues() {
LongExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
/*spanId=*/ "0000000000000002",
/*traceId=*/ "00000000000000000000000000000001",
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
0))))))
.containsExactly(
NumberDataPoint.newBuilder()
Expand Down Expand Up @@ -131,8 +136,11 @@ void dataPoint_withDefaultValues() {
DoubleExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
/*spanId=*/ "0000000000000002",
/*traceId=*/ "00000000000000000000000000000001",
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
0))))))
.containsExactly(
NumberDataPoint.newBuilder()
Expand Down Expand Up @@ -174,8 +182,11 @@ void longDataPoints() {
LongExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
/*spanId=*/ "0000000000000002",
/*traceId=*/ "00000000000000000000000000000001",
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
1))))))
.containsExactly(
NumberDataPoint.newBuilder()
Expand Down Expand Up @@ -342,8 +353,11 @@ void histogramDataPoints() {
DoubleExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
/*spanId=*/ "0000000000000002",
/*traceId=*/ "00000000000000000000000000000001",
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
1.5))))))
.containsExactly(
HistogramDataPoint.newBuilder()
Expand Down Expand Up @@ -399,8 +413,11 @@ void exponentialHistogramDataPoints() {
DoubleExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
/*spanId=*/ "0000000000000002",
/*traceId=*/ "00000000000000000000000000000001",
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
1.5))))))
.containsExactly(
ExponentialHistogramDataPoint.newBuilder()
Expand Down Expand Up @@ -596,7 +613,7 @@ void toProtoMetric_gauges() {
"name",
"description",
"1",
LongGaugeData.create(
ImmutableGaugeData.create(
singletonList(LongPointData.create(123, 456, KV_ATTR, 5))))))
.isEqualTo(
Metric.newBuilder()
Expand Down Expand Up @@ -627,7 +644,7 @@ void toProtoMetric_gauges() {
"name",
"description",
"1",
DoubleGaugeData.create(
ImmutableGaugeData.create(
singletonList(DoublePointData.create(123, 456, KV_ATTR, 5.1))))))
.isEqualTo(
Metric.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import static io.prometheus.client.Collector.doubleToGoString;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoubleExemplarData;
import io.opentelemetry.sdk.metrics.data.DoubleHistogramPointData;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.DoubleSumData;
import io.opentelemetry.sdk.metrics.data.DoubleSummaryPointData;
import io.opentelemetry.sdk.metrics.data.ExemplarData;
import io.opentelemetry.sdk.metrics.data.LongExemplarData;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.LongSumData;
import io.opentelemetry.sdk.metrics.data.MetricData;
Expand Down Expand Up @@ -261,7 +264,7 @@ private static ExemplarData filterExemplars(
Collection<ExemplarData> exemplars, double min, double max) {
ExemplarData result = null;
for (ExemplarData e : exemplars) {
double value = e.getValueAsDouble();
double value = getExemplarValue(e);
if (value <= max && value > min) {
result = e;
}
Expand Down Expand Up @@ -324,17 +327,24 @@ private static Sample createSample(

private static io.prometheus.client.exemplars.Exemplar toPrometheusExemplar(
ExemplarData exemplar) {
if (exemplar.getSpanId() != null && exemplar.getTraceId() != null) {
SpanContext spanContext = exemplar.getSpanContext();
if (spanContext.isValid()) {
return new io.prometheus.client.exemplars.Exemplar(
exemplar.getValueAsDouble(),
getExemplarValue(exemplar),
// Convert to ms for prometheus, truncate nanosecond precision.
TimeUnit.NANOSECONDS.toMillis(exemplar.getEpochNanos()),
"trace_id",
exemplar.getTraceId(),
spanContext.getTraceId(),
"span_id",
exemplar.getSpanId());
spanContext.getSpanId());
}
return new io.prometheus.client.exemplars.Exemplar(exemplar.getValueAsDouble());
return new io.prometheus.client.exemplars.Exemplar(getExemplarValue(exemplar));
}

private static double getExemplarValue(ExemplarData exemplar) {
return exemplar instanceof DoubleExemplarData
? ((DoubleExemplarData) exemplar).getValue()
: (double) ((LongExemplarData) exemplar).getValue();
}

private MetricAdapter() {}
Expand Down
Loading

0 comments on commit 7dd81c7

Please sign in to comment.