sdk

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Diverge SDK

Provides context propagation and resource isolation helpers for Diverge's async routing (Temporal, Kafka, and Redis/Valkey). When a service in a preview environment calls Temporal, publishes to Kafka, or accesses shared Redis/Valkey caches, the routing header (x-diverge-env) and key prefixes propagate so preview traffic stays isolated from production.

Usage

Temporal Worker Setup
import "github.com/divergedev/diverge/pkg/sdk/temporal"

w := worker.New(c, temporal.TaskQueue("my-queue"), worker.Options{
    ContextPropagators: []workflow.ContextPropagator{
        temporal.NewContextPropagator(),
    },
})
Kafka Producer
import "github.com/divergedev/diverge/pkg/sdk/kafka"

topic, _ := kafka.Topic("my-topic", os.Getenv("DIVERGE_ENV"))
headers := kafka.Headers() // returns map[string]string with x-diverge-env
Redis / Valkey Key Prefix Isolation
import "github.com/divergedev/diverge/pkg/sdk"

key := sdk.ResolveRedisKey(ctx, "session:user-123")
// Returns "preview:pr-42:session:user-123" in preview environments, or "session:user-123" in baseline

Documentation

Index

Constants

View Source
const BinaryHeaderKey = "x-diverge-context-bin"

BinaryHeaderKey is the HTTP header key used to propagate the binary context.

View Source
const DefaultHeaderKey = "x-diverge-env"

DefaultHeaderKey is used to extract the preview environment name from the HTTP header.

Variables

View Source
var EnvContextKey = envContextKey{}

EnvContextKey is the key used to store the environment name in a context.

Functions

func DecodePropagationContext added in v0.7.0

func DecodePropagationContext(encoded string) (*divergev1alpha1.PropagationContext, error)

DecodePropagationContext decodes a base64 string and unmarshals it into a PropagationContext.

func EncodePropagationContext added in v0.7.0

func EncodePropagationContext(ctx *divergev1alpha1.PropagationContext) (string, error)

EncodePropagationContext marshals a PropagationContext to protobuf and encodes it as base64.

func EnvironmentFromContext

func EnvironmentFromContext(ctx context.Context) string

EnvironmentFromContext extracts the environment name from the context.

func GetHeaderKey added in v0.5.0

func GetHeaderKey() string

GetHeaderKey returns the header key from DIVERGE_HEADER_KEY or DefaultHeaderKey.

func ResolveRedisKey added in v0.17.0

func ResolveRedisKey(ctx context.Context, key string) string

ResolveRedisKey prefixes a Redis/Valkey key with the active preview prefix. It is idempotent: if key already starts with the resolved prefix, it is returned unchanged.

func ResolveRedisPrefix added in v0.17.0

func ResolveRedisPrefix(ctx context.Context) string

ResolveRedisPrefix returns the active Redis/Valkey key prefix for the current preview environment. Priority:

  1. DIVERGE_REDIS_PREFIX or REDIS_KEY_PREFIX environment variable (ensuring a trailing ":").
  2. Active environment from context (or DIVERGE_ENV fallback), formatted as "preview:<env>:".
  3. Empty string "" when running in baseline/production without an active preview environment.

func ResolveStorageKey added in v0.19.0

func ResolveStorageKey(ctx context.Context, key string) string

ResolveStorageKey prefixes an S3/GCS/MinIO object key with the active preview prefix. It is idempotent: if key already starts with the resolved prefix, it is returned without duplicate prefixing.

func ResolveStoragePrefix added in v0.19.0

func ResolveStoragePrefix(ctx context.Context) string

ResolveStoragePrefix returns the active S3/GCS/MinIO object key prefix for the current preview environment. Priority:

  1. DIVERGE_STORAGE_PREFIX, S3_KEY_PREFIX, AWS_S3_PREFIX, or GCS_KEY_PREFIX environment variable (trimmed of leading "/" and normalized with a trailing "/").
  2. Active environment from context (or DIVERGE_ENV fallback), formatted as "previews/<ns>/<env>/" when DIVERGE_NAMESPACE or POD_NAMESPACE is set, or "previews/<env>/" otherwise.
  3. Empty string "" when running in baseline/production without an active preview environment.

func WithEnvironment

func WithEnvironment(ctx context.Context, envName string) context.Context

WithEnvironment returns a context with the environment name set.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL