Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<groupId>io.novatec</groupId>
<artifactId>todobackend-manual</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>todobackend-manual</name>
<description>Novatec Demo Application</description>

<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>1.29.0-alpha</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.40.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>todobackend-${version}</finalName>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.novatec.todobackend;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.semconv.ServiceAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenTelemetryConfiguration {

@Bean
public OpenTelemetry openTelemetry() {

Resource resource = Resource.getDefault().toBuilder()
.put(ServiceAttributes.SERVICE_NAME, "todobackend")
.put(ServiceAttributes.SERVICE_VERSION, "0.1.0")
.build();

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
.setResource(resource)
.build();

OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.build();

return openTelemetry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package io.novatec.todobackend;

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@SpringBootApplication
@RestController
@CrossOrigin(origins = "*")

public class TodobackendApplication {

private Logger logger = LoggerFactory.getLogger(TodobackendApplication.class);

private Tracer tracer;

@Value("${HOSTNAME:not_set}")
String hostname;

@Value("${spring.profiles.active: none}")
String profile;

@Autowired
TodoRepository todoRepository;

@Autowired
public TodobackendApplication(OpenTelemetry openTelemetry) {

this.tracer = openTelemetry.getTracer(TodobackendApplication.class.getName(), "0.1.0");
}


private String getInstanceId() {

if (!hostname.equals("not_set"))
return hostname;
return "probably localhost";
}

@GetMapping("/hello")
String hello() {

return getInstanceId() + " Hallo, Welt ! ";
}

@GetMapping("/fail")
String fail() {

System.exit(1);
return "fixed!";
}

@GetMapping("/todos/")
List<String> getTodos() {

Span span = tracer.spanBuilder("getTodos").startSpan();

List<String> todos = new ArrayList<>();

try (Scope scope = span.makeCurrent()) {
todoRepository.findAll().forEach(todo -> todos.add(todo.getTodo()));
logger.info("GET /todos/ " + todos.toString());

} finally {
span.end();
}

return todos;
}

@PostMapping("/todos/{todo}")
String addTodo(HttpServletRequest request, HttpServletResponse response, @PathVariable String todo){

this.someInternalMethod(todo);

logger.info("POST /todos/ "+todo.toString());

return todo;
}

String someInternalMethod(String todo){

todoRepository.save(new Todo(todo));

if(todo.equals("slow")){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(todo.equals("fail")){

System.out.println("Failing ...");
throw new RuntimeException();
}

return todo;
}

boolean isValid(String todo) {

return todo != null && !todo.isBlank();
}

double getCpuLoad() {

OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
return osBean.getCpuLoad() * 100;
}

@DeleteMapping("/todos/{todo}")
String removeTodo(@PathVariable String todo) {

todoRepository.deleteById(todo);
logger.info("DELETE /todos/ " + todo.toString());
return "removed " + todo;
}

public static void main(String[] args) {
SpringApplication.run(TodobackendApplication.class, args);
}

}

@Entity
class Todo {

@Id
String todo;

public Todo() {
}

public Todo(String todo) {
this.todo = todo;
}

public String getTodo() {
return todo;
}

public void setTodo(String todo) {
this.todo = todo;
}

}

interface TodoRepository extends CrudRepository<Todo, String> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="console"/>
</root>

</configuration>
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading