Skip to content
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

Feat/java add data to all spans #12483

Merged
merged 6 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 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,38 @@
```java
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {
// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
transaction.getContexts().getTrace().getData().put("myAttribute", "myValue");
adinauer marked this conversation as resolved.
Show resolved Hide resolved
}

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() != null) {
span.getData().put("myAttribute", "myValue");
adinauer marked this conversation as resolved.
Show resolved Hide resolved
}
});

return transaction;
});
});
```
```kotlin
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {
// set the attribute on the root span
transaction.contexts.trace?.data?.let { data ->
data["myAttribute"] = "myValue"
}

// and on all child spans
transaction.spans.forEach(Consumer { span: SentrySpan ->
span.data?.let { data ->
data["myAttribute"] = "myValue"
}
})

transaction
});
});
```
38 changes: 38 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,38 @@
```java
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {
// set the attribute on the root span
if (transaction.getContexts().getTrace() == null) {
transaction.getContexts().getTrace().getData().put("myAttribute", "myValue");
}

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

return transaction;
});
});
```
```kotlin
Sentry.init(options -> {
options.setBeforeSendTransaction((transaction, hint) -> {
// set the attribute on the root span
transaction.contexts.trace?.data?.let { data ->
data["myAttribute"] = "myValue"
}

// and on all child spans
transaction.spans.forEach(Consumer { span: SentrySpan ->
span.data?.let { data ->
data["myAttribute"] = "myValue"
}
})

transaction
});
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
```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) {
transaction.getContexts().getTrace().getData().put("myAttribute", "myValue");
}

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() != null) {
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
transaction.contexts.trace?.data?.let { data ->
data["myAttribute"] = "myValue"
}

// and on all child spans
transaction.spans.forEach { span ->
span.data?.let { data ->
data["myAttribute"] = "myValue"
}
}

transaction
}
}
```
54 changes: 54 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,54 @@
```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) {
transaction.getContexts().getTrace().getData().put("myAttribute", "myValue");
}

// and on all child spans
transaction.getSpans().forEach(span -> {
if (span.getData() != null) {
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
transaction.contexts.trace?.data?.let { data ->
data["myAttribute"] = "myValue"
}

// and on all child spans
transaction.spans.forEach { span ->
span.data?.let { data ->
data["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" />
Loading