Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service.name attribute to metrics #1048

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion runtime/exporter-otlp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
</artifactItems>
<includes>
io/aklivity/zilla/specs/exporter/otlp/application/**/*,
io/aklivity/zilla/specs/exporter/otlp/network/**/*,
io/aklivity/zilla/specs/exporter/otlp/config/*
</includes>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.aklivity.zilla.runtime.engine.metrics.Metric.Unit.COUNT;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -43,6 +44,7 @@ public class OtlpMetricsSerializer
{
private static final String SCOPE_NAME = "OtlpMetricsSerializer";
private static final String SCOPE_VERSION = "1.0.0";
private static final String SERVICE_NAME = "service.name";
// CUMULATIVE is an AggregationTemporality for a metric aggregator which reports changes since a fixed start time.
private static final int CUMULATIVE = 2;
private static final Map<String, String> SERVER_METRIC_NAMES = Map.of(
Expand All @@ -66,6 +68,7 @@ public class OtlpMetricsSerializer
private final LongFunction<KindConfig> resolveKind;
private final Function<String, Metric> resolveMetric;
private final OtlpMetricsDescriptor descriptor;
private final AttributeConfig serviceNameAttribute;

public OtlpMetricsSerializer(
List<MetricRecord> records,
Expand All @@ -78,6 +81,7 @@ public OtlpMetricsSerializer(
this.resolveMetric = resolveMetric;
this.resolveKind = resolveKind;
this.descriptor = new OtlpMetricsDescriptor();
this.serviceNameAttribute = resolveServiceNameAttribute();
}

public String serializeAll()
Expand All @@ -89,6 +93,24 @@ public String serializeAll()
return createJson(attributesArray, metricsArray);
}

private AttributeConfig resolveServiceNameAttribute()
{
String serviceName = null;
for (AttributeConfig attribute : attributes)
{
if (SERVICE_NAME.equals(attribute.name))
{
serviceName = attribute.value;
break;
}
}
return serviceName == null ? null :
AttributeConfig.builder()
.name(SERVICE_NAME)
.value(serviceName)
.build();
}

private JsonObject serialize(
MetricRecord metric)
{
Expand Down Expand Up @@ -140,16 +162,22 @@ private long now()
private JsonArrayBuilder attributes(
MetricRecord record)
{
return attributesToJson(List.of(
AttributeConfig.builder()
.name("namespace")
.value(record.namespace())
.build(),
AttributeConfig.builder()
.name("binding")
.value(record.binding())
.build()
));
List<AttributeConfig> attributes = new LinkedList<>();
attributes.add(AttributeConfig.builder()
.name("namespace")
.value(record.namespace())
.build()
);
attributes.add(AttributeConfig.builder()
.name("binding")
.value(record.binding())
.build()
);
if (serviceNameAttribute != null)
{
attributes.add(serviceNameAttribute);
}
return attributesToJson(attributes);
}

private JsonArrayBuilder attributesToJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.kaazing.k3po.junit.annotation.Specification;
import org.kaazing.k3po.junit.rules.K3poRule;

import io.aklivity.zilla.runtime.engine.namespace.NamespacedId;
import io.aklivity.zilla.runtime.engine.test.EngineRule;
import io.aklivity.zilla.runtime.engine.test.annotation.Configuration;

Expand Down Expand Up @@ -57,16 +58,44 @@ public class MetricsIT
public void shouldPostMetricsToOtlpCollector() throws Exception
{
// GIVEN
writeMetrics();

// WHEN
// the exporter publishes the metric data to the collector in json format

// THEN
k3po.finish();
}

@Test
@Configuration("metrics.with.service.name.yaml")
@Specification({
"metrics.with.service.name/server"
})
public void shouldPostMetricsWithServiceNameToOtlpCollector() throws Exception
{
// GIVEN
writeMetrics();

// WHEN
// the exporter publishes the metric data to the collector in json format

// THEN
k3po.finish();
}

private void writeMetrics()
{
int namespaceId = engine.supplyLabelId("test");
int bindingId = engine.supplyLabelId("net0");
int counterId = engine.supplyLabelId("test.counter");
int gaugeId = engine.supplyLabelId("test.gauge");
int histogramId = engine.supplyLabelId("test.histogram");

long nsBindingId = namespacedId(namespaceId, bindingId);
long nsCounterId = namespacedId(namespaceId, counterId);
long nsGaugeId = namespacedId(namespaceId, gaugeId);
long nsHistogramId = namespacedId(namespaceId, histogramId);
long nsBindingId = NamespacedId.id(namespaceId, bindingId);
long nsCounterId = NamespacedId.id(namespaceId, counterId);
long nsGaugeId = NamespacedId.id(namespaceId, gaugeId);
long nsHistogramId = NamespacedId.id(namespaceId, histogramId);

LongConsumer counterWriter0 = engine.counterWriter(nsBindingId, nsCounterId, 0);
LongConsumer counterWriter1 = engine.counterWriter(nsBindingId, nsCounterId, 1);
Expand All @@ -93,18 +122,5 @@ public void shouldPostMetricsToOtlpCollector() throws Exception
histogramWriter1.accept(17L);
histogramWriter2.accept(18L);
// 1 value goes to bucket #0 and 2 values go to bucket #4

// WHEN
// the exporter publishes the metric data to the collector in json format

// THEN
k3po.finish();
}

private static long namespacedId(
final int namespaceId,
final int localId)
{
return (long) namespaceId << Integer.SIZE | (long) localId << 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

connect "http://localhost:4318/v1/metrics"
connected

write http:method "POST"
write http:version "HTTP/1.1"
write http:header "Host" "localhost:4318"
write http:header "Content-Length" "942"
write
'{'
'"resourceMetrics":['
'{'
'"resource":{'
'"attributes":['
'{'
'"key":"service.namespace",'
'"value":{'
'"stringValue":"example"'
'}'
'},'
'{'
'"key":"service.name",'
'"value":{'
'"stringValue":"zilla"'
'}'
'}'
']'
'},'
'"scopeMetrics":['
'{'
'"scope":{'
'"name":"OtlpMetricsSerializer",'
'"version":"1.0.0"'
'},'
'"metrics":['
'{'
'"name":"test.counter",'
'"unit":"",'
'"description":"Description for test.counter",'
'"sum":{'
'"dataPoints":['
'{'
'"asInt":77,'
'"timeUnixNano":0123456789123456789,'
'"attributes":['
'{'
'"key":"namespace",'
'"value":{'
'"stringValue":"test"'
'}'
'},'
'{'
'"key":"binding",'
'"value":{'
'"stringValue":"net0"'
'}'
'},'
'{'
'"key":"service.name",'
'"value":{'
'"stringValue":"zilla"'
'}'
'}'
']'
'}'
'],'
'"aggregationTemporality":2,'
'"isMonotonic":true'
'}'
'},'
'{'
'"name":"test.gauge",'
'"unit":"",'
'"description":"Description for test.gauge",'
'"gauge":{'
'"dataPoints":['
'{'
'"asInt":66,'
'"timeUnixNano":0123456789123456789,'
'"attributes":['
'{'
'"key":"namespace",'
'"value":{'
'"stringValue":"test"'
'}'
'},'
'{'
'"key":"binding",'
'"value":{'
'"stringValue":"net0"'
'}'
'},'
'{'
'"key":"service.name",'
'"value":{'
'"stringValue":"zilla"'
'}'
'}'
']'
'}'
']'
'}'
'}'
']'
'}'
']'
'}'
']'
'}'
write close

read http:status "200" "OK"
Loading