Skip to content

Commit

Permalink
adjusted initial & solution
Browse files Browse the repository at this point in the history
  • Loading branch information
maeddes committed Jul 24, 2024
1 parent 81eec46 commit aab18ab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -36,9 +30,6 @@ public class TodobackendApplication {

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

private OpenTelemetry openTelemetry;
private Tracer tracer;

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

Expand All @@ -48,11 +39,6 @@ public class TodobackendApplication {
@Autowired
TodoRepository todoRepository;

public TodobackendApplication(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
tracer = openTelemetry.getTracer(TodobackendApplication.class.getName(), "0.1.0");

}

private String getInstanceId() {

Expand Down Expand Up @@ -92,35 +78,14 @@ String addTodo(HttpServletRequest request, HttpServletResponse response, @PathVa

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

Span span = tracer.spanBuilder("addTodo").setSpanKind(SpanKind.SERVER).startSpan();
this.someInternalMethod(todo);

span.setAttribute("http.method", request.getMethod());
span.setAttribute("http.url", request.getRequestURL().toString());
span.setAttribute("client.address", request.getRemoteAddr());
span.setAttribute("user.agent",request.getHeader("User-Agent"));

try (Scope scope = span.makeCurrent()) {
this.someInternalMethod(todo);
response.setStatus(HttpServletResponse.SC_CREATED);
span.setAttribute("response.status", HttpServletResponse.SC_CREATED);
} catch (Throwable t) {
span.setStatus(StatusCode.ERROR, "Error on server side!");
span.recordException(t);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
span.setAttribute("response.status", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
span.end();
}

logger.info("Span.toString():"+span.toString());
return todo;

}

String someInternalMethod(String todo){

Span childSpan = tracer.spanBuilder("someInternalMethod").setSpanKind(SpanKind.INTERNAL).startSpan();

todoRepository.save(new Todo(todo));

if(todo.equals("slow")){
Expand All @@ -137,8 +102,6 @@ String someInternalMethod(String todo){

}

logger.info("childSpan.toString():"+childSpan.toString());
childSpan.end();
return todo;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
</parent>

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

<properties>
Expand Down Expand Up @@ -55,10 +55,33 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>2.4.0</version>
<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</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>1.26.0-alpha</version>
</dependency>

</dependencies>

<build>
Expand All @@ -70,4 +93,16 @@
</plugins>
</build>

<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>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@

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.*;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
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 io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@SpringBootApplication
@RestController
Expand All @@ -30,6 +36,9 @@ public class TodobackendApplication {

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

private OpenTelemetry openTelemetry;
private Tracer tracer;

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

Expand All @@ -39,6 +48,12 @@ public class TodobackendApplication {
@Autowired
TodoRepository todoRepository;

public TodobackendApplication(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
tracer = openTelemetry.getTracer(TodobackendApplication.class.getName(), "0.1.0");

}

private String getInstanceId() {

if (!hostname.equals("not_set"))
Expand All @@ -62,88 +77,67 @@ String fail() {
}

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

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

todoRepository.findAll().forEach(todo -> todos.add(todo.getTodo()));
logger.info("GET /todos/ "+todos.toString());

logger.info("GET /todos/ " + todos.toString());

return todos;
}

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

//Context extractedContext = openTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), httpExchange, getter);
logger.info("POST /todos/ "+todo.toString());

Span span = tracer.spanBuilder("addTodo").setSpanKind(SpanKind.SERVER).startSpan();

System.out.println("Span initial:"+span.toString());
span.setAttribute("http.method", request.getMethod());
span.setAttribute("http.url", request.getRequestURL().toString());
span.setAttribute("client.address", request.getRemoteAddr());
span.setAttribute("user.agent",request.getHeader("User-Agent"));

span.setAttribute("http.method", "POST");
span.setAttribute("http.url", "/todos/{todo}");

try (Scope scope = span.makeCurrent()) {

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

response.setStatus(HttpServletResponse.SC_CREATED);
span.setAttribute("response.status", HttpServletResponse.SC_CREATED);
} catch (Throwable t) {
span.setStatus(StatusCode.ERROR, "Something bad happened!");
span.setStatus(StatusCode.ERROR, "Error on server side!");
span.recordException(t);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
span.setAttribute("response.status", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
System.out.println("Span final:"+span.toString());
span.end();
}

return "";

}

@PostMapping("/todos/{todo}")
String addTodo(@PathVariable String todo){

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

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

span.end();

logger.info("Span.toString():"+span.toString());
return todo;

}

String someInternalMethod(String todo) {
}

Span childSpan = tracer.spanBuilder("someInternalMethod")
//.setParent(Context.current().with(parentSpan))
.startSpan();
String someInternalMethod(String todo){

childSpan.setAttribute("Todo item", todo);
Span childSpan = tracer.spanBuilder("someInternalMethod").setSpanKind(SpanKind.INTERNAL).startSpan();

todoRepository.save(new Todo(todo));

childSpan.addEvent("Persisted to database");

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

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

}

}

childSpan.addEvent("Finished checking todo item");
logger.info("childSpan.toString():"+childSpan.toString());
childSpan.end();
return todo;

Expand All @@ -153,29 +147,31 @@ String someInternalMethod(String todo) {
String removeTodo(@PathVariable String todo) {

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

}

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

}

@Entity
class Todo{
class Todo {

@Id
String todo;

public Todo(){}
public Todo() {
}

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

public String getTodo(){
public String getTodo() {
return todo;
}

Expand Down

0 comments on commit aab18ab

Please sign in to comment.