Add tracing to cnf-adapter 54/141354/1
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Mon, 23 Jun 2025 08:54:31 +0000 (10:54 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Mon, 23 Jun 2025 09:17:19 +0000 (11:17 +0200)
- add dependencyManagement for spring-cloud
- add tracing dependencies and properties
- define TracingTest that is not yet working due
  to context issues

Issue-ID: SO-4177
Change-Id: I744da93c5d63f7b9d0dd049d72ae00c6b0ec1c3b
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
so-cnf-adapter-application/pom.xml
so-cnf-adapter-application/src/main/resources/application.yaml
so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/TracingTest.java [new file with mode: 0644]
so-cnf-adapter-application/src/test/resources/application.properties [deleted file]
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/pom.xml

index b82a62f..5783ecb 100755 (executable)
           </execution>
         </executions>
       </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-jar-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>original</id>
-                    </execution>
-                </executions>
-            </plugin>
-            <plugin>
-                <groupId>org.jacoco</groupId>
-                <artifactId>jacoco-maven-plugin</artifactId>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-surefire-plugin</artifactId>
-                <configuration>
-                    <systemPropertyVariables>
-                        <so.log.level>DEBUG</so.log.level>
-                    </systemPropertyVariables>
-                    <rerunFailingTestsCount>2</rerunFailingTestsCount>
-                    <parallel>suites</parallel>
-                    <useUnlimitedThreads>false</useUnlimitedThreads>
-                    <threadCount>1</threadCount>
-                </configuration>
-            </plugin>
-        </plugins>
-        <resources>
-            <resource>
-                <directory>src/main/resources</directory>
-                <filtering>true</filtering>
-                <excludes>
-                    <exclude>**/*.p12</exclude>
-                    <exclude>**/*.jks</exclude>
-                </excludes>
-            </resource>
-            <resource>
-                <directory>src/main/resources</directory>
-                <filtering>false</filtering>
-                <includes>
-                    <include>**/*.p12</include>
-                    <include>**/*.jks</include>
-                </includes>
-            </resource>
-        </resources>
+      <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-jar-plugin</artifactId>
+          <executions>
+              <execution>
+                  <id>original</id>
+              </execution>
+          </executions>
+      </plugin>
+      <plugin>
+          <groupId>org.jacoco</groupId>
+          <artifactId>jacoco-maven-plugin</artifactId>
+      </plugin>
+      <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <configuration>
+              <systemPropertyVariables>
+                  <so.log.level>DEBUG</so.log.level>
+              </systemPropertyVariables>
+              <rerunFailingTestsCount>2</rerunFailingTestsCount>
+              <parallel>suites</parallel>
+              <useUnlimitedThreads>false</useUnlimitedThreads>
+              <threadCount>1</threadCount>
+          </configuration>
+      </plugin>
+    </plugins>
+    <resources>
+      <resource>
+          <directory>src/main/resources</directory>
+          <filtering>true</filtering>
+          <excludes>
+              <exclude>**/*.p12</exclude>
+              <exclude>**/*.jks</exclude>
+          </excludes>
+      </resource>
+      <resource>
+          <directory>src/main/resources</directory>
+          <filtering>false</filtering>
+          <includes>
+              <include>**/*.p12</include>
+              <include>**/*.jks</include>
+          </includes>
+      </resource>
+    </resources>
   </build>
   <dependencyManagement>
-        <dependencies>
-            <dependency>
-                <groupId>org.onap.aai.schema-service</groupId>
-                <artifactId>aai-schema</artifactId>
-                <version>1.9.2</version>
-            </dependency>
-        </dependencies>
+    <dependencies>
+      <dependency>
+        <groupId>org.onap.aai.schema-service</groupId>
+        <artifactId>aai-schema</artifactId>
+        <version>1.9.2</version>
+      </dependency>
+      <dependency>
+        <groupId>org.springframework.cloud</groupId>
+        <artifactId>spring-cloud-dependencies</artifactId>
+        <version>Hoxton.SR12</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+    </dependencies>
   </dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.springframework.cloud</groupId>
+      <artifactId>spring-cloud-starter-sleuth</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.cloud</groupId>
+      <artifactId>spring-cloud-sleuth-zipkin</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-contract-wiremock</artifactId>
+      <version>2.2.8.RELEASE</version>
       <scope>test</scope>
     </dependency>
     <dependency>
index 098ca64..dd36cb7 100644 (file)
@@ -27,11 +27,25 @@ aai:
   enabled: false
 
 spring:
+  application:
+    name: so-cnf-adapter
+  main:
+    allow-bean-definition-overriding: true
   security:
     usercredentials:
       - username: ${ACTUATOR_USERNAME}
         password: ${ACTUATOR_PASSWORD}
         role: ACTUATOR
+  sleuth:
+    enabled: ${TRACING_ENABLED:false}
+    trace-id128: true
+    sampler:
+      probability: ${TRACING_SAMPLING_PROBABILITY:1.0}
+    supports-join: false
+    propagation:
+      type: w3c,bc
+    web:
+      skip-pattern: ${TRACING_SKIP_PATTERN}
 
  # jpa:
  #   show-sql: false
@@ -63,4 +77,4 @@ multicloud:
 #management:
 #  security:
  #   enabled: false
- #   basic:
\ No newline at end of file
+ #   basic:
diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/TracingTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/TracingTest.java
new file mode 100644 (file)
index 0000000..fbdfe5d
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright © 2025 Deutsche Telekom
+ *
+ * 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.
+ */
+package org.onap.so.adapters.cnf.rest;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
+import org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration;
+import org.springframework.http.HttpStatus;
+import org.springframework.test.context.DynamicPropertyRegistry;
+import org.springframework.test.context.DynamicPropertySource;
+import org.springframework.util.SocketUtils;
+import org.springframework.web.client.RestClientException;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.DefaultUriBuilderFactory;
+
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+
+import lombok.SneakyThrows;
+
+@Ignore
+@EnableAutoConfiguration
+@ImportAutoConfiguration(classes = { TraceAutoConfiguration.class, ZipkinAutoConfiguration.class })
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
+    "spring.sleuth.enabled=true",
+    "spring.sleuth.sampler.probability=1.0"
+})
+public class TracingTest {
+
+  private static int wireMockPort = SocketUtils.findAvailableTcpPort();
+
+  @Rule
+  public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(wireMockPort));
+
+  @LocalServerPort
+  private int port;
+
+  RestTemplate restTemplate;
+
+  @Before
+  public void setup() {
+    this.restTemplate = new RestTemplate();
+    restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:" + port));
+  }
+
+  @DynamicPropertySource
+  static void configureProperties(DynamicPropertyRegistry registry) {
+    registry.add("spring.sleuth.enabled", () -> "true");
+    registry.add("spring.zipkin.baseUrl", () -> "http://localhost:" + wireMockPort);
+    registry.add("spring.sleuth.sampler.probability", () -> "1.0");
+  }
+
+  @Test
+  @SneakyThrows
+  public void thatTracesAreExported() throws InterruptedException {
+    WireMock.stubFor(WireMock.post(WireMock.urlEqualTo("/api/v2/spans"))
+        .willReturn(WireMock.aResponse().withStatus(HttpStatus.OK.value())));
+
+    try {
+      restTemplate.getForObject("http://localhost:" + port + "/foo", String.class);
+    } catch (RestClientException e) {
+      // this provokes a 404. For the test it's not important what is returned here
+    }
+
+    Thread.sleep(1000);
+    WireMock.verify(WireMock.postRequestedFor(WireMock.urlEqualTo("/api/v2/spans")));
+  }
+}
diff --git a/so-cnf-adapter-application/src/test/resources/application.properties b/so-cnf-adapter-application/src/test/resources/application.properties
deleted file mode 100644 (file)
index cbbfef1..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-aai.enabled=true
-aai.endpoint=http://localhost:8443
-aai.auth=5E12ACACBD552A415E081E29F2C4772F9835792A51C766CCFDD7433DB5220B59969CB2798C
-mso.msoKey=07a7159d3bf51a0e53be7a8f89699be7
-spring.main.allow-bean-definition-overriding=true
\ No newline at end of file
index 447577e..8e58cfe 100644 (file)
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-contract-wiremock</artifactId>
+            <version>2.2.8.RELEASE</version>
             <scope>test</scope>
         </dependency>
         <dependency>
                     <groupId>org.apache.tomcat</groupId>
                     <artifactId>tomcat-catalina</artifactId>
                 </exclusion>
+                <exclusion>
+                    <groupId>org.springframework.cloud</groupId>
+                    <artifactId>spring-cloud-starter-sleuth</artifactId>
+                </exclusion>
             </exclusions>
         </dependency>
         <dependency>