Skip to content

Commit 6cae11c

Browse files
lbloderadinauer
andauthored
Feat/java add data to all spans (#12483)
* document how to attach data to all transactions and its spans * fix import statement * fix kotlin samples, use setData on trace * fix code samples --------- Co-authored-by: Alexander Dinauer <[email protected]>
1 parent b18c595 commit 6cae11c

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```java
2+
Sentry.init(options -> {
3+
options.setBeforeSendTransaction((transaction, hint) -> {
4+
5+
// set the attribute on the root span
6+
if (transaction.getContexts().getTrace() == null) {
7+
SpanContext spanContext = new SpanContext("op");
8+
transaction.getContexts().setTrace(spanContext);
9+
}
10+
transaction.getContexts().getTrace().setData("myAttribute", "myValue");
11+
12+
// and on all child spans
13+
transaction.getSpans().forEach(span -> {
14+
if (span.getData() == null) {
15+
span.setData(new HashMap<>());
16+
}
17+
span.getData().put("myAttribute", "myValue");
18+
});
19+
20+
return transaction;
21+
});
22+
});
23+
```
24+
```kotlin
25+
Sentry.init { options ->
26+
options.setBeforeSendTransaction { transaction, hint ->
27+
28+
// set the attribute on the root span
29+
if (transaction.contexts.trace == null) {
30+
transaction.contexts.setTrace(SpanContext("op"))
31+
}
32+
transaction.contexts.trace?.setData("myAttribute", "myValue")
33+
34+
// and on all child spans
35+
transaction.spans.forEach { span ->
36+
if (span.data == null) {
37+
span.data = HashMap()
38+
}
39+
span.data?.set("myAttribute", "myValue")
40+
}
41+
42+
transaction
43+
}
44+
}
45+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```java
2+
Sentry.init(options -> {
3+
options.setBeforeSendTransaction((transaction, hint) -> {
4+
5+
// set the attribute on the root span
6+
if (transaction.getContexts().getTrace() == null) {
7+
SpanContext spanContext = new SpanContext("op");
8+
transaction.getContexts().setTrace(spanContext);
9+
}
10+
transaction.getContexts().getTrace().setData("myAttribute", "myValue");
11+
12+
// and on all child spans
13+
transaction.getSpans().forEach(span -> {
14+
if (span.getData() == null) {
15+
span.setData(new HashMap<>());
16+
}
17+
span.getData().put("myAttribute", "myValue");
18+
});
19+
20+
return transaction;
21+
});
22+
});
23+
```
24+
```kotlin
25+
Sentry.init { options ->
26+
options.setBeforeSendTransaction { transaction, hint ->
27+
28+
// set the attribute on the root span
29+
if (transaction.contexts.trace == null) {
30+
transaction.contexts.setTrace(SpanContext("op"))
31+
}
32+
transaction.contexts.trace?.setData("myAttribute", "myValue")
33+
34+
// and on all child spans
35+
transaction.spans.forEach { span ->
36+
if (span.data == null) {
37+
span.data = HashMap()
38+
}
39+
span.data?.set("myAttribute", "myValue")
40+
}
41+
42+
transaction
43+
}
44+
}
45+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
```java
2+
import io.sentry.protocol.SentryTransaction;
3+
import io.sentry.SentryOptions;
4+
import io.sentry.Hint;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class CustomBeforeSendTransactionCallback implements SentryOptions.BeforeSendTransactionCallback {
9+
@Override
10+
public SentryTransaction execute(SentryTransaction transaction, Hint hint) {
11+
12+
// set the attribute on the root span
13+
if (transaction.getContexts().getTrace() == null) {
14+
SpanContext spanContext = new SpanContext("op");
15+
transaction.getContexts().setTrace(spanContext);
16+
}
17+
transaction.getContexts().getTrace().setData("myAttribute", "myValue");
18+
19+
// and on all child spans
20+
transaction.getSpans().forEach(span -> {
21+
if (span.getData() == null) {
22+
span.setData(new HashMap<>());
23+
}
24+
span.getData().put("myAttribute", "myValue");
25+
});
26+
27+
return transaction;
28+
}
29+
}
30+
```
31+
32+
```kotlin
33+
import io.sentry.protocol.SentryTransaction
34+
import io.sentry.SentryOptions
35+
import io.sentry.Hint
36+
import org.springframework.stereotype.Component
37+
38+
@Component
39+
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
40+
override fun execute(transaction: SentryTransaction, hint: Hint): SentryTransaction? {
41+
42+
// set the attribute on the root span
43+
if (transaction.contexts.trace == null) {
44+
transaction.contexts.setTrace(SpanContext("op"))
45+
}
46+
transaction.contexts.trace?.setData("myAttribute", "myValue")
47+
48+
// and on all child spans
49+
transaction.spans.forEach { span ->
50+
if (span.data == null) {
51+
span.data = HashMap()
52+
}
53+
span.data?.set("myAttribute", "myValue")
54+
}
55+
56+
transaction
57+
}
58+
}
59+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
```java
2+
import io.sentry.protocol.SentryTransaction;
3+
import io.sentry.SentryOptions;
4+
import io.sentry.Hint;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class CustomBeforeSendTransactionCallback implements SentryOptions.BeforeSendTransactionCallback {
9+
@Override
10+
public SentryTransaction execute(SentryTransaction transaction, Hint hint) {
11+
12+
// set the attribute on the root span
13+
if (transaction.getContexts().getTrace() == null) {
14+
SpanContext spanContext = new SpanContext("op");
15+
transaction.getContexts().setTrace(spanContext);
16+
}
17+
transaction.getContexts().getTrace().setData("myAttribute", "myValue");
18+
19+
// and on all child spans
20+
transaction.getSpans().forEach(span -> {
21+
if (span.getData() == null) {
22+
span.setData(new HashMap<>());
23+
}
24+
span.getData().put("myAttribute", "myValue");
25+
});
26+
27+
return transaction;
28+
}
29+
}
30+
```
31+
32+
```kotlin
33+
import io.sentry.protocol.SentryTransaction
34+
import io.sentry.SentryOptions
35+
import io.sentry.Hint
36+
import org.springframework.stereotype.Component
37+
38+
@Component
39+
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
40+
override fun execute(transaction: SentryTransaction, hint: Hint): SentryTransaction? {
41+
42+
// set the attribute on the root span
43+
if (transaction.contexts.trace == null) {
44+
transaction.contexts.setTrace(SpanContext("op"))
45+
}
46+
transaction.contexts.trace?.setData("myAttribute", "myValue")
47+
48+
// and on all child spans
49+
transaction.spans.forEach { span ->
50+
if (span.data == null) {
51+
span.data = HashMap()
52+
}
53+
span.data?.set("myAttribute", "myValue")
54+
}
55+
56+
transaction
57+
}
58+
}
59+
```

platform-includes/performance/improving-data/android.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ span.setData("my-data-attribute-4", listOf("value1", "value2", "value3"))
4949
span.setData("my-data-attribute-5", listOf(42, 43, 44))
5050
span.setData("my-data-attribute-6", listOf(true, false, true))
5151
```
52+
53+
### Adding Attributes to all Spans and Transactions
54+
55+
To add an attribute to all spans, use the `beforeSendTransaction` callback:
56+
57+
<PlatformContent includePath="configuration/data-to-all-spans" />

platform-includes/performance/improving-data/java.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ span.setData("my-data-attribute-4", listOf("value1", "value2", "value3"))
4949
span.setData("my-data-attribute-5", listOf(42, 43, 44))
5050
span.setData("my-data-attribute-6", listOf(true, false, true))
5151
```
52+
53+
### Adding Attributes to all Spans and Transactions
54+
55+
To add an attribute to all spans, use the `beforeSendTransaction` callback:
56+
57+
<PlatformContent includePath="configuration/data-to-all-spans" />

0 commit comments

Comments
 (0)