Skip to content

Commit

Permalink
Poc OTEL listener
Browse files Browse the repository at this point in the history
  • Loading branch information
kagkarlsson committed Jul 3, 2024
1 parent af6fcbf commit f065ad7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
12 changes: 12 additions & 0 deletions db-scheduler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.39.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -128,6 +135,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<optional>true</optional>
</dependency>


<!-- Test -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) Gustav Karlsson
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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 com.github.kagkarlsson.scheduler.event;

import com.github.kagkarlsson.scheduler.CurrentlyExecuting;
import com.github.kagkarlsson.scheduler.task.ExecutionComplete;
import com.github.kagkarlsson.scheduler.task.ExecutionComplete.Result;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;

public class OtelTracingListener extends AbstractSchedulerListener {

private final OpenTelemetry openTelemetry;

public OtelTracingListener(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}

@Override
public void onExecutionStart(CurrentlyExecuting currentlyExecuting) {
Tracer t = openTelemetry.getTracer("db-scheduler.execution");
TaskInstance<?> taskInstance = currentlyExecuting.getTaskInstance();
Span span =
t.spanBuilder("execute")
.setAttribute("task-name", taskInstance.getTaskName())
.setAttribute("task-instance-id", taskInstance.getId())
.startSpan();
span.makeCurrent();
}

@Override
public void onExecutionComplete(ExecutionComplete executionComplete) {
Span currentSpan = Span.current();

try {
if (executionComplete.getResult() == Result.FAILED) {
executionComplete.getCause().ifPresent(currentSpan::recordException);
}
} finally {
currentSpan.end();
}
}
}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
<version>3.12.0</version>
<scope>test</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit f065ad7

Please sign in to comment.