use of io.opentelemetry.sdk.metrics.data.LongPointData in project opentelemetry-java by open-telemetry.
the class MetricAdapter method toSamples.
// Converts a list of points from MetricData to a list of Prometheus Samples.
static List<Sample> toSamples(String name, MetricDataType type, Collection<? extends PointData> points) {
List<Sample> samples = new ArrayList<>(estimateNumSamples(points.size(), type));
for (PointData pointData : points) {
Attributes attributes = pointData.getAttributes();
List<String> labelNames = new ArrayList<>(attributes.size());
List<String> labelValues = new ArrayList<>(attributes.size());
attributes.forEach((key, value) -> {
String sanitizedLabelName = sanitizer.apply(key.getKey());
labelNames.add(sanitizedLabelName);
// TODO: We want to create an error-log if there is overlap in toString of attribute
// values for the same key name.
labelValues.add(value == null ? "" : value.toString());
});
switch(type) {
case DOUBLE_SUM:
case DOUBLE_GAUGE:
DoublePointData doublePoint = (DoublePointData) pointData;
samples.add(createSample(name, labelNames, labelValues, doublePoint.getValue(), // Prometheus doesn't support exemplars on SUM/GAUGE
null, doublePoint.getEpochNanos()));
break;
case LONG_SUM:
case LONG_GAUGE:
LongPointData longPoint = (LongPointData) pointData;
samples.add(createSample(name, labelNames, labelValues, longPoint.getValue(), // Prometheus doesn't support exemplars on SUM/GAUGE
null, longPoint.getEpochNanos()));
break;
case SUMMARY:
addSummarySamples((SummaryPointData) pointData, name, labelNames, labelValues, samples);
break;
case HISTOGRAM:
addHistogramSamples((HistogramPointData) pointData, name, labelNames, labelValues, samples);
break;
case EXPONENTIAL_HISTOGRAM:
// todo
break;
}
}
return samples;
}
use of io.opentelemetry.sdk.metrics.data.LongPointData in project opentelemetry-java by open-telemetry.
the class Serializer method write.
private void write(MetricData metric, Writer writer) throws IOException {
// Not supported in specification yet.
if (metric.getType() == MetricDataType.EXPONENTIAL_HISTOGRAM) {
return;
}
PrometheusType type = PrometheusType.forMetric(metric);
String name = NameSanitizer.INSTANCE.apply(metric.getName());
String headerName = headerName(name, type);
if (type == PrometheusType.COUNTER) {
name = name + "_total";
}
if (!metricNameFilter.test(name)) {
return;
}
writer.write("# TYPE ");
writer.write(headerName);
writer.write(' ');
writer.write(type.getTypeString());
writer.write('\n');
writer.write("# HELP ");
writer.write(headerName);
writer.write(' ');
writeHelp(writer, metric.getDescription());
writer.write('\n');
for (PointData point : MetricAdapter.getPoints(metric)) {
switch(metric.getType()) {
case DOUBLE_SUM:
case DOUBLE_GAUGE:
writePoint(writer, name, ((DoublePointData) point).getValue(), point.getAttributes(), point.getEpochNanos());
break;
case LONG_SUM:
case LONG_GAUGE:
writePoint(writer, name, ((LongPointData) point).getValue(), point.getAttributes(), point.getEpochNanos());
break;
case HISTOGRAM:
writeHistogram(writer, name, (HistogramPointData) point);
break;
case SUMMARY:
writeSummary(writer, name, (SummaryPointData) point);
break;
case EXPONENTIAL_HISTOGRAM:
throw new IllegalArgumentException("Can't happen");
}
}
}
use of io.opentelemetry.sdk.metrics.data.LongPointData in project opentelemetry-java by open-telemetry.
the class BatchSpanProcessorMetrics method getMetric.
private long getMetric(boolean dropped) {
String labelValue = String.valueOf(dropped);
OptionalLong value = allMetrics.stream().filter(metricData -> metricData.getName().equals("processedSpans")).filter(metricData -> !metricData.isEmpty()).map(metricData -> metricData.getLongSumData().getPoints()).flatMap(Collection::stream).filter(point -> labelValue.equals(point.getAttributes().get(stringKey("dropped")))).mapToLong(LongPointData::getValue).findFirst();
return value.isPresent() ? value.getAsLong() : 0;
}
use of io.opentelemetry.sdk.metrics.data.LongPointData in project opentelemetry-java by open-telemetry.
the class NumberDataPointMarshaler method create.
static NumberDataPointMarshaler create(PointData point) {
ExemplarMarshaler[] exemplarMarshalers = ExemplarMarshaler.createRepeated(point.getExemplars());
KeyValueMarshaler[] attributeMarshalers = KeyValueMarshaler.createRepeated(point.getAttributes());
ProtoFieldInfo valueField;
if (point instanceof LongPointData) {
valueField = NumberDataPoint.AS_INT;
} else {
assert point instanceof DoublePointData;
valueField = NumberDataPoint.AS_DOUBLE;
}
return new NumberDataPointMarshaler(point.getStartEpochNanos(), point.getEpochNanos(), point, valueField, exemplarMarshalers, attributeMarshalers);
}
use of io.opentelemetry.sdk.metrics.data.LongPointData in project opentelemetry-java by open-telemetry.
the class NumberDataPointMarshaler method calculateSize.
private static int calculateSize(long startTimeUnixNano, long timeUnixNano, ProtoFieldInfo valueField, PointData value, ExemplarMarshaler[] exemplars, KeyValueMarshaler[] attributes) {
int size = 0;
size += MarshalerUtil.sizeFixed64(NumberDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
size += MarshalerUtil.sizeFixed64(NumberDataPoint.TIME_UNIX_NANO, timeUnixNano);
if (valueField == NumberDataPoint.AS_INT) {
size += MarshalerUtil.sizeFixed64Optional(valueField, ((LongPointData) value).getValue());
} else {
size += MarshalerUtil.sizeDoubleOptional(valueField, ((DoublePointData) value).getValue());
}
size += MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.EXEMPLARS, exemplars);
size += MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.ATTRIBUTES, attributes);
return size;
}
Aggregations