diff --git a/content/en/docs/refguide/runtime/tracing-in-runtime.md b/content/en/docs/refguide/runtime/tracing-in-runtime.md index b97cc97de5f..8a6b18b0a92 100644 --- a/content/en/docs/refguide/runtime/tracing-in-runtime.md +++ b/content/en/docs/refguide/runtime/tracing-in-runtime.md @@ -165,3 +165,39 @@ You can also collect metrics data (CPU load, memory, etc.) and logs using OpenTe * See the [OpenTelemetry](/refguide/metrics/#opentelemetry) section of *Metrics* for a guide on how to setup metrics with OpenTelemetry. * See [Request to Create New Log Subscriber in Open Telemetry Format](/refguide/monitoring-mendix-runtime/#new-log-sub-opentelemetry) in *Monitoring Mendix Runtime* for a guide on how to setup logs with OpenTelemetry. + +## Custom Spans in Java Actions + +{{% alert color="info" %}} +Custom spans in Java actions was introduced in Mendix 11.10.0. +{{% /alert %}} + +Custom spans can be created in Java actions using the `Core.tracing()` API. + +Below is an example of how to create a span and wrap some code with it. The `run` method will start and close the span, set the span status and handle exceptions. + +```java +Core.tracing() + .createSpan("my span name") + .withAttribute("attribute key", "attribute value") + .run(span -> { + // the code here will be wrapped by the span + }); +``` + +If the flow of control is more complicated then you can also manually handle the lifecycle of the span using the `start` and `close` methods. + +```java +var span = Core.tracing() + .createSpan("my span name") + .withAttribute("attribute key", "attribute value") + .start(); +try { + // your code + span.setStatus(Span.Status.OK); +} catch (Throwable exc) { + span.setError(exc); +} finally { + span.close(); +} +```