Skip to content

Commit

Permalink
Span Events Java Documentation (#25157)
Browse files Browse the repository at this point in the history
* Java documentation

* Updating code snippet after testing latest release

* Applying comments and updating keeping other languages consistent
  • Loading branch information
link04 authored Oct 4, 2024
1 parent c6327cd commit dd5b791
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ catch(Exception e)

## Adding span events

_Minimum SDK version: 2.53.0._
<div class="alert alert-info">Adding span events requires SDK version 2.53.0 or higher.</div>

You can add span events using the `AddEvent` API. This method requires an `ActivityEvent`constructed with the `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.

- **Name** [_required_]: A string representing the event's name.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects a `DateTimeOffset` object.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects a `DateTimeOffset` object.
- **Attributes** [_optional_]: Zero or more key-value pairs with the following properties:
- The key must be a non-empty string.
- The value can be either:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ span.End()
## Adding span events
_Minimum SDK version: 1.67.0._
<div class="alert alert-info">Adding span events requires SDK version 1.67.0 or higher.</div>
You can add span events using the `AddEvent` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.
Expand All @@ -160,7 +160,7 @@ You can add span events using the `AddEvent` API. This method requires a `name`
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects a `Time` object.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects a `Time` object.
In the following example, `oteltrace` is an alias for the go.opentelemetry.io/otel/trace package and `attribute` refers to the go.opentelemetry.io/otel/attribute package. These packages must be imported in order to use this example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,52 @@ public class Example {
}
```

## Adding span events

<div class="alert alert-info">Adding span events requires SDK version 1.40.0 or higher.</div>

You can add span events using the `addEvent` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.

- **Name** [_required_]: A string representing the event's name.
- **Attributes** [_optional_]: Zero or more key-value pairs with the following properties:
- The key must be a non-empty string.
- The value can be either:
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects an `Instant` object.

The following examples demonstrate different ways to add events to a span:

```java
Attributes eventAttributes = Attributes.builder()
.put(AttributeKey.longKey("int_val"), 1L)
.put(AttributeKey.stringKey("string_val"), "two")
.put(AttributeKey.longArrayKey("int_array"), Arrays.asList(3L, 4L))
.put(AttributeKey.stringArrayKey("string_array"), Arrays.asList("5", "6"))
.put(AttributeKey.booleanArrayKey("bool_array"), Arrays.asList(true, false))
.build();

span.addEvent("Event With No Attributes");
span.addEvent("Event With Some Attributes", eventAttributes);
```

Read the [OpenTelemetry][21] specification for more information.

### Recording exceptions

To record exceptions, use the `recordException` API. This method requires an `exception` parameter and optionally accepts a UNIX `timestamp` parameter. It creates a new span event that includes standardized exception attributes and associates it with the corresponding span.

The following examples demonstrate different ways to record exceptions:

```java
span.recordException(new Exception("Error Message"));
span.recordException(new Exception("Error Message"),
Attributes.builder().put(AttributeKey.stringKey("status"), "failed").build());
```

Read the [OpenTelemetry][22] specification for more information.

## Trace client and Agent configuration

Both the tracing client and Datadog Agent offer additional configuration options for context propagation. You can also exclude specific resources from sending traces to Datadog if you don't want those traces to be included in calculated metrics, such as traces related to health checks.
Expand All @@ -203,3 +249,5 @@ Traces can be excluded based on their resource name, to remove synthetic traffic
[18]: /tracing/trace_collection/trace_context_propagation/
[19]: /tracing/security
[20]: /tracing/guide/ignoring_apm_resources/
[21]: https://opentelemetry.io/docs/specs/otel/trace/api/#add-events
[22]: https://opentelemetry.io/docs/specs/otel/trace/api/#record-exception
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function performTask(iterations, param1, param2) {
## Adding span events
_Minimum SDK version: 5.17.0 & 4.41.0._
<div class="alert alert-info">Adding span events requires SDK version 5.17.0/4.41.0 or higher.</div>
You can add span events using the `addEvent` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.
Expand All @@ -114,7 +114,7 @@ You can add span events using the `addEvent` API. This method requires a `name`
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects a `TimeInput` object.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects a `TimeInput` object.
The following examples demonstrate different ways to add events to a span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ $span->end();

## Adding span events

_Minimum SDK version: 1.3.0._
<div class="alert alert-info">Adding span events requires SDK version 1.3.0 or higher.</div>

You can add span events using the `addEvent` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.

Expand All @@ -114,7 +114,7 @@ You can add span events using the `addEvent` API. This method requires a `name`
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects `nanoseconds`.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects `nanoseconds`.

The following examples demonstrate different ways to add events to a span:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ current_span.set_attribute("attribute_key1", 1)

## Adding span events

_Minimum SDK version: 2.9.0._
<div class="alert alert-info">Adding span events requires SDK version 2.9.0 or higher.</div>

You can add span events using the `add_event` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.

Expand All @@ -81,7 +81,7 @@ You can add span events using the `add_event` API. This method requires a `name`
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects `microseconds`.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects `microseconds`.

The following examples demonstrate different ways to add events to a span:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Datadog combines these OpenTelemetry spans with other Datadog APM spans into a s

## Adding span events

_Minimum SDK version: 2.3.0._
<div class="alert alert-info">Adding span events requires SDK version 2.3.0 or higher.</div>

You can add span events using the `add_event` API. This method requires a `name` parameter and optionally accepts `attributes` and `timestamp` parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.

Expand All @@ -82,7 +82,7 @@ You can add span events using the `add_event` API. This method requires a `name`
- A primitive type: string, Boolean, or number.
- A homogeneous array of primitive type values (for example, an array of strings).
- Nested arrays and arrays containing elements of different data types are not allowed.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time, expects `seconds(Float)`.
- **Timestamp** [_optional_]: A UNIX timestamp representing the event's occurrence time. Expects `seconds(Float)`.

The following examples demonstrate different ways to add events to a span:

Expand Down

0 comments on commit dd5b791

Please sign in to comment.