davsclaus commented on code in PR #31365:
URL: https://github.com/apache/camel/pull/31365#discussion_r4801056960
##########
components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/CounterRouteTest.java:
##########
@@ -97,6 +97,10 @@ public void configure() {
from("direct:in10")
.to("opentelemetry-metrics:counter:J?attributes.a=${body.length}&attributes.b=uri")
.to("mock:result");
+
+ from("direct:in11")
+
.to("opentelemetry-metrics:counter:K?increment=${body.missing}")
Review Comment:
`increment` is evaluated in `CounterProducer.doProcess()`, which was already
inside the `try` before this PR, so this route does not hit the code that was
moved. A failing `attributes.*`, metric name or description expression is
evaluated in `AbstractOpenTelemetryProducer.process()`, e.g.
`opentelemetry-metrics:counter:K?attributes.a=${body.missing}`.
##########
components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/CounterRouteTest.java:
##########
@@ -345,6 +349,30 @@ public void
testHeaderAttributeOverridesUriAttributeWithSameKey() throws Excepti
MockEndpoint.assertIsSatisfied(context);
}
+ @Test
+ public void testHeadersAreClearedEvenWhenExpressionFails() throws
Exception {
+ MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
+ mockEndpoint.expectedMessageCount(0);
+
+ // Set headers that should be cleared even when the expression fails
+ Map<String, Object> headers = Map.of(
+ HEADER_METRIC_NAME, "CustomName",
+ HEADER_COUNTER_INCREMENT, 100L,
+ HEADER_METRIC_ATTRIBUTES,
Attributes.of(AttributeKey.stringKey("test"), "value"),
+ "normalHeader", "keepThis");
+
+ // Verify that an exception is thrown when the expression fails
+ // (The specific exception type may vary -
RuntimeBeanExpressionException for bean access,
+ // IllegalArgumentException for null values, etc.)
+ assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeaders("direct:in11",
Map.of("count", 5), headers));
+
+ // The important part: verify that no metric was recorded despite the
headers being set
+ // (this confirms that the error occurred and headers were cleaned up
properly)
+ assertNull(findMetric("K"));
Review Comment:
This only shows that no metric was recorded, not that the `CamelMetrics*`
headers were removed. With `template.send("direct:in11", e -> {...})` you get
the failed exchange back and can assert
`out.getMessage().getHeader(HEADER_METRIC_NAME)` is null while `normalHeader`
is kept. As written, the test also passes on `main` without the fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]