Skip to content

Commit fa3886f

Browse files
authored
Add sample rate to baggage as well as trace in envelope header and flatten user (#2135)
* Add sample rate to baggage and trace in envelope header; flatten user * Add changelog * Use _ for baggage keys * Commit tests * Feat/traces sampler into sample rate (#2141) * Commit tests * Add sample rate from traces sampler to DSC * Do not replace null with true/false * Restore sample rate in OutboxSender * Remove fallback for sampling decision from TraceContext * Remove sample rate fallback from TracesSamplingDecision * Test more envelope header trace fields for OutboxSender * CR changes * Fix changelog * Only send userid in Dynamic Sampling Context if sendDefaultPii is true (#2147) * Skip sending userId in DSC if send default pii is off * Add changelog * Add test case
1 parent 413862e commit fa3886f

39 files changed

+819
-275
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Only send userid in Dynamic Sampling Context if sendDefaultPii is true ([#2147](https://github.com/getsentry/sentry-java/pull/2147))
8+
59
### Features
610

711
- New package `sentry-android-navigation` for AndroidX Navigation support ([#2136](https://github.com/getsentry/sentry-java/pull/2136))
812
- New package `sentry-compose` for Jetpack Compose support (Navigation) ([#2136](https://github.com/getsentry/sentry-java/pull/2136))
13+
- Add sample rate to baggage as well as trace in envelope header and flatten user ([#2135](https://github.com/getsentry/sentry-java/pull/2135))
914

1015
## 6.1.4
1116

sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.nhaarman.mockitokotlin2.whenever
66
import io.sentry.Hint
77
import io.sentry.IHub
88
import io.sentry.SentryTracer
9+
import io.sentry.TracesSamplingDecision
910
import io.sentry.TransactionContext
1011
import io.sentry.android.core.ActivityLifecycleIntegration.UI_LOAD_OP
1112
import io.sentry.protocol.MeasurementValue
@@ -21,7 +22,7 @@ class PerformanceAndroidEventProcessorTest {
2122
val options = SentryAndroidOptions()
2223

2324
val hub = mock<IHub>()
24-
val context = TransactionContext("name", "op", true)
25+
val context = TransactionContext("name", "op", TracesSamplingDecision(true))
2526
val tracer = SentryTracer(context, hub)
2627
val activityFramesTracker = mock<ActivityFramesTracker>()
2728

sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.sentry.SentryTraceHeader
1616
import io.sentry.SentryTracer
1717
import io.sentry.SpanStatus
1818
import io.sentry.TraceContext
19+
import io.sentry.TracesSamplingDecision
1920
import io.sentry.TransactionContext
2021
import io.sentry.protocol.SentryTransaction
2122
import kotlinx.coroutines.launch
@@ -200,7 +201,7 @@ class SentryApolloInterceptorTest {
200201
private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true) = runBlocking {
201202
var tx: ITransaction? = null
202203
if (isSpanActive) {
203-
tx = SentryTracer(TransactionContext("op", "desc", true), fixture.hub)
204+
tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub)
204205
whenever(fixture.hub.span).thenReturn(tx)
205206
}
206207

sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.sentry.SentryOptions
1111
import io.sentry.SentryTraceHeader
1212
import io.sentry.SentryTracer
1313
import io.sentry.SpanStatus
14+
import io.sentry.TracesSamplingDecision
1415
import io.sentry.TransactionContext
1516
import okhttp3.mockwebserver.MockResponse
1617
import okhttp3.mockwebserver.MockWebServer
@@ -28,7 +29,7 @@ class SentrySpanRestTemplateCustomizerTest {
2829
val hub = mock<IHub>()
2930
val restTemplate = RestTemplateBuilder().build()
3031
var mockServer = MockWebServer()
31-
val transaction = SentryTracer(TransactionContext("aTransaction", "op", true), hub)
32+
val transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub)
3233
internal val customizer = SentrySpanRestTemplateCustomizer(hub)
3334
val url = mockServer.url("/test/123").toString()
3435

sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.sentry.SentryOptions
1212
import io.sentry.SentryTraceHeader
1313
import io.sentry.SentryTracer
1414
import io.sentry.SpanStatus
15+
import io.sentry.TracesSamplingDecision
1516
import io.sentry.TransactionContext
1617
import okhttp3.mockwebserver.Dispatcher
1718
import okhttp3.mockwebserver.MockResponse
@@ -34,7 +35,7 @@ class SentrySpanWebClientCustomizerTest {
3435
lateinit var sentryOptions: SentryOptions
3536
val hub = mock<IHub>()
3637
var mockServer = MockWebServer()
37-
val transaction = SentryTracer(TransactionContext("aTransaction", "op", true), hub)
38+
val transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub)
3839
private val customizer = SentrySpanWebClientCustomizer(hub)
3940

4041
fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, throwIOException: Boolean = false, includeMockServerInTracingOrigins: Boolean = true): WebClient {

sentry/api/sentry.api

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public final class io/sentry/Baggage {
3030
public fun setEnvironment (Ljava/lang/String;)V
3131
public fun setPublicKey (Ljava/lang/String;)V
3232
public fun setRelease (Ljava/lang/String;)V
33+
public fun setSampleRate (Ljava/lang/String;)V
3334
public fun setTraceId (Ljava/lang/String;)V
3435
public fun setTransaction (Ljava/lang/String;)V
3536
public fun setUserId (Ljava/lang/String;)V
@@ -473,6 +474,7 @@ public abstract interface class io/sentry/ITransaction : io/sentry/ISpan {
473474
public abstract fun getEventId ()Lio/sentry/protocol/SentryId;
474475
public abstract fun getLatestActiveSpan ()Lio/sentry/Span;
475476
public abstract fun getName ()Ljava/lang/String;
477+
public abstract fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
476478
public abstract fun getSpans ()Ljava/util/List;
477479
public abstract fun isSampled ()Ljava/lang/Boolean;
478480
public abstract fun scheduleFinish ()V
@@ -660,6 +662,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
660662
public fun getLatestActiveSpan ()Lio/sentry/Span;
661663
public fun getName ()Ljava/lang/String;
662664
public fun getOperation ()Ljava/lang/String;
665+
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
663666
public fun getSpanContext ()Lio/sentry/SpanContext;
664667
public fun getSpans ()Ljava/util/List;
665668
public fun getStatus ()Lio/sentry/SpanStatus;
@@ -1386,6 +1389,7 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
13861389
public fun getLatestActiveSpan ()Lio/sentry/Span;
13871390
public fun getName ()Ljava/lang/String;
13881391
public fun getOperation ()Ljava/lang/String;
1392+
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
13891393
public fun getSpanContext ()Lio/sentry/SpanContext;
13901394
public fun getSpans ()Ljava/util/List;
13911395
public fun getStartTimestamp ()Ljava/util/Date;
@@ -1488,6 +1492,7 @@ public final class io/sentry/Span : io/sentry/ISpan {
14881492
public fun getHighPrecisionTimestamp ()Ljava/lang/Double;
14891493
public fun getOperation ()Ljava/lang/String;
14901494
public fun getParentSpanId ()Lio/sentry/SpanId;
1495+
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
14911496
public fun getSpanContext ()Lio/sentry/SpanContext;
14921497
public fun getSpanId ()Lio/sentry/SpanId;
14931498
public fun getStartTimestamp ()Ljava/util/Date;
@@ -1520,14 +1525,15 @@ public class io/sentry/SpanContext : io/sentry/JsonSerializable, io/sentry/JsonU
15201525
protected field status Lio/sentry/SpanStatus;
15211526
protected field tags Ljava/util/Map;
15221527
public fun <init> (Lio/sentry/SpanContext;)V
1523-
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Lio/sentry/SpanId;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Lio/sentry/SpanStatus;)V
1524-
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Ljava/lang/String;Lio/sentry/SpanId;Ljava/lang/Boolean;)V
1528+
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Lio/sentry/SpanId;Ljava/lang/String;Ljava/lang/String;Lio/sentry/TracesSamplingDecision;Lio/sentry/SpanStatus;)V
1529+
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Ljava/lang/String;Lio/sentry/SpanId;Lio/sentry/TracesSamplingDecision;)V
15251530
public fun <init> (Ljava/lang/String;)V
1526-
public fun <init> (Ljava/lang/String;Ljava/lang/Boolean;)V
1531+
public fun <init> (Ljava/lang/String;Lio/sentry/TracesSamplingDecision;)V
15271532
public fun getDescription ()Ljava/lang/String;
15281533
public fun getOperation ()Ljava/lang/String;
15291534
public fun getParentSpanId ()Lio/sentry/SpanId;
15301535
public fun getSampled ()Ljava/lang/Boolean;
1536+
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
15311537
public fun getSpanId ()Lio/sentry/SpanId;
15321538
public fun getStatus ()Lio/sentry/SpanStatus;
15331539
public fun getTags ()Ljava/util/Map;
@@ -1537,6 +1543,7 @@ public class io/sentry/SpanContext : io/sentry/JsonSerializable, io/sentry/JsonU
15371543
public fun setDescription (Ljava/lang/String;)V
15381544
public fun setOperation (Ljava/lang/String;)V
15391545
public fun setSampled (Ljava/lang/Boolean;)V
1546+
public fun setSamplingDecision (Lio/sentry/TracesSamplingDecision;)V
15401547
public fun setStatus (Lio/sentry/SpanStatus;)V
15411548
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
15421549
public fun setUnknown (Ljava/util/Map;)V
@@ -1619,10 +1626,12 @@ public final class io/sentry/TraceContext : io/sentry/JsonSerializable, io/sentr
16191626
public fun getEnvironment ()Ljava/lang/String;
16201627
public fun getPublicKey ()Ljava/lang/String;
16211628
public fun getRelease ()Ljava/lang/String;
1629+
public fun getSampleRate ()Ljava/lang/String;
16221630
public fun getTraceId ()Lio/sentry/protocol/SentryId;
16231631
public fun getTransaction ()Ljava/lang/String;
16241632
public fun getUnknown ()Ljava/util/Map;
1625-
public fun getUser ()Lio/sentry/TraceContext$TraceContextUser;
1633+
public fun getUserId ()Ljava/lang/String;
1634+
public fun getUserSegment ()Ljava/lang/String;
16261635
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
16271636
public fun setUnknown (Ljava/util/Map;)V
16281637
public fun toBaggage (Lio/sentry/ILogger;)Lio/sentry/Baggage;
@@ -1638,31 +1647,20 @@ public final class io/sentry/TraceContext$JsonKeys {
16381647
public static final field ENVIRONMENT Ljava/lang/String;
16391648
public static final field PUBLIC_KEY Ljava/lang/String;
16401649
public static final field RELEASE Ljava/lang/String;
1650+
public static final field SAMPLE_RATE Ljava/lang/String;
16411651
public static final field TRACE_ID Ljava/lang/String;
16421652
public static final field TRANSACTION Ljava/lang/String;
16431653
public static final field USER Ljava/lang/String;
1654+
public static final field USER_ID Ljava/lang/String;
1655+
public static final field USER_SEGMENT Ljava/lang/String;
16441656
public fun <init> ()V
16451657
}
16461658

1647-
public final class io/sentry/TraceContext$TraceContextUser : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
1648-
public fun <init> (Lio/sentry/protocol/User;)V
1649-
public fun getId ()Ljava/lang/String;
1650-
public fun getSegment ()Ljava/lang/String;
1651-
public fun getUnknown ()Ljava/util/Map;
1652-
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
1653-
public fun setUnknown (Ljava/util/Map;)V
1654-
}
1655-
1656-
public final class io/sentry/TraceContext$TraceContextUser$Deserializer : io/sentry/JsonDeserializer {
1657-
public fun <init> ()V
1658-
public fun deserialize (Lio/sentry/JsonObjectReader;Lio/sentry/ILogger;)Lio/sentry/TraceContext$TraceContextUser;
1659-
public synthetic fun deserialize (Lio/sentry/JsonObjectReader;Lio/sentry/ILogger;)Ljava/lang/Object;
1660-
}
1661-
1662-
public final class io/sentry/TraceContext$TraceContextUser$JsonKeys {
1663-
public static final field ID Ljava/lang/String;
1664-
public static final field SEGMENT Ljava/lang/String;
1665-
public fun <init> ()V
1659+
public final class io/sentry/TracesSamplingDecision {
1660+
public fun <init> (Ljava/lang/Boolean;)V
1661+
public fun <init> (Ljava/lang/Boolean;Ljava/lang/Double;)V
1662+
public fun getSampleRate ()Ljava/lang/Double;
1663+
public fun getSampled ()Ljava/lang/Boolean;
16661664
}
16671665

16681666
public final class io/sentry/TracingOrigins {
@@ -1673,10 +1671,11 @@ public final class io/sentry/TracingOrigins {
16731671

16741672
public final class io/sentry/TransactionContext : io/sentry/SpanContext {
16751673
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
1676-
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;)V
1674+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TracesSamplingDecision;)V
16771675
public static fun fromSentryTrace (Ljava/lang/String;Ljava/lang/String;Lio/sentry/SentryTraceHeader;)Lio/sentry/TransactionContext;
16781676
public fun getName ()Ljava/lang/String;
16791677
public fun getParentSampled ()Ljava/lang/Boolean;
1678+
public fun getParentSamplingDecision ()Lio/sentry/TracesSamplingDecision;
16801679
public fun setParentSampled (Ljava/lang/Boolean;)V
16811680
}
16821681

@@ -2826,6 +2825,7 @@ public final class io/sentry/protocol/SentryTransaction : io/sentry/SentryBaseEv
28262825
public fun <init> (Lio/sentry/SentryTracer;)V
28272826
public fun <init> (Ljava/lang/String;Ljava/lang/Double;Ljava/lang/Double;Ljava/util/List;Ljava/util/Map;)V
28282827
public fun getMeasurements ()Ljava/util/Map;
2828+
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
28292829
public fun getSpans ()Ljava/util/List;
28302830
public fun getStartTimestamp ()Ljava/lang/Double;
28312831
public fun getStatus ()Lio/sentry/SpanStatus;
@@ -3035,6 +3035,14 @@ public final class io/sentry/util/Platform {
30353035
public static fun isJvm ()Z
30363036
}
30373037

3038+
public final class io/sentry/util/SampleRateUtil {
3039+
public fun <init> ()V
3040+
public static fun isValidSampleRate (Ljava/lang/Double;)Z
3041+
public static fun isValidSampleRate (Ljava/lang/Double;Z)Z
3042+
public static fun isValidTracesSampleRate (Ljava/lang/Double;)Z
3043+
public static fun isValidTracesSampleRate (Ljava/lang/Double;Z)Z
3044+
}
3045+
30383046
public final class io/sentry/util/StringUtils {
30393047
public static fun byteCountToString (J)Ljava/lang/String;
30403048
public static fun calculateStringHash (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/String;

sentry/src/main/java/io/sentry/Baggage.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ private static String decode(final @NotNull String value) throws UnsupportedEnco
153153
}
154154

155155
public void setTraceId(final @Nullable String traceId) {
156-
set("sentry-traceid", traceId);
156+
set("sentry-trace_id", traceId);
157157
}
158158

159159
public void setPublicKey(final @Nullable String publicKey) {
160-
set("sentry-publickey", publicKey);
160+
set("sentry-public_key", publicKey);
161161
}
162162

163163
public void setEnvironment(final @Nullable String environment) {
@@ -169,17 +169,21 @@ public void setRelease(final @Nullable String release) {
169169
}
170170

171171
public void setUserId(final @Nullable String userId) {
172-
set("sentry-userid", userId);
172+
set("sentry-user_id", userId);
173173
}
174174

175175
public void setUserSegment(final @Nullable String userSegment) {
176-
set("sentry-usersegment", userSegment);
176+
set("sentry-user_segment", userSegment);
177177
}
178178

179179
public void setTransaction(final @Nullable String transaction) {
180180
set("sentry-transaction", transaction);
181181
}
182182

183+
public void setSampleRate(final @Nullable String sampleRate) {
184+
set("sentry-sample_rate", sampleRate);
185+
}
186+
183187
public void set(final @NotNull String key, final @Nullable String value) {
184188
this.keyValues.put(key, value);
185189
}

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ public void flush(long timeoutMillis) {
730730
} else {
731731
final SamplingContext samplingContext =
732732
new SamplingContext(transactionContext, customSamplingContext);
733-
boolean samplingDecision = tracesSampler.sample(samplingContext);
734-
transactionContext.setSampled(samplingDecision);
733+
@NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext);
734+
transactionContext.setSamplingDecision(samplingDecision);
735735

736736
transaction =
737737
new SentryTracer(
@@ -745,7 +745,7 @@ public void flush(long timeoutMillis) {
745745

746746
// The listener is called only if the transaction exists, as the transaction is needed to
747747
// stop it
748-
if (samplingDecision && options.isProfilingEnabled()) {
748+
if (samplingDecision.getSampled() && options.isProfilingEnabled()) {
749749
final ITransactionProfiler transactionProfiler = options.getTransactionProfiler();
750750
transactionProfiler.onTransactionStart(transaction);
751751
}

sentry/src/main/java/io/sentry/ITransaction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public interface ITransaction extends ISpan {
3535
@Nullable
3636
Boolean isSampled();
3737

38+
@Nullable
39+
TracesSamplingDecision getSamplingDecision();
40+
3841
/**
3942
* Returns the latest span that is not finished.
4043
*

sentry/src/main/java/io/sentry/NoOpTransaction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public void setTag(@NotNull String key, @NotNull String value) {}
136136
return null;
137137
}
138138

139+
@Override
140+
public @Nullable TracesSamplingDecision getSamplingDecision() {
141+
return null;
142+
}
143+
139144
@Override
140145
public void setData(@NotNull String key, @NotNull Object value) {}
141146

0 commit comments

Comments
 (0)