aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/main/java
diff options
context:
space:
mode:
authorToine Siebelink <toine.siebelink@est.tech>2024-06-05 07:11:05 +0000
committerGerrit Code Review <gerrit@onap.org>2024-06-05 07:11:05 +0000
commit026e4d2cfc38468a75c035c832b8fd04d66ce4d8 (patch)
tree19a8d90ca7cc06f5209ec0945121dc7d1730e0a5 /cps-ncmp-service/src/main/java
parentc056bee2fc0f52055464839248abfa4d16c8f89c (diff)
parent2293486de5c408c2c9c27205f1167747df6e1d42 (diff)
Merge "Added OpenTelemetry to CPS"
Diffstat (limited to 'cps-ncmp-service/src/main/java')
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/OpenTelemetryConfig.java111
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/kafka/KafkaConfig.java35
2 files changed, 146 insertions, 0 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/OpenTelemetryConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/OpenTelemetryConfig.java
new file mode 100644
index 000000000..bcbacbd42
--- /dev/null
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/OpenTelemetryConfig.java
@@ -0,0 +1,111 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * Licensed 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.impl.config;
+
+import io.micrometer.observation.ObservationPredicate;
+import io.micrometer.observation.ObservationRegistry;
+import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
+import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
+import io.opentelemetry.sdk.extension.trace.jaeger.sampler.JaegerRemoteSampler;
+import io.opentelemetry.sdk.trace.samplers.Sampler;
+import java.time.Duration;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.actuate.autoconfigure.observation.ObservationRegistryCustomizer;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.server.observation.ServerRequestObservationContext;
+import org.springframework.util.AntPathMatcher;
+import org.springframework.util.PathMatcher;
+
+@Configuration
+public class OpenTelemetryConfig {
+
+ public static final int JAEGER_REMOTE_SAMPLER_POLLING_INTERVAL_IN_SECOND = 30;
+
+ @Value("${spring.application.name:cps-application}")
+ private String serviceId;
+
+ @Value("${cps.tracing.exporter.endpoint:http://onap-otel-collector:4317}")
+ private String tracingExporterEndpointUrl;
+
+ @Value("${cps.tracing.sampler.jaeger_remote.endpoint:http://onap-otel-collector:14250}")
+ private String jaegerRemoteSamplerUrl;
+
+ /**
+ * OTLP Exporter with Grpc exporter protocol.
+ */
+ @Bean
+ @ConditionalOnExpression(
+ "${cps.tracing.enabled} && 'grpc'.equals('${cps.tracing.exporter.protocol}')")
+ public OtlpGrpcSpanExporter createOtlpExporterGrpc() {
+ return OtlpGrpcSpanExporter.builder().setEndpoint(tracingExporterEndpointUrl).build();
+ }
+
+ /**
+ * OTLP Exporter with HTTP exporter protocol.
+ */
+ @Bean
+ @ConditionalOnExpression(
+ "${cps.tracing.enabled} && 'http'.equals('${cps.tracing.exporter.protocol}')")
+ public OtlpHttpSpanExporter createOtlpExporterHttp() {
+ return OtlpHttpSpanExporter.builder().setEndpoint(tracingExporterEndpointUrl).build();
+ }
+
+ /**
+ * Jaeger Remote Sampler.
+ */
+ @Bean
+ @ConditionalOnProperty("cps.tracing.enabled")
+ public JaegerRemoteSampler createJaegerRemoteSampler() {
+ return JaegerRemoteSampler.builder()
+ .setEndpoint(jaegerRemoteSamplerUrl)
+ .setPollingInterval(Duration.ofSeconds(JAEGER_REMOTE_SAMPLER_POLLING_INTERVAL_IN_SECOND))
+ .setInitialSampler(Sampler.alwaysOff())
+ .setServiceName(serviceId)
+ .build();
+ }
+
+ /**
+ * Excluding /actuator/** endpoints.
+ */
+ @Bean
+ @ConditionalOnProperty("cps.tracing.enabled")
+ ObservationRegistryCustomizer<ObservationRegistry> skipActuatorEndpointsFromObservation() {
+ final PathMatcher pathMatcher = new AntPathMatcher("/");
+ return registry ->
+ registry.observationConfig().observationPredicate(observationPredicate(pathMatcher));
+ }
+
+ /**
+ * Excluding /actuator/** endpoints.
+ */
+ static ObservationPredicate observationPredicate(final PathMatcher pathMatcher) {
+ return (name, context) -> {
+ if (context instanceof ServerRequestObservationContext observationContext) {
+ return !pathMatcher.match("/actuator/**", observationContext.getCarrier().getRequestURI());
+ } else {
+ return true;
+ }
+ };
+ }
+} \ No newline at end of file
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/kafka/KafkaConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/kafka/KafkaConfig.java
index 167df5a98..cf6f1c5b1 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/kafka/KafkaConfig.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/kafka/KafkaConfig.java
@@ -21,10 +21,14 @@
package org.onap.cps.ncmp.api.impl.config.kafka;
import io.cloudevents.CloudEvent;
+import io.opentelemetry.instrumentation.kafkaclients.v2_6.TracingConsumerInterceptor;
+import io.opentelemetry.instrumentation.kafkaclients.v2_6.TracingProducerInterceptor;
import java.time.Duration;
import java.util.Map;
import lombok.RequiredArgsConstructor;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.context.annotation.Bean;
@@ -52,6 +56,9 @@ public class KafkaConfig<T> {
private final KafkaProperties kafkaProperties;
+ @Value("${cps.tracing.enabled:false}")
+ private boolean tracingEnabled;
+
private static final SslBundles NO_SSL = null;
/**
@@ -64,6 +71,10 @@ public class KafkaConfig<T> {
public ProducerFactory<String, T> legacyEventProducerFactory() {
final Map<String, Object> producerConfigProperties = kafkaProperties.buildProducerProperties(NO_SSL);
producerConfigProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
+ if (tracingEnabled) {
+ producerConfigProperties.put(
+ ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName());
+ }
return new DefaultKafkaProducerFactory<>(producerConfigProperties);
}
@@ -77,6 +88,10 @@ public class KafkaConfig<T> {
public ConsumerFactory<String, T> legacyEventConsumerFactory() {
final Map<String, Object> consumerConfigProperties = kafkaProperties.buildConsumerProperties(NO_SSL);
consumerConfigProperties.put("spring.deserializer.value.delegate.class", JsonDeserializer.class);
+ if (tracingEnabled) {
+ consumerConfigProperties.put(
+ ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
+ }
return new DefaultKafkaConsumerFactory<>(consumerConfigProperties);
}
@@ -90,6 +105,9 @@ public class KafkaConfig<T> {
public KafkaTemplate<String, T> legacyEventKafkaTemplate() {
final KafkaTemplate<String, T> kafkaTemplate = new KafkaTemplate<>(legacyEventProducerFactory());
kafkaTemplate.setConsumerFactory(legacyEventConsumerFactory());
+ if (tracingEnabled) {
+ kafkaTemplate.setObservationEnabled(true);
+ }
return kafkaTemplate;
}
@@ -104,6 +122,9 @@ public class KafkaConfig<T> {
new ConcurrentKafkaListenerContainerFactory<>();
containerFactory.setConsumerFactory(legacyEventConsumerFactory());
containerFactory.getContainerProperties().setAuthExceptionRetryInterval(Duration.ofSeconds(10));
+ if (tracingEnabled) {
+ containerFactory.getContainerProperties().setObservationEnabled(true);
+ }
return containerFactory;
}
@@ -116,6 +137,10 @@ public class KafkaConfig<T> {
@Bean
public ProducerFactory<String, CloudEvent> cloudEventProducerFactory() {
final Map<String, Object> producerConfigProperties = kafkaProperties.buildProducerProperties(NO_SSL);
+ if (tracingEnabled) {
+ producerConfigProperties.put(
+ ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName());
+ }
return new DefaultKafkaProducerFactory<>(producerConfigProperties);
}
@@ -128,6 +153,10 @@ public class KafkaConfig<T> {
@Bean
public ConsumerFactory<String, CloudEvent> cloudEventConsumerFactory() {
final Map<String, Object> consumerConfigProperties = kafkaProperties.buildConsumerProperties(NO_SSL);
+ if (tracingEnabled) {
+ consumerConfigProperties.put(
+ ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
+ }
return new DefaultKafkaConsumerFactory<>(consumerConfigProperties);
}
@@ -142,6 +171,9 @@ public class KafkaConfig<T> {
final KafkaTemplate<String, CloudEvent> kafkaTemplate =
new KafkaTemplate<>(cloudEventProducerFactory());
kafkaTemplate.setConsumerFactory(cloudEventConsumerFactory());
+ if (tracingEnabled) {
+ kafkaTemplate.setObservationEnabled(true);
+ }
return kafkaTemplate;
}
@@ -157,6 +189,9 @@ public class KafkaConfig<T> {
new ConcurrentKafkaListenerContainerFactory<>();
containerFactory.setConsumerFactory(cloudEventConsumerFactory());
containerFactory.getContainerProperties().setAuthExceptionRetryInterval(Duration.ofSeconds(10));
+ if (tracingEnabled) {
+ containerFactory.getContainerProperties().setObservationEnabled(true);
+ }
return containerFactory;
}