-
Notifications
You must be signed in to change notification settings - Fork 352
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
[test] Create integration test for exposing metrics to prometheus #1660
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// camel-k: language=java trait=quarkus.enabled=true trait=prometheus.enabled=true | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.LoggingLevel; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.apache.camel.component.microprofile.metrics.MicroProfileMetricsConstants; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
/** | ||
* This example registers the following metrics: | ||
* <ul> | ||
* <li>{@code camel-k-example-metrics-attempt}</code>: meters the number of calls | ||
* made to the service to process incoming events</li> | ||
* <li>{@code camel-k-example-metrics-error}</code>: meters the number of errors | ||
* corresponding to the number of events that haven't been processed</li> | ||
* <li>{@code camel-k-example-metrics-generated}</code>: meters the number of events to be processed</li> | ||
* <li>{@code camel-k-example-metrics-redelivery}</code>: meters the number of retries | ||
* made to process the events</li> | ||
* <li>{@code camel-k-example-metrics-success}</code>: meters the number of events successfully processed</li> | ||
* </ul> | ||
* The invariant being: {@code attempt = redelivery - success - error}. | ||
* <p> In addition, a ratio gauge {@code success-ratio = success / generated} is registered. | ||
* | ||
*/ | ||
@ApplicationScoped | ||
public class Metrics extends RouteBuilder { | ||
|
||
@Override | ||
public void configure() { | ||
onException() | ||
.handled(true) | ||
.maximumRedeliveries(2) | ||
.logStackTrace(false) | ||
.logExhausted(false) | ||
.log(LoggingLevel.ERROR, "Failed processing ${body}") | ||
.to("microprofile-metrics:meter:camel-k-example-metrics-redelivery?mark=2") | ||
// The 'error' meter | ||
.to("microprofile-metrics:meter:camel-k-example-metrics-error"); | ||
|
||
from("timer:stream?period=1000") | ||
.routeId("unreliable-service") | ||
.setBody(header(Exchange.TIMER_COUNTER).prepend("event #")) | ||
.log("Processing ${body}...") | ||
// The 'generated' meter | ||
.to("microprofile-metrics:meter:camel-k-example-metrics-generated") | ||
// TODO: replace with lookup by type as soon as CAMEL-15217 gets fixed | ||
// The 'attempt' meter via @Metered interceptor | ||
.bean("service") | ||
.filter(header(Exchange.REDELIVERED)) | ||
.log(LoggingLevel.WARN, "Processed ${body} after ${header.CamelRedeliveryCounter} retries") | ||
.setHeader(MicroProfileMetricsConstants.HEADER_METER_MARK, header(Exchange.REDELIVERY_COUNTER)) | ||
// The 'redelivery' meter | ||
.to("microprofile-metrics:meter:camel-k-example-metrics-redelivery") | ||
.end() | ||
.log("Successfully processed ${body}") | ||
// The 'success' meter | ||
.to("microprofile-metrics:meter:camel-k-example-metrics-success"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.github.openshift-integration</groupId> | ||
<artifactId>camel-k-example-metrics</artifactId> | ||
<version>master-SNAPSHOT</version> | ||
|
||
<name>camel-k-example-metrics</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.camel.quarkus</groupId> | ||
<artifactId>camel-quarkus-microprofile-metrics</artifactId> | ||
<version>1.0.0-CR2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>11</source> | ||
<target>11</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jboss.jandex</groupId> | ||
<artifactId>jandex-maven-plugin</artifactId> | ||
<version>1.0.7</version> | ||
<executions> | ||
<execution> | ||
<id>make-index</id> | ||
<goals> | ||
<goal>jandex</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
49 changes: 49 additions & 0 deletions
49
e2e/yaks/monitoring/app/src/main/java/org/apache/camel/integration/Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.camel.integration; | ||
|
||
import java.util.Random; | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.RuntimeExchangeException; | ||
|
||
import org.eclipse.microprofile.metrics.Gauge; | ||
import org.eclipse.microprofile.metrics.Meter; | ||
|
||
import org.eclipse.microprofile.metrics.annotation.Metered; | ||
import org.eclipse.microprofile.metrics.annotation.Metric; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.Produces; | ||
|
||
import javax.inject.Named; | ||
|
||
@Named("service") | ||
@ApplicationScoped | ||
// TODO: to be removed as soon as it's possible to add `quarkus.arc.remove-unused-beans=framework` to Quarkus build configuration in Camel K | ||
@io.quarkus.arc.Unremovable | ||
public class Service { | ||
|
||
@Metered(name = "camel-k-example-metrics-attempt", absolute = true) | ||
public void attempt(Exchange exchange) { | ||
Random rand = new Random(); | ||
if (rand.nextDouble() < 0.5) { | ||
throw new RuntimeExchangeException("Random failure", exchange); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Feature: Camel K can serve metrics to Prometheus | ||
|
||
Background: Prepare Thanos URL | ||
Given URL: https://thanos-querier.openshift-monitoring:9091 | ||
|
||
Scenario: Integration gets the message from the timer | ||
Given integration metrics is running | ||
Then integration metrics should print Successfully processed | ||
Then sleep 10000 ms | ||
|
||
Scenario: Thanos is able to serve custom microprofile annotation metrics | ||
Given HTTP request header Authorization is "Bearer TOKEN" | ||
When send GET /api/v1/query?query=application_camel_k_example_metrics_attempt_total | ||
Then verify HTTP response expressions | ||
| $.status | success | | ||
| $.data.result[0].metric.__name__ | application_camel_k_example_metrics_attempt_total | | ||
| $.data.result[0].metric.pod | @startsWith('metrics')@ | | ||
| $.data.result[0].value[1] | @isNumber()@ | | ||
And receive HTTP 200 | ||
|
||
Scenario: Thanos is able to serve custom camel microprofile metrics | ||
Given HTTP request header Authorization is "Bearer TOKEN" | ||
When send GET /api/v1/query?query=application_camel_k_example_metrics_error_total | ||
Then verify HTTP response expressions | ||
| $.status | success | | ||
| $.data.result[0].metric.__name__ | application_camel_k_example_metrics_error_total | | ||
| $.data.result[0].metric.pod | @startsWith('metrics')@ | | ||
| $.data.result[0].value[1] | @isNumber()@ | | ||
And receive HTTP 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
SOURCE_DIR=$( dirname "${BASH_SOURCE[0]}") | ||
TEST_FILE="${SOURCE_DIR}/metrics.feature" | ||
|
||
TOKEN=`kubectl config view --minify --output 'jsonpath={..token}'` | ||
sed "s/TOKEN/${TOKEN}/g" "${TEST_FILE}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# --------------------------------------------------------------------------- | ||
# 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. | ||
# --------------------------------------------------------------------------- | ||
|
||
config: | ||
namespace: | ||
temporary: true | ||
pre: | ||
- name: ObtainToken | ||
script: ./obtainToken.sh | ||
- name: installation | ||
run: | | ||
kamel install -w -n ${YAKS_NAMESPACE} | ||
kamel run --name metrics Metrics.java -w -n $YAKS_NAMESPACE --dependency github:apache:camel-k:master-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
jdk: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally the tests would not depend on Jitpack. Unfortunately, the service bean has to be compiled beforehand and provided as an external bean archive JAR, so it has to be served from a Maven repository. |
||
- openjdk11 | ||
install: | ||
- mvn clean install -f ./e2e/yaks/monitoring/app/pom.xml |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@astefanutti Any idea how we could keep this up to date ?