Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions content/en/docs/refguide/runtime/tracing-in-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
```