You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: mkdocs/docs/en/documentation/tracing.md
+68-14Lines changed: 68 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -259,33 +259,87 @@ You can create a span with the current one in parent as follows:
259
259
}
260
260
```
261
261
262
-
## Get tracing
262
+
## Асинхронная трассировка
263
263
264
-
Obtain the current tracing `Span`, you can use the `getSpan` method in `OpentelemetryContext`:
264
+
In addition to spans automatically created by the framework, you can use the `Tracer` object from the container to create your own traces. The main challenge lies in correctly propagating the context `Fork` to another execution thread to ensure the trace works properly.
265
+
266
+
To create a trace for asynchronous code inherited from the current parent context, you can do the following:
265
267
266
268
===! ":fontawesome-brands-java: `Java`"
267
269
270
+
Example is shown for the `CompletableStage` asynchronous approach:
271
+
268
272
```java
269
-
var span = OpentelemetryContext.getSpan();
270
-
```
273
+
@Component
274
+
public final class MyService {
271
275
272
-
=== ":simple-kotlin: `Kotlin`"
276
+
private final io.opentelemetry.api.trace.Tracer tracer;
273
277
274
-
```kotlin
275
-
val span = OpentelemetryContext.getSpan();
276
-
```
278
+
public MyService(Tracer tracer) {
279
+
this.tracer = tracer;
280
+
}
277
281
278
-
Obtain the current trace ID, you can use the `getTraceId()` method in `OpentelemetryContext`:
282
+
public CompletionStage<String> doTraceWork() {
283
+
var ctx = ru.tinkoff.kora.common.Context.current().fork();
284
+
var otctx = OpentelemetryContext.get(ctx);
285
+
var span = tracer.spanBuilder("myOperation")
286
+
.setParent(otctx.getContext())
287
+
.startSpan();
279
288
280
-
===! ":fontawesome-brands-java: `Java`"
289
+
return CompletableFuture.supplyAsync(() -> {
290
+
OpentelemetryContext.set(ctx, otctx.add(span));
291
+
var result = doWork();
292
+
return result;
293
+
})
294
+
.whenComplete((r, e) -> {
295
+
if (e != null) {
296
+
span.recordException(e);
297
+
span.setStatus(StatusCode.ERROR, e.getMessage());
298
+
} else {
299
+
span.setStatus(StatusCode.OK);
300
+
}
301
+
span.end();
302
+
});
303
+
}
281
304
282
-
```java
283
-
var traceId = OpentelemetryContext.getTraceId();
305
+
public String doWork() {
306
+
// do some work
307
+
}
308
+
}
284
309
```
285
310
286
311
=== ":simple-kotlin: `Kotlin`"
287
312
313
+
Example is shown for the `suspend` asynchronous approach:
314
+
288
315
```kotlin
289
-
val traceId = OpentelemetryContext.getTraceId();
290
-
```
316
+
@Component
317
+
class MyService(private val tracer: io.opentelemetry.api.trace.Tracer) {
318
+
319
+
suspend fun doTraceWork(): String {
320
+
val ctx = ru.tinkoff.kora.common.Context.current()
0 commit comments