2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import io.micrometer.observation.Observation;
30 import io.micrometer.observation.ObservationRegistry;
31 import io.opentelemetry.api.OpenTelemetry;
32 import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
33 import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
34 import jakarta.servlet.http.HttpServletRequest;
35 import java.util.Objects;
36 import org.junit.jupiter.api.Test;
37 import org.springframework.beans.BeansException;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.http.server.observation.ServerRequestObservationContext;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.util.AntPathMatcher;
48 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
49 @TestPropertySource(properties = { //
50 "server.ssl.key-store=./config/keystore.jks",
51 "app.webclient.trust-store=./config/truststore.jks",
52 "app.webclient.trust-store-used=true",
53 "app.vardata-directory=/tmp/pmstest",
56 "spring.application.name=a1-pms",
57 "management.tracing.enabled=true",
58 "management.tracing.exporter.protocol=grpc",
59 "management.tracing.sampler.jaeger_remote.endpoint=http://127.0.0.1:14250",
60 "management.tracing.propagator.type=W3C"
62 @AutoConfigureObservability
63 class OtelConfigTest {
65 @Autowired private ApplicationContext context;
67 @Autowired OtelConfig otelConfig;
69 @Autowired ObservationRegistry observationRegistry;
72 OpenTelemetry openTelemetry() {
73 return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
77 void otlpExporterGrpc() {
78 assertNotNull(otelConfig);
79 assertNotNull(otelConfig.otlpExporterGrpc());
83 void otlpExporterHttpNotActive() {
84 assertNotNull(otelConfig);
85 assertThrows(BeansException.class, () -> context.getBean(OtlpHttpSpanExporter.class));
89 void jaegerRemoteSampler() {
90 assertNotNull(otelConfig);
91 assertNotNull(otelConfig.jaegerRemoteSampler());
95 void skipActuatorEndpointsFromObservation() {
96 assertNotNull(otelConfig);
97 var actuatorCustomizer = otelConfig.skipActuatorEndpointsFromObservation();
98 assertNotNull(actuatorCustomizer);
99 Observation.Scope otelScope = Observation.Scope.NOOP;
100 observationRegistry.setCurrentObservationScope(otelScope);
101 Objects.requireNonNull(observationRegistry.getCurrentObservation()).start();
105 void observationPredicate() {
106 var antPathMatcher = new AntPathMatcher("/");
107 ServerRequestObservationContext serverRequestObservationContext =
108 mock(ServerRequestObservationContext.class);
109 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
110 when(httpServletRequest.getRequestURI()).thenReturn("/actuator/health");
111 when(serverRequestObservationContext.getCarrier()).thenReturn(httpServletRequest);
113 OtelConfig.observationPredicate(antPathMatcher)
114 .test("anything", serverRequestObservationContext);
116 when(httpServletRequest.getRequestURI()).thenReturn("/api/v1/anything");
118 OtelConfig.observationPredicate(antPathMatcher)
119 .test("anything", serverRequestObservationContext);