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

[test] Create integration test for exposing metrics to prometheus #1660

Merged
merged 2 commits into from
Aug 19, 2020
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
3 changes: 2 additions & 1 deletion .github/workflows/knative.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ jobs:
export CAMEL_K_TEST_IMAGE_NAME=${CAMEL_K_REGISTRY}:5000/apache/camel-k
export CAMEL_K_TEST_IMAGE_VERSION=$(make version)

yaks test e2e/yaks
yaks test e2e/yaks/knative
yaks test e2e/yaks/knative-sinkbinding
77 changes: 77 additions & 0 deletions e2e/yaks/monitoring/Metrics.java
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");
}
}
52 changes: 52 additions & 0 deletions e2e/yaks/monitoring/app/pom.xml
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>
Copy link
Contributor Author

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 ?

</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>
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);
}
}
}
29 changes: 29 additions & 0 deletions e2e/yaks/monitoring/metrics.feature
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
7 changes: 7 additions & 0 deletions e2e/yaks/monitoring/obtainToken.sh
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}"
27 changes: 27 additions & 0 deletions e2e/yaks/monitoring/yaks-config.yaml
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
4 changes: 4 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jdk:
Copy link
Member

Choose a reason for hiding this comment

The 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