Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/tidy-lamps-wander.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"posthog-server": minor
"posthog": minor
---

Add `getRemoteConfigPayload(key)` to the server SDK. It returns the parsed payload of a remote config feature flag, with no distinct id. It needs `personalApiKey`, and returns `null` and logs a warning on a failure.
2 changes: 2 additions & 0 deletions posthog-server/api/posthog-server.api
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class com/posthog/server/PostHog : com/posthog/PostHogStateless, co
public fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;)Lcom/posthog/FeatureFlagResult;
public fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;Lcom/posthog/server/PostHogFeatureFlagResultOptions;)Lcom/posthog/FeatureFlagResult;
public fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/lang/Boolean;)Lcom/posthog/FeatureFlagResult;
public fun getRemoteConfigPayload (Ljava/lang/String;)Ljava/lang/Object;
public fun group (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public fun group (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
public fun identify (Ljava/lang/String;)V
Expand Down Expand Up @@ -419,6 +420,7 @@ public abstract interface class com/posthog/server/PostHogInterface {
public abstract fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;)Lcom/posthog/FeatureFlagResult;
public abstract fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;Lcom/posthog/server/PostHogFeatureFlagResultOptions;)Lcom/posthog/FeatureFlagResult;
public abstract fun getFeatureFlagResult (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/lang/Boolean;)Lcom/posthog/FeatureFlagResult;
public abstract fun getRemoteConfigPayload (Ljava/lang/String;)Ljava/lang/Object;
public abstract fun group (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public abstract fun group (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
public abstract fun identify (Ljava/lang/String;)V
Expand Down
5 changes: 5 additions & 0 deletions posthog-server/src/main/java/com/posthog/server/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ public class PostHog : PostHogStateless(), PostHogInterface {
(featureFlags as? PostHogFeatureFlags)?.loadFeatureFlagDefinitions()
}

override fun getRemoteConfigPayload(key: String): Any? {
if (!isEnabled()) return null
return (featureFlags as? PostHogFeatureFlags)?.getRemoteConfigPayload(key)
}

override fun captureException(
exception: Throwable,
distinctId: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public class PostHogFeatureFlagEvaluations internal constructor(
* payload. The server returns payloads as JSON-encoded strings; use [getFlagPayloadAs] when
* you want the deserialized value. Does not fire any event and does not record the access.
*
* A remote config flag with an encrypted payload is not available here. Use
* [PostHogInterface.getRemoteConfigPayload].
*
* @param key Feature flag key.
* @return The raw JSON payload string, or null when absent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ public sealed interface PostHogInterface {
/**
* Returns the feature flag payload
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
*
* A remote config flag with an encrypted payload is not available here. Use [getRemoteConfigPayload].
* @param distinctId the distinctId
* @param key the Key
* @param defaultValue the default value if not found
Expand Down Expand Up @@ -773,6 +775,29 @@ public sealed interface PostHogInterface {
return evaluateFlags(distinctId = null)
}

/**
* Returns the payload of a remote config feature flag by key, parsed from JSON, with no
* distinct ID. Use it for a remote config flag, and always when the payload is encrypted:
* flag evaluation does not return such a flag, so `evaluateFlags(distinctId).getFlagPayload(key)`
* and `getFeatureFlagPayload` cannot read it. Needs [PostHogConfig.personalApiKey]. A personal
* API key returns an encrypted payload decrypted. A project secret API key returns it redacted.
* Docs https://posthog.com/docs/feature-flags/remote-config
*
* Nothing is evaluated, no `$feature_flag_called` event is sent, and the result is not cached.
* The call blocks the calling thread for the HTTP request.
*
* The server stores the payload as a JSON-encoded string. This method parses it, and parses
* again when the result is still a string. If the second parse fails, the string is returned.
* The result is a Map, List, String, Number, Boolean, or null.
*
* @param key the feature flag key
* @return the parsed payload, or null when the client is disabled, no personal API key is set,
* the flag key is invalid, the request fails, the response body is not valid JSON, or the
* flag has no payload. An empty response means the flag has no payload, and is not a failure.
* A failure is logged as a warning.
*/
public fun getRemoteConfigPayload(key: String): Any?

/**
* Reloads feature flag definitions from the server for use with local evaluation.
* Note that feature flag definitions are automatically fetched on initialization and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.posthog.server.internal

import com.google.gson.JsonParseException
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.posthog.FeatureFlagResult
import com.posthog.PostHogConfig
import com.posthog.PostHogOnFeatureFlags
Expand Down Expand Up @@ -1062,6 +1065,61 @@ internal class PostHogFeatureFlags(
config.logger.log("Fetching feature flag definitions requires a personal API key. This call will be ignored.")
}

fun getRemoteConfigPayload(flagKey: String): Any? {
val secretKey = personalApiKey
if (secretKey == null) {
config.logger.logWarning(
"Fetching a remote config payload requires a personal API key. This call will be ignored.",
)
return null
}
if (flagKey.isBlank() || flagKey == "." || flagKey == "..") {
config.logger.logWarning("The flag key '$flagKey' is not valid for a remote config payload. No request was sent.")
return null
}
val body =
try {
api.flagRemoteConfigPayload(flagKey, secretKey)
} catch (e: PostHogApiError) {
config.logger.logWarning("Loading the remote config payload for flag '$flagKey' failed with status ${e.statusCode}.")
return null
} catch (e: IOException) {
config.logger.logWarning("Loading the remote config payload for flag '$flagKey' failed: $e")
return null
} catch (e: Throwable) {
config.logger.logWarning("Loading the remote config payload for flag '$flagKey' failed unexpectedly: $e")
return null
}
// The server answers a flag that has no payload with an empty 2xx body.
if (body.isNullOrBlank()) return null
val parsed =
try {
parseStrictJson(body)
} catch (e: Throwable) {
config.logger.logWarning(
"Loading the remote config payload for flag '$flagKey' failed: the response body is not valid JSON.",
)
return null
}
if (parsed !is String) return parsed
return try {
parseStrictJson(parsed)
} catch (e: IOException) {
parsed
} catch (e: JsonParseException) {
parsed
}
}

// The shared Gson is lenient, so this reads through a strict JsonReader instead of Gson.fromJson.
@Throws(IOException::class)
private fun parseStrictJson(json: String): Any? {
val reader = JsonReader(StringReader(json))
val value = config.serializer.gson.getAdapter(Any::class.java).read(reader)
if (reader.peek() != JsonToken.END_DOCUMENT) throw IOException("Trailing data after the JSON value")
return value
}

/**
* Get the requestId from the cache for the given distinctId and groups
*/
Expand Down
Loading
Loading