Skip to content

Commit a43b961

Browse files
authored
Merge pull request #85 from statsig-io/add-exposure-metadata-on-layer
add back get exposure metadata function on layer
2 parents 203fc29 + 0286e70 commit a43b961

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
kotlin.code.style=official
2-
version=0.15.0
2+
version=0.16.0

src/main/kotlin/com/statsig/sdk/Layer.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.statsig.sdk
22

3+
import com.google.gson.Gson
4+
35
typealias OnLayerExposure = (layerExposureEventData: LayerExposureEventData) -> Unit
46
internal typealias OnLayerExposureInternal = (layer: Layer, parameterName: String) -> Unit
57

@@ -10,6 +12,8 @@ class Layer internal constructor(
1012
val name: String,
1113
val ruleID: String? = null,
1214
val value: Map<String, Any>,
15+
private val secondaryExposures: ArrayList<Map<String, String>> = arrayListOf(),
16+
private val allocatedExperiment: String = "",
1317
private val onExposure: OnLayerExposureInternal? = null
1418
) {
1519

@@ -113,6 +117,20 @@ class Layer internal constructor(
113117
}
114118
}
115119

120+
/**
121+
* Legacy exposure data at the layer level. Should NOT be used in new code as this will cause over exposure.
122+
*/
123+
fun getLegacyExposureMetadata(): String {
124+
return Gson().toJson(
125+
mapOf(
126+
"config" to this.name,
127+
"ruleID" to this.ruleID,
128+
"secondaryExposures" to this.secondaryExposures,
129+
"allocatedExperiment" to this.allocatedExperiment
130+
)
131+
)
132+
}
133+
116134
private fun logParameterExposure(key: String) {
117135
onExposure?.let {
118136
it(this, key)

src/main/kotlin/com/statsig/sdk/Statsig.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class Statsig {
6565
return statsigServer.getLayerWithCustomExposureLogging(user, layerName, onExposure)
6666
}
6767

68+
suspend fun getLayerWithExposureLoggingDisabled(user: StatsigUser, layerName: String): Layer {
69+
enforceInitialized()
70+
return statsigServer.getLayerWithExposureLoggingDisabled(user, layerName)
71+
}
72+
6873
suspend fun shutdownSuspend() {
6974
statsigServer.shutdownSuspend()
7075
}
@@ -163,6 +168,15 @@ class Statsig {
163168
return statsigServer.getLayerWithCustomExposureLoggingAsync(user, layerName, onExposure)
164169
}
165170

171+
@JvmStatic
172+
fun getLayerWithExposureLoggingDisabledAsync(
173+
user: StatsigUser,
174+
layerName: String,
175+
): CompletableFuture<Layer> {
176+
enforceInitialized()
177+
return statsigServer.getLayerWithExposureLoggingDisabledAsync(user, layerName)
178+
}
179+
166180
@JvmStatic
167181
fun getExperimentInLayerForUserAsync(
168182
user: StatsigUser,

src/main/kotlin/com/statsig/sdk/StatsigMetadata.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.statsig.sdk
22

33
import java.util.Properties
44

5-
private const val VERSION = "0.15.0"
5+
private const val VERSION = "0.16.0"
66

77
internal class StatsigMetadata {
88
companion object {

src/main/kotlin/com/statsig/sdk/StatsigServer.kt

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ sealed class StatsigServer {
4545

4646
@JvmSynthetic abstract suspend fun getLayerWithCustomExposureLogging(user: StatsigUser, layerName: String, onExposure: OnLayerExposure): Layer
4747

48+
@JvmSynthetic abstract suspend fun getLayerWithExposureLoggingDisabled(user: StatsigUser, layerName: String): Layer
49+
4850
@JvmSynthetic abstract suspend fun shutdownSuspend()
4951

5052
fun logEvent(user: StatsigUser?, eventName: String) {
@@ -109,6 +111,11 @@ sealed class StatsigServer {
109111
onExposure: OnLayerExposure
110112
): CompletableFuture<Layer>
111113

114+
abstract fun getLayerWithExposureLoggingDisabledAsync(
115+
user: StatsigUser,
116+
layerName: String,
117+
): CompletableFuture<Layer>
118+
112119
/**
113120
* @deprecated
114121
* - we make no promises of support for this API
@@ -292,15 +299,23 @@ private class StatsigServerImpl(serverSecret: String, private val options: Stats
292299

293300
override suspend fun getLayer(user: StatsigUser, layerName: String): Layer {
294301
return this.errorBoundary.capture({
295-
return@capture getLayerImpl(user, layerName)
302+
return@capture getLayerImpl(user, layerName, false)
296303
}, {
297304
return@capture Layer.empty(layerName)
298305
})
299306
}
300307

301308
override suspend fun getLayerWithCustomExposureLogging(user: StatsigUser, layerName: String, onExposure: OnLayerExposure): Layer {
302309
return this.errorBoundary.capture({
303-
return@capture getLayerImpl(user, layerName, onExposure)
310+
return@capture getLayerImpl(user, layerName, false, onExposure)
311+
}, {
312+
return@capture Layer.empty(layerName)
313+
})
314+
}
315+
316+
override suspend fun getLayerWithExposureLoggingDisabled(user: StatsigUser, layerName: String): Layer {
317+
return this.errorBoundary.capture({
318+
return@capture getLayerImpl(user, layerName, true)
304319
}, {
305320
return@capture Layer.empty(layerName)
306321
})
@@ -425,6 +440,15 @@ private class StatsigServerImpl(serverSecret: String, private val options: Stats
425440
}
426441
}
427442

443+
override fun getLayerWithExposureLoggingDisabledAsync(
444+
user: StatsigUser,
445+
layerName: String,
446+
): CompletableFuture<Layer> {
447+
return statsigScope.future {
448+
return@future getLayerWithExposureLoggingDisabled(user, layerName)
449+
}
450+
}
451+
428452
/**
429453
* @deprecated
430454
* - we make no promises of support for this API
@@ -441,7 +465,7 @@ private class StatsigServerImpl(serverSecret: String, private val options: Stats
441465
logger.flush()
442466
}
443467

444-
private suspend fun getLayerImpl(user: StatsigUser, layerName: String, onExposure: OnLayerExposure? = null): Layer {
468+
private suspend fun getLayerImpl(user: StatsigUser, layerName: String, disableExposure: Boolean, onExposure: OnLayerExposure? = null): Layer {
445469
return this.errorBoundary.capture({
446470
enforceActive()
447471
val normalizedUser = normalizeUser(user)
@@ -456,10 +480,14 @@ private class StatsigServerImpl(serverSecret: String, private val options: Stats
456480
return@capture Layer(
457481
layerName,
458482
result.ruleID,
459-
value as Map<String, Any>
460-
) { layer, paramName ->
483+
value as Map<String, Any>,
484+
result.secondaryExposures,
485+
result.configDelegate ?: "",
486+
) exposureFun@{ layer, paramName ->
461487
val metadata = createLayerExposureMetadata(layer, paramName, result)
462-
488+
if (disableExposure) {
489+
return@exposureFun
490+
}
463491
if (onExposure != null) {
464492
onExposure(
465493
LayerExposureEventData(

0 commit comments

Comments
 (0)