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();
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations