Skip to content
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
13 changes: 3 additions & 10 deletions components/camel-opentelemetry-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-telemetry</artifactId>
<artifactId>camel-support</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
Expand All @@ -60,7 +60,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring-junit5</artifactId>
<artifactId>camel-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -102,15 +102,8 @@
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
}

private class MyEndpoint extends DefaultEndpoint {
@SuppressWarnings("unused")
private final String password;
@SuppressWarnings("unused")
private final String clear;

MyEndpoint(String uri, MyComponent myComponent, Map<String, Object> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.PointData;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.opentelemetry.metrics.eventnotifier.OpenTelemetryExchangeEventNotifier;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -48,12 +54,28 @@ public class CounterRouteAutoConfigIT extends CamelTestSupport {
public static void init() {
// Open telemetry autoconfiguration using an exporter that writes to the console via logging.
// Other possible exporters include 'logging-otlp' and 'otlp'.
GlobalOpenTelemetry.resetForTest();
System.setProperty("otel.java.global-autoconfigure.enabled", "true");
System.setProperty("otel.metrics.exporter", "console");
System.setProperty("otel.traces.exporter", "none");
System.setProperty("otel.logs.exporter", "none");
System.setProperty("otel.propagators", "tracecontext");
System.setProperty("otel.metric.export.interval", "300");
System.setProperty("otel.metric.export.interval", "50");
}

@AfterEach
void cleanup() {
GlobalOpenTelemetry.resetForTest();
}

@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
// not setting any meter explicitly, relying on opentelemetry autoconfigure
OpenTelemetryExchangeEventNotifier eventNotifier = new OpenTelemetryExchangeEventNotifier();
context.getManagementStrategy().addEventNotifier(eventNotifier);
eventNotifier.init();
return context;
}

@Test
Expand All @@ -71,18 +93,24 @@ public void testIncrement() throws Exception {

List<LogRecord> logs = new ArrayList<>(handler.getLogs());
assertFalse(logs.isEmpty(), "No metrics were exported");
int dataCount = 0;
for (LogRecord log : logs) {
if (log.getParameters() != null && log.getParameters().length > 0) {
MetricData metricData = (MetricData) log.getParameters()[0];
assertEquals("B", metricData.getName());
long dataCount = logs.stream()
.map(LogRecord::getParameters)
.filter(Objects::nonNull)
.flatMap(Arrays::stream)
.filter(MetricData.class::isInstance)
.map(MetricData.class::cast)
.filter(md -> "B".equals(md.getName()))
.peek(md -> {
PointData pd = md.getData()
.getPoints()
.stream()
.findFirst()
.orElseThrow();

PointData pd = metricData.getData().getPoints().stream().findFirst().orElse(null);
assertInstanceOf(LongPointData.class, pd, "Expected LongPointData");
assertEquals(5, ((LongPointData) pd).getValue());
dataCount++;
}
}
assertInstanceOf(LongPointData.class, pd, "Expected LongPointData");
assertEquals(5, ((LongPointData) pd).getValue());
})
.count();
assertTrue(dataCount > 0, "No metric data found with name B");
MockEndpoint.assertIsSatisfied(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public List<LogRecord> getLogs() {

@Override
public void publish(LogRecord record) {
super.publish(record);
logs.add(record);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.PointData;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.opentelemetry.metrics.eventnotifier.OpenTelemetryExchangeEventNotifier;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -48,6 +54,7 @@ public class TimerRouteAutoConfigIT extends CamelTestSupport {

@BeforeAll
public static void init() {
GlobalOpenTelemetry.resetForTest();
// Open telemetry autoconfiguration using an exporter that writes to the console via logging.
// Other possible exporters include 'logging-otlp' and 'otlp'.
System.setProperty("otel.java.global-autoconfigure.enabled", "true");
Expand All @@ -58,6 +65,21 @@ public static void init() {
System.setProperty("otel.metric.export.interval", "300");
}

@AfterEach
void cleanup() {
GlobalOpenTelemetry.resetForTest();
}

@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
// not setting any meter explicitly, relying on opentelemetry autoconfigure
OpenTelemetryExchangeEventNotifier eventNotifier = new OpenTelemetryExchangeEventNotifier();
context.getManagementStrategy().addEventNotifier(eventNotifier);
eventNotifier.init();
return context;
}

@Test
public void testOverrideMetricsName() throws Exception {
Logger logger = Logger.getLogger(LoggingMetricExporter.class.getName());
Expand All @@ -74,19 +96,26 @@ public void testOverrideMetricsName() throws Exception {

List<LogRecord> logs = new ArrayList<>(handler.getLogs());
assertFalse(logs.isEmpty(), "No metrics were exported");
int dataCount = 0;
for (LogRecord log : logs) {
if (log.getParameters() != null && log.getParameters().length > 0) {
MetricData metricData = (MetricData) log.getParameters()[0];
assertEquals("A", metricData.getName());

PointData pd = metricData.getData().getPoints().stream().findFirst().orElse(null);
assertInstanceOf(HistogramPointData.class, pd, "Expected LongPointData");
assertEquals(1L, ((HistogramPointData) pd).getCount());
assertTrue(((HistogramPointData) pd).getMin() >= DELAY);
dataCount++;
}
}
long dataCount = logs.stream()
.map(LogRecord::getParameters)
.filter(Objects::nonNull)
.flatMap(Arrays::stream)
.filter(MetricData.class::isInstance)
.map(MetricData.class::cast)
.filter(md -> "A".equals(md.getName()))
.peek(md -> {
PointData pd = md.getData()
.getPoints()
.stream()
.findFirst()
.orElseThrow();
assertInstanceOf(HistogramPointData.class, pd, "Expected HistogramPointData");
HistogramPointData hpd = (HistogramPointData) pd;
assertEquals(1L, hpd.getCount());
assertTrue(hpd.getMin() >= DELAY);
})
.count();
assertTrue(dataCount > 0, "No metric data found with name A");
MockEndpoint.assertIsSatisfied(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
Expand All @@ -35,6 +36,7 @@
import org.apache.camel.opentelemetry.metrics.integration.MemoryLogHandler;
import org.apache.camel.opentelemetry.metrics.messagehistory.OpenTelemetryMessageHistoryFactory;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -52,6 +54,7 @@ public class ManagedMessageHistoryAutoConfigIT extends CamelTestSupport {
public static void init() {
// Open telemetry autoconfiguration using an exporter that writes to the console via logging.
// Other possible exporters include 'logging-otlp' and 'otlp'.
GlobalOpenTelemetry.resetForTest();
System.setProperty("otel.java.global-autoconfigure.enabled", "true");
System.setProperty("otel.metrics.exporter", "console");
System.setProperty("otel.traces.exporter", "none");
Expand All @@ -60,6 +63,11 @@ public static void init() {
System.setProperty("otel.metric.export.interval", "300");
}

@AfterEach
void cleanup() {
GlobalOpenTelemetry.resetForTest();
}

@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
Expand Down Expand Up @@ -99,8 +107,7 @@ public void testMessageHistory() throws Exception {
MetricData metricData = (MetricData) log.getParameters()[0];
assertEquals(DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME, metricData.getName());

HistogramPointData hpd = getPointDataForRouteId(metricData, "route1");
assertEquals(count / 2, hpd.getCount());
assertPointDataForRouteId(metricData, "route1");

assertTrue(verifyMetricDataHasNodeId(metricData, "route1", "foo"));
assertTrue(verifyMetricDataHasNodeId(metricData, "route2", "bar"));
Expand All @@ -118,14 +125,13 @@ private boolean verifyMetricDataHasNodeId(MetricData metricData, String routeId,
.anyMatch(point -> nodeId.equals(point.getAttributes().get(AttributeKey.stringKey("nodeId"))));
}

private HistogramPointData getPointDataForRouteId(MetricData metricData, String routeId) {
private void assertPointDataForRouteId(MetricData metricData, String routeId) {
List<PointData> pdList = metricData.getData().getPoints().stream()
.filter(point -> routeId.equals(getRouteId(point)))
.collect(Collectors.toList());
assertEquals(1, pdList.size(), "Should have one metric for routeId " + routeId);
PointData pd = pdList.get(0);
assertInstanceOf(HistogramPointData.class, pd);
return (HistogramPointData) pd;
}

protected String getRouteId(PointData pd) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
appender.file.type=File
appender.file.name=file
appender.file.fileName=target/camel-opentelemetry-test.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n

appender.out.type=Console
appender.out.name=out
appender.out.layout.type=PatternLayout
appender.out.layout.pattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n

rootLogger.level=INFO
rootLogger.appenderRefs=file
rootLogger.appenderRef.file.ref=file