Skip to content

Commit

Permalink
OTLP export
Browse files Browse the repository at this point in the history
  • Loading branch information
maeddes committed Jul 18, 2024
1 parent 5dbd8c5 commit cd0a5a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,22 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<!-- Not managed by opentelemetry-bom -->
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>1.26.0-alpha</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package io.novatec.todobackend;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

//Basic Otel
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
//Tracing and Spans
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.ResourceAttributes;


Expand All @@ -19,18 +25,29 @@
public class OpenTelemetryConfiguration {

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public OpenTelemetry openTelemetry(){

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

OtlpGrpcSpanExporter jaegerOtlpExporter =
OtlpGrpcSpanExporter.builder()
.setEndpoint("http://localhost:4317")
.setTimeout(30, TimeUnit.SECONDS)
.build();

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
.addSpanProcessor(SimpleSpanProcessor.create(jaegerOtlpExporter))
// .addSpanProcessor(BatchSpanProcessor.builder(LoggingSpanExporter.create()).build()) // same results for now
.setResource(resource)
.build();
.build();
// .buildAndRegisterGlobal();


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

return openTelemetry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.opentelemetry.api.trace.Tracer;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Scope;

import jakarta.persistence.Entity;
Expand Down Expand Up @@ -86,21 +87,29 @@ String addTodo(@PathVariable String todo) {

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

System.out.println("Span initial:"+span.toString());

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

System.out.println("Span with attribute:"+span.toString());

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

this.someInternalMethod(todo);
// todoRepository.save(new Todo(todo));
logger.info("POST /todos/ " + todo.toString());

return todo;

} catch (Throwable t) {
span.setStatus(StatusCode.ERROR, "Something bad happened!");
span.recordException(t);
throw t;
} finally {
System.out.println("Span before completion:"+span.toString());
span.end();
}

return "";

}

String someInternalMethod(String todo) {
Expand Down

0 comments on commit cd0a5a1

Please sign in to comment.