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 in it. The `run` method starts and closes the span, sets the span status and handles exceptions.

```java
Core.tracing()
.createSpan("my span name")
.withAttribute("attribute key", "attribute value")
Comment on lines +181 to +182
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these placeholder values ("my span name" etc.) take spaces?
If not, might be better to have my_span_name or mySpanName as placeholders?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The span name can take spaces and in production we already have span names with spaces.
The attributes keys can also take spaces, though in production we have no attribute keys that have spaces, so it might be good to only update the attribute key to attribute_key.

.run(span -> {
// the code here will be wrapped by the span
});
```

If the flow of control is more complicated, then you can also handle the lifecycle of the span manually 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();
}
```