@PropertyInject("instana.token")
private String iToken;
@PropertyInject(value="instana.endpoint")
private String iEndpoint;
Use Instana backend REST API
The Instana backend REST API includes the following contents:
- API for infrastructure monitoring
- API for website monitoring
- API for application monitoring
- APIs for events
- API for settings
Here is a curl command to call a simple Instana back-end service of listing all supported plugins of infrastructure monitoring:
curl -ik -H "Authorization: apiToken $TOKEN" "https://$ENDPOINT/api/infrastructure-monitoring/catalog/plugins?pretty"
Typical settings for TOKEN and ENDPOINT environment variables for a public Instana Server:
ENDPOINT=xxxx.instana.io
TOKEN=xxxxxxxx
Typical settings for TOKEN and ENDPOINT environment variables for a self-hosting Instana Server:
ENDPOINT=xxxx.yyyy.com
TOKEN=xxxxxxxx
Sample code of Camel K to call Instana REST API to get settings of Instana Application Perspectives (Note: we are using Java DSL):
from("direct:start").streamCaching()
.removeHeaders("CamelHttp*")
.setHeader("Authorization", constant("apiToken " + iToken))
.setHeader("app-id", constant(appId))
.setProperty("instana_endpoint", constant(iEndpoint))
.setBody().constant(null)
.toD("https://${exchangeProperty[instana_endpoint]}/api/application-monitoring/settings/application/"
+ "${header.app-id}")
.log("get-app-settings in: ${body}");
More considerations of the Camel K code
For self-hosting Instana, usually we do not need to verify the certificate and key file returned by the HTTPS request to the Instana server. We just use API tokens. Here is the code:
public static void configureHttpClient(CamelContext context) {
HttpComponent httpComponent = context.getComponent("https", HttpComponent.class);
httpComponent.setHttpClientConfigurer(new HttpClientConfigurer() {
@Override
public void configureHttpClient(HttpClientBuilder clientBuilder) {
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientBuilder.setSSLContext(sslContext);
final HostnameVerifier hostnameVerifier = getHostnameVerifier();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
clientBuilder.setConnectionManager(connMgr);
}
});
}
public void configure() {
configureHttpClient(getContext());
...
}
In Camel Integration, it is common to retry when there are exceptions which might be caused by temporary network or database failures. This mechanism is called Redelivery. Following is typical code to set the Redelivery policy to access Instana.
public static void handleErrorAndException(RouteBuilder route, Logger log) {
route.errorHandler(
getDefaultErrorHandler(route).useExponentialBackOff().useCollisionAvoidance().onRedelivery(exchange -> log
.warn("{} - retrying", exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage())));
route.onException(IOException.class).maximumRedeliveries(10);
}
With the code, we enable Camel Integration to retry 10 times at maximum when there is IOExeption. Collision avoidance is enabled to add some randomness to the timings. The exponential backoff is used (i.e. the time between retries increases using a backoff multiplier).
And finally, following is a sample command line to run Camel K Integration with Instana: (Note: we use Camel K version 3.2)
kamel run --name instana-controller \
--property xxx1=yyy1 \
--property xxx2=yyy2 \
--trait knative-service.enabled=false \
--trait knative-service.min-scale=1 \
--trait knative.sink-binding=false \
--configmap instana-config \
-d camel-quarkus-http \
-d camel-quarkus-kafka \
-d camel-quarkus-avro \
-d mvn:org.apache.camel.k:camel-knative-api:1.4.1 \
-d mvn:io.apicurio:apicurio-registry-rest-client:1.3.2.Final \
xxxx1.java \
xxxx2.java
Now it is time for you to create your own first Camel K integration to access Instana. Enjoy it!