Skip to content

Commit 0f4beea

Browse files
authored
Add metric config and send metrics (#350)
1 parent 0cafbf2 commit 0f4beea

File tree

13 files changed

+223
-16
lines changed

13 files changed

+223
-16
lines changed

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/EnvironmentConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hypertrace.agent.config.v1.Config.DataCapture;
2424
import org.hypertrace.agent.config.v1.Config.JavaAgent;
2525
import org.hypertrace.agent.config.v1.Config.Message;
26+
import org.hypertrace.agent.config.v1.Config.MetricReporterType;
2627
import org.hypertrace.agent.config.v1.Config.PropagationFormat;
2728
import org.hypertrace.agent.config.v1.Config.Reporting;
2829
import org.hypertrace.agent.config.v1.Config.TraceReporterType;
@@ -43,6 +44,8 @@ private EnvironmentConfig() {}
4344
private static final String REPORTING_PREFIX = HT_PREFIX + "reporting.";
4445
static final String REPORTING_ENDPOINT = REPORTING_PREFIX + "endpoint";
4546
static final String REPORTING_TRACE_TYPE = REPORTING_PREFIX + "trace.reporter.type";
47+
static final String REPORTING_METRIC_ENDPOINT = REPORTING_PREFIX + "metric.endpoint";
48+
static final String REPORTING_METRIC_TYPE = REPORTING_PREFIX + "metric.reporter.type";
4649
static final String REPORTING_SECURE = REPORTING_PREFIX + "secure";
4750
static final String REPORTING_CERT_FILE = REPORTING_PREFIX + "cert.file";
4851

@@ -123,6 +126,23 @@ private static Reporting.Builder applyReporting(Reporting.Builder builder) {
123126
if (traceReportingType != null) {
124127
builder.setTraceReporterType(TraceReporterType.valueOf(traceReportingType));
125128
}
129+
String metricReportingType = getProperty(REPORTING_METRIC_TYPE);
130+
if (metricReportingType != null) {
131+
builder.setMetricReporterType(MetricReporterType.valueOf(metricReportingType));
132+
}
133+
if (builder.getTraceReporterType().equals(TraceReporterType.ZIPKIN)) {
134+
// disable metric reporting if trace reporter type is ZIPKIN
135+
builder.setMetricReporterType(MetricReporterType.METRIC_REPORTER_TYPE_NONE);
136+
}
137+
String metricReporterAddress = getProperty(REPORTING_METRIC_ENDPOINT);
138+
if (metricReporterAddress != null) {
139+
builder.setMetricEndpoint(StringValue.newBuilder().setValue(metricReporterAddress).build());
140+
} else if (reporterAddress != null
141+
&& TraceReporterType.OTLP.equals(builder.getTraceReporterType())
142+
&& builder.getMetricReporterType() == MetricReporterType.METRIC_REPORTER_TYPE_OTLP) {
143+
// If metric endpoint is not given, use the reporter endpoint if it is otlp
144+
builder.setMetricEndpoint(StringValue.newBuilder().setValue(reporterAddress).build());
145+
}
126146
String secure = getProperty(REPORTING_SECURE);
127147
if (secure != null) {
128148
builder.setSecure(BoolValue.newBuilder().setValue(Boolean.valueOf(secure)).build());

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/HypertraceAgentConfiguration.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.stream.Collectors;
2626
import org.hypertrace.agent.config.v1.Config.AgentConfig;
27+
import org.hypertrace.agent.config.v1.Config.MetricReporterType;
2728
import org.hypertrace.agent.config.v1.Config.PropagationFormat;
2829
import org.hypertrace.agent.config.v1.Config.TraceReporterType;
2930
import org.slf4j.Logger;
@@ -44,7 +45,10 @@ public class HypertraceAgentConfiguration implements ConfigPropertySource {
4445
private static final String OTEL_PROCESSOR_BATCH_MAX_QUEUE = "otel.bsp.max.queue.size";
4546
private static final String OTEL_DEFAULT_LOG_LEVEL =
4647
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";
47-
private static final String OTEL_EXPORTER_OTLP_ENDPOINT = "otel.exporter.otlp.endpoint";
48+
private static final String OTEL_EXPORTER_OTLP_TRACES_ENDPOINT =
49+
"otel.exporter.otlp.traces.endpoint";
50+
private static final String OTEL_EXPORTER_OTLP_METRICS_ENDPOINT =
51+
"otel.exporter.otlp.metrics.endpoint";
4852

4953
private static final String OTEL_ENABLED = "otel.javaagent.enabled";
5054
private static final String OTEL_OTLP_CERT = "otel.exporter.otlp.certificate";
@@ -65,12 +69,28 @@ public Map<String, String> getProperties() {
6569
OTEL_EXPORTER_ZIPKIN_ENDPOINT, agentConfig.getReporting().getEndpoint().getValue());
6670
} else if (agentConfig.getReporting().getTraceReporterType() == TraceReporterType.OTLP) {
6771
configProperties.put(
68-
OTEL_EXPORTER_OTLP_ENDPOINT, agentConfig.getReporting().getEndpoint().getValue());
72+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, agentConfig.getReporting().getEndpoint().getValue());
73+
}
74+
if (agentConfig.getReporting().getMetricReporterType()
75+
== MetricReporterType.METRIC_REPORTER_TYPE_OTLP) {
76+
configProperties.put(
77+
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
78+
agentConfig.getReporting().getMetricEndpoint().getValue());
79+
}
80+
if (agentConfig.getReporting().getMetricReporterType()
81+
== MetricReporterType.METRIC_REPORTER_TYPE_OTLP) {
82+
configProperties.put(OTEL_METRICS_EXPORTER, "otlp");
83+
} else if (agentConfig.getReporting().getMetricReporterType()
84+
== MetricReporterType.METRIC_REPORTER_TYPE_PROMETHEUS) {
85+
configProperties.put(OTEL_METRICS_EXPORTER, "prometheus");
86+
} else if (agentConfig.getReporting().getMetricReporterType()
87+
== MetricReporterType.METRIC_REPORTER_TYPE_LOGGING) {
88+
configProperties.put(OTEL_METRICS_EXPORTER, "logging");
89+
} else {
90+
configProperties.put(OTEL_METRICS_EXPORTER, "none");
6991
}
7092
configProperties.put(
7193
OTEL_PROPAGATORS, toOtelPropagators(agentConfig.getPropagationFormatsList()));
72-
// metrics are not reported
73-
configProperties.put(OTEL_METRICS_EXPORTER, "none");
7494
if (agentConfig.getReporting().hasCertFile()) {
7595
if (agentConfig.getReporting().getTraceReporterType() == TraceReporterType.OTLP) {
7696
configProperties.put(OTEL_OTLP_CERT, agentConfig.getReporting().getCertFile().getValue());

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/HypertraceConfig.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.io.Reader;
33-
import org.hypertrace.agent.config.v1.Config;
3433
import org.hypertrace.agent.config.v1.Config.AgentConfig;
3534
import org.hypertrace.agent.config.v1.Config.DataCapture;
3635
import org.hypertrace.agent.config.v1.Config.Message;
36+
import org.hypertrace.agent.config.v1.Config.MetricReporterType;
3737
import org.hypertrace.agent.config.v1.Config.PropagationFormat;
3838
import org.hypertrace.agent.config.v1.Config.Reporting;
3939
import org.hypertrace.agent.config.v1.Config.TraceReporterType;
@@ -51,6 +51,7 @@ private HypertraceConfig() {}
5151
private static volatile AgentConfig agentConfig;
5252

5353
static final String DEFAULT_SERVICE_NAME = "unknown";
54+
// Default reporting endpoint for traces and metrics
5455
static final String DEFAULT_REPORTING_ENDPOINT = "http://localhost:4317";
5556
// 128 KiB
5657
static final int DEFAULT_BODY_MAX_SIZE_BYTES = 128 * 1024;
@@ -139,9 +140,23 @@ private static Reporting.Builder applyReportingDefaults(Reporting.Builder builde
139140
if (!builder.hasEndpoint()) {
140141
builder.setEndpoint(StringValue.newBuilder().setValue(DEFAULT_REPORTING_ENDPOINT).build());
141142
}
142-
if (builder.getTraceReporterType().equals(Config.TraceReporterType.UNSPECIFIED)) {
143+
if (!builder.hasMetricEndpoint()) {
144+
if (TraceReporterType.OTLP.equals(builder.getTraceReporterType())) {
145+
// If trace reporter type is OTLP, use the same endpoint for metrics
146+
builder.setMetricEndpoint(builder.getEndpoint());
147+
} else {
148+
builder.setMetricEndpoint(
149+
StringValue.newBuilder().setValue(DEFAULT_REPORTING_ENDPOINT).build());
150+
}
151+
}
152+
if (builder.getTraceReporterType().equals(TraceReporterType.UNSPECIFIED)) {
143153
builder.setTraceReporterType(TraceReporterType.OTLP);
144154
}
155+
if (builder
156+
.getMetricReporterType()
157+
.equals(MetricReporterType.METRIC_REPORTER_TYPE_UNSPECIFIED)) {
158+
builder.setMetricReporterType(MetricReporterType.METRIC_REPORTER_TYPE_OTLP);
159+
}
145160
return builder;
146161
}
147162

otel-extensions/src/test/java/org/hypertrace/agent/otel/extensions/config/EnvironmentConfigTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.protobuf.StringValue;
2020
import java.util.Arrays;
2121
import org.hypertrace.agent.config.v1.Config.AgentConfig;
22+
import org.hypertrace.agent.config.v1.Config.MetricReporterType;
2223
import org.hypertrace.agent.config.v1.Config.PropagationFormat;
2324
import org.hypertrace.agent.config.v1.Config.TraceReporterType;
2425
import org.junit.jupiter.api.Assertions;
@@ -29,8 +30,10 @@ class EnvironmentConfigTest {
2930

3031
@Test
3132
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_ENDPOINT)
33+
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_METRIC_ENDPOINT)
3234
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_SECURE)
3335
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_TRACE_TYPE)
36+
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_METRIC_TYPE)
3437
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_CERT_FILE)
3538
@ClearSystemProperty(key = EnvironmentConfig.PROPAGATION_FORMATS)
3639
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request")
@@ -43,6 +46,7 @@ public void systemProperties() {
4346
// parallel tests
4447
System.setProperty(EnvironmentConfig.REPORTING_ENDPOINT, "http://:-)");
4548
System.setProperty(EnvironmentConfig.REPORTING_TRACE_TYPE, "OTLP");
49+
System.setProperty(EnvironmentConfig.REPORTING_METRIC_TYPE, "METRIC_REPORTER_TYPE_OTLP");
4650
System.setProperty(EnvironmentConfig.REPORTING_SECURE, "true");
4751
System.setProperty(EnvironmentConfig.REPORTING_CERT_FILE, "/bar/test.pem");
4852
System.setProperty(EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request", "true");
@@ -67,6 +71,12 @@ public void systemProperties() {
6771
Assertions.assertEquals("http://:-)", agentConfig.getReporting().getEndpoint().getValue());
6872
Assertions.assertEquals(
6973
TraceReporterType.OTLP, agentConfig.getReporting().getTraceReporterType());
74+
// Assert that metrics endpoint is same as reporter endpoint if not set for otlp reporter type
75+
Assertions.assertEquals(
76+
"http://:-)", agentConfig.getReporting().getMetricEndpoint().getValue());
77+
Assertions.assertEquals(
78+
MetricReporterType.METRIC_REPORTER_TYPE_OTLP,
79+
agentConfig.getReporting().getMetricReporterType());
7080
Assertions.assertEquals(512, agentConfig.getDataCapture().getBodyMaxSizeBytes().getValue());
7181
Assertions.assertEquals(true, agentConfig.getReporting().getSecure().getValue());
7282
Assertions.assertEquals("/bar/test.pem", agentConfig.getReporting().getCertFile().getValue());
@@ -79,5 +89,18 @@ public void systemProperties() {
7989
Assertions.assertEquals(
8090
StringValue.newBuilder().setValue("/path/2/jar.jar").build(),
8191
agentConfig.getJavaagent().getFilterJarPaths(1));
92+
93+
// Assert that metrics endpoint is added correctly if set
94+
System.setProperty(EnvironmentConfig.REPORTING_METRIC_ENDPOINT, "https://:-)");
95+
agentConfig = EnvironmentConfig.applyPropertiesAndEnvVars(configBuilder).build();
96+
Assertions.assertEquals(
97+
"https://:-)", agentConfig.getReporting().getMetricEndpoint().getValue());
98+
99+
// Assert that metrics reporter is disabled if ZIPKIN trace reporter is used
100+
System.setProperty(EnvironmentConfig.REPORTING_TRACE_TYPE, "ZIPKIN");
101+
agentConfig = EnvironmentConfig.applyPropertiesAndEnvVars(configBuilder).build();
102+
Assertions.assertEquals(
103+
MetricReporterType.METRIC_REPORTER_TYPE_NONE,
104+
agentConfig.getReporting().getMetricReporterType());
82105
}
83106
}

otel-extensions/src/test/java/org/hypertrace/agent/otel/extensions/config/HypertraceConfigTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import java.net.URL;
2525
import java.util.Arrays;
2626
import org.hypertrace.agent.config.v1.Config.AgentConfig;
27+
import org.hypertrace.agent.config.v1.Config.MetricReporterType;
2728
import org.hypertrace.agent.config.v1.Config.PropagationFormat;
2829
import org.hypertrace.agent.config.v1.Config.TraceReporterType;
2930
import org.junit.jupiter.api.Assertions;
3031
import org.junit.jupiter.api.Test;
3132
import org.junit.jupiter.api.io.TempDir;
3233
import org.junitpioneer.jupiter.ClearSystemProperty;
34+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
3335

3436
public class HypertraceConfigTest {
3537

@@ -41,10 +43,16 @@ public void defaultValues() throws IOException {
4143
Assertions.assertEquals("unknown", agentConfig.getServiceName().getValue());
4244
Assertions.assertEquals(
4345
TraceReporterType.OTLP, agentConfig.getReporting().getTraceReporterType());
46+
Assertions.assertEquals(
47+
MetricReporterType.METRIC_REPORTER_TYPE_OTLP,
48+
agentConfig.getReporting().getMetricReporterType());
4449
Assertions.assertFalse(agentConfig.getReporting().hasCertFile());
4550
Assertions.assertEquals(
4651
HypertraceConfig.DEFAULT_REPORTING_ENDPOINT,
4752
agentConfig.getReporting().getEndpoint().getValue());
53+
Assertions.assertEquals(
54+
HypertraceConfig.DEFAULT_REPORTING_ENDPOINT,
55+
agentConfig.getReporting().getMetricEndpoint().getValue());
4856
Assertions.assertEquals(
4957
Arrays.asList(PropagationFormat.TRACECONTEXT), agentConfig.getPropagationFormatsList());
5058
Assertions.assertEquals(false, agentConfig.getReporting().getSecure().getValue());
@@ -100,10 +108,15 @@ private void assertConfig(AgentConfig agentConfig) {
100108
Arrays.asList(PropagationFormat.B3), agentConfig.getPropagationFormatsList());
101109
Assertions.assertEquals(
102110
TraceReporterType.OTLP, agentConfig.getReporting().getTraceReporterType());
111+
Assertions.assertEquals(
112+
MetricReporterType.METRIC_REPORTER_TYPE_OTLP,
113+
agentConfig.getReporting().getMetricReporterType());
103114
Assertions.assertEquals(
104115
"/foo/bar/example.pem", agentConfig.getReporting().getCertFile().getValue());
105116
Assertions.assertEquals(
106117
"http://localhost:4317", agentConfig.getReporting().getEndpoint().getValue());
118+
Assertions.assertEquals(
119+
"http://localhost:4317", agentConfig.getReporting().getMetricEndpoint().getValue());
107120
Assertions.assertEquals(true, agentConfig.getReporting().getSecure().getValue());
108121
Assertions.assertEquals(16, agentConfig.getDataCapture().getBodyMaxSizeBytes().getValue());
109122
Assertions.assertEquals(
@@ -134,4 +147,62 @@ public void configWithSystemProps() throws IOException {
134147
"http://nowhere.here", agentConfig.getReporting().getEndpoint().getValue());
135148
Assertions.assertEquals("service", agentConfig.getServiceName().getValue());
136149
}
150+
151+
@Test
152+
@SetEnvironmentVariable(key = "HT_REPORTING_ENDPOINT", value = "http://oltp.hypertrace.org:4317")
153+
public void complexConfig() throws IOException {
154+
// GIVEN a config file with a non-default reporting endpoint and an env-var with a different
155+
// non-default otlp reporting endpoint
156+
URL resource = getClass().getClassLoader().getResource("config.yaml");
157+
// WHEN we load the config
158+
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
159+
// VERIFY the trace and metric endpoints are the both the value of the env var
160+
String expectedEndpoint = "http://oltp.hypertrace.org:4317";
161+
Assertions.assertEquals(expectedEndpoint, agentConfig.getReporting().getEndpoint().getValue());
162+
Assertions.assertEquals(
163+
expectedEndpoint, agentConfig.getReporting().getMetricEndpoint().getValue());
164+
Assertions.assertEquals(
165+
TraceReporterType.OTLP, agentConfig.getReporting().getTraceReporterType());
166+
Assertions.assertEquals(
167+
MetricReporterType.METRIC_REPORTER_TYPE_OTLP,
168+
agentConfig.getReporting().getMetricReporterType());
169+
}
170+
171+
@Test
172+
public void zipkinExporter() throws IOException {
173+
// GIVEN a config file with a non-default zipkin reporting endpoint
174+
URL resource = getClass().getClassLoader().getResource("zipkinConfig.yaml");
175+
// WHEN we load the config
176+
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
177+
// VERIFY the trace reporting endpoint is the zipkin endpoint
178+
Assertions.assertEquals(
179+
"http://example.com:9411/api/v2/spans",
180+
agentConfig.getReporting().getEndpoint().getValue());
181+
// VERIFY the trace reporting type is ZIPKIN
182+
Assertions.assertEquals(
183+
TraceReporterType.ZIPKIN, agentConfig.getReporting().getTraceReporterType());
184+
// VERIFY the metric reporting type is none
185+
Assertions.assertEquals(
186+
MetricReporterType.METRIC_REPORTER_TYPE_NONE,
187+
agentConfig.getReporting().getMetricReporterType());
188+
}
189+
190+
@Test
191+
public void noneTraceExporter() throws IOException {
192+
// GIVEN a config file with a non-default zipkin reporting endpoint
193+
URL resource = getClass().getClassLoader().getResource("noneTraceReportingConfig.yaml");
194+
// WHEN we load the config
195+
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
196+
// VERIFY the trace reporting type is NONE
197+
Assertions.assertEquals(
198+
TraceReporterType.NONE, agentConfig.getReporting().getTraceReporterType());
199+
// VERIFY the metric reporting type is OTLP
200+
Assertions.assertEquals(
201+
MetricReporterType.METRIC_REPORTER_TYPE_OTLP,
202+
agentConfig.getReporting().getMetricReporterType());
203+
// VERIFY the metric reporting endpoint is default
204+
Assertions.assertEquals(
205+
HypertraceConfig.DEFAULT_REPORTING_ENDPOINT,
206+
agentConfig.getReporting().getMetricEndpoint().getValue());
207+
}
137208
}

otel-extensions/src/test/resources/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ propagationFormats:
55
- B3
66
reporting:
77
endpoint: http://localhost:4317
8+
metric_endpoint: http://localhost:4317
89
secure: true
910
trace_reporter_type: OTLP
11+
metric_reporter_type: METRIC_REPORTER_TYPE_OTLP
1012
cert_file: /foo/bar/example.pem
1113
dataCapture:
1214
bodyMaxSizeBytes: 16
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
service_name: service
2+
reporting:
3+
trace_reporter_type: NONE
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
service_name: service
2+
reporting:
3+
trace_reporter_type: ZIPKIN
4+
endpoint: http://example.com:9411/api/v2/spans

0 commit comments

Comments
 (0)