Skip to content

Feat/java add data to all spans #12483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions platform-includes/configuration/data-to-all-spans/android.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
```java
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {

// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
SpanContext spanContext = new SpanContext("op");
transaction.getContexts().setTrace(spanContext);
}
transaction.getContexts().getTrace().setData("myAttribute", "myValue");

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() == null) {
span.setData(new HashMap<>());
}
span.getData().put("myAttribute", "myValue");
});

return transaction;
});
});
```
```kotlin
Sentry.init { options ->
options.setBeforeSendTransaction { transaction, hint ->

// set the attribute on the root span
if (transaction.contexts.trace == null) {
transaction.contexts.setTrace(SpanContext("op"))
}
transaction.contexts.trace?.setData("myAttribute", "myValue")

// and on all child spans
transaction.spans.forEach { span ->
if (span.data == null) {
span.data = HashMap()
}
span.data?.set("myAttribute", "myValue")
}

transaction
}
}
```
45 changes: 45 additions & 0 deletions platform-includes/configuration/data-to-all-spans/java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
```java
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {

// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
SpanContext spanContext = new SpanContext("op");
transaction.getContexts().setTrace(spanContext);
}
transaction.getContexts().getTrace().setData("myAttribute", "myValue");

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() == null) {
span.setData(new HashMap<>());
}
span.getData().put("myAttribute", "myValue");
});

return transaction;
});
});
```
```kotlin
Sentry.init { options ->
options.setBeforeSendTransaction { transaction, hint ->

// set the attribute on the root span
if (transaction.contexts.trace == null) {
transaction.contexts.setTrace(SpanContext("op"))
}
transaction.contexts.trace?.setData("myAttribute", "myValue")

// and on all child spans
transaction.spans.forEach { span ->
if (span.data == null) {
span.data = HashMap()
}
span.data?.set("myAttribute", "myValue")
}

transaction
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
```java
import io.sentry.protocol.SentryTransaction;
import io.sentry.SentryOptions;
import io.sentry.Hint;
import org.springframework.stereotype.Component;

@Component
public class CustomBeforeSendTransactionCallback implements SentryOptions.BeforeSendTransactionCallback {
@Override
public SentryTransaction execute(SentryTransaction transaction, Hint hint) {

// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
SpanContext spanContext = new SpanContext("op");
transaction.getContexts().setTrace(spanContext);
}
transaction.getContexts().getTrace().setData("myAttribute", "myValue");

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() == null) {
span.setData(new HashMap<>());
}
span.getData().put("myAttribute", "myValue");
});

return transaction;
}
}
```

```kotlin
import io.sentry.protocol.SentryTransaction
import io.sentry.SentryOptions
import io.sentry.Hint
import org.springframework.stereotype.Component

@Component
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
override fun execute(transaction: SentryTransaction, hint: Hint): SentryTransaction? {

// set the attribute on the root span
if (transaction.contexts.trace == null) {
transaction.contexts.setTrace(SpanContext("op"))
}
transaction.contexts.trace?.setData("myAttribute", "myValue")

// and on all child spans
transaction.spans.forEach { span ->
if (span.data == null) {
span.data = HashMap()
}
span.data?.set("myAttribute", "myValue")
}

transaction
}
}
```
59 changes: 59 additions & 0 deletions platform-includes/configuration/data-to-all-spans/java.spring.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
```java
import io.sentry.protocol.SentryTransaction;
import io.sentry.SentryOptions;
import io.sentry.Hint;
import org.springframework.stereotype.Component;

@Component
public class CustomBeforeSendTransactionCallback implements SentryOptions.BeforeSendTransactionCallback {
@Override
public SentryTransaction execute(SentryTransaction transaction, Hint hint) {

// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
SpanContext spanContext = new SpanContext("op");
transaction.getContexts().setTrace(spanContext);
}
transaction.getContexts().getTrace().setData("myAttribute", "myValue");

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() == null) {
span.setData(new HashMap<>());
}
span.getData().put("myAttribute", "myValue");
});

return transaction;
}
}
```

```kotlin
import io.sentry.protocol.SentryTransaction
import io.sentry.SentryOptions
import io.sentry.Hint
import org.springframework.stereotype.Component

@Component
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
override fun execute(transaction: SentryTransaction, hint: Hint): SentryTransaction? {

// set the attribute on the root span
if (transaction.contexts.trace == null) {
transaction.contexts.setTrace(SpanContext("op"))
}
transaction.contexts.trace?.setData("myAttribute", "myValue")

// and on all child spans
transaction.spans.forEach { span ->
if (span.data == null) {
span.data = HashMap()
}
span.data?.set("myAttribute", "myValue")
}

transaction
}
}
```
6 changes: 6 additions & 0 deletions platform-includes/performance/improving-data/android.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ span.setData("my-data-attribute-4", listOf("value1", "value2", "value3"))
span.setData("my-data-attribute-5", listOf(42, 43, 44))
span.setData("my-data-attribute-6", listOf(true, false, true))
```

### Adding Attributes to all Spans and Transactions

To add an attribute to all spans, use the `beforeSendTransaction` callback:

<PlatformContent includePath="configuration/data-to-all-spans" />
6 changes: 6 additions & 0 deletions platform-includes/performance/improving-data/java.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ span.setData("my-data-attribute-4", listOf("value1", "value2", "value3"))
span.setData("my-data-attribute-5", listOf(42, 43, 44))
span.setData("my-data-attribute-6", listOf(true, false, true))
```

### Adding Attributes to all Spans and Transactions

To add an attribute to all spans, use the `beforeSendTransaction` callback:

<PlatformContent includePath="configuration/data-to-all-spans" />