StatsigClient

Struct StatsigClient 

Source
pub struct StatsigClient { /* private fields */ }
Expand description

A high-performance, async client for Statsig feature flags and dynamic configs.

§Architecture

The client uses a multi-layered architecture:

  • API Layer: Handles HTTP communication with Statsig servers
  • Cache Layer: Provides intelligent caching with TTL support
  • Batch Layer: Optimizes multiple requests into single API calls

§Performance Characteristics

  • Cache hit latency: ~1ms
  • API call latency: ~100ms (network dependent)
  • Batch processing: Reduces API calls by up to 90%

Implementations§

Source§

impl StatsigClient

Source

pub async fn new(api_key: impl Into<String>) -> Result<Self>

Create a new Statsig client with the given API key

§Arguments
  • api_key - Your Statsig server API key
§Returns

A configured StatsigClient ready for use

§Errors

Returns an error if the API key is invalid or HTTP client creation fails

Source

pub async fn with_config(config: StatsigClientConfig) -> Result<Self>

Create a new Statsig client with custom configuration

§Arguments
  • config - Custom configuration for the client
§Returns

A configured StatsigClient with custom settings

§Errors

Returns an error if configuration validation fails

Source

pub async fn log_event( &self, event_name: impl Into<String>, user: &User, ) -> Result<bool>

Source

pub async fn log_events( &self, events: Vec<StatsigEvent>, user: &User, ) -> Result<LogEventResponse>

Source

pub async fn check_gate( &self, gate_name: impl Into<String>, user: &User, ) -> Result<bool>

Check if a single feature gate passes for a user

This method first checks the cache for a recent evaluation, falling back to the Statsig API if needed. Results are automatically cached for the configured TTL duration.

§Arguments
  • gate_name - The name of the feature gate to check (2-100 characters)
  • user - The user to evaluate the gate for
§Returns

Ok(true) if the gate passes, Ok(false) if it doesn’t, or an error if the evaluation fails.

§Errors
  • StatsigError::Validation if the gate name or user is invalid
  • StatsigError::Network if the API request fails
  • StatsigError::Api if the server returns an error response
§Performance
  • Cache hit: ~1ms
  • Cache miss: ~100ms (network dependent)
Source

pub async fn check_gates( &self, gate_names: Vec<String>, user: &User, ) -> Result<HashMap<String, bool>>

Check multiple feature gates for a user

This method efficiently checks multiple gates in a single API call when cache misses occur, significantly reducing network overhead.

§Arguments
  • gate_names - List of gate names to check
  • user - The user to evaluate the gates for
§Returns

A HashMap mapping gate names to their boolean results

§Errors

Same as check_gate

Source

pub async fn get_config( &self, config_name: impl Into<String>, user: &User, ) -> Result<Value>

Get a single dynamic config for a user

Retrieves a dynamic config (or experiment) value for the given user, with caching for improved performance. Statsig uses the same endpoint for both dynamic configs and experiments; the backend determines which based on the name.

§Arguments
  • config_name - The name of the config to retrieve
  • user - The user to get the config for
§Returns

The config value as a JSON Value, or null if not found

§Errors

Similar to check_gate, with validation and network errors

Source

pub async fn get_config_evaluation( &self, config_name: impl Into<String>, user: &User, ) -> Result<ConfigEvaluationResult>

Get a single dynamic config (or experiment) evaluation for a user

Returns the full evaluation payload including rule_id, group_name, and group.

Source

pub async fn get_configs( &self, config_names: Vec<String>, user: &User, ) -> Result<HashMap<String, Value>>

Get multiple dynamic configs for a user

Efficiently retrieves multiple configuration objects (or experiments) in parallel when cache misses occur.

§Arguments
  • config_names - List of config names to retrieve
  • user - The user to get configs for
§Returns

A HashMap mapping config names to their JSON values

§Errors

Similar to check_gate

Source

pub async fn get_config_evaluations( &self, config_names: Vec<String>, user: &User, ) -> Result<HashMap<String, ConfigEvaluationResult>>

Get multiple dynamic config (or experiment) evaluations for a user

Returns full evaluation payloads including rule_id, group_name, and group.

Source

pub fn cache_metrics(&self) -> CacheMetricsSummary

Get cache performance metrics

Returns a snapshot of cache performance metrics including hit ratio, total requests, and other useful statistics for monitoring.

§Returns

A summary of cache metrics

Source

pub fn reset_cache_metrics(&self)

Reset cache metrics

Resets all cache performance counters to zero. Useful for periodic monitoring or testing scenarios.

Trait Implementations§

Source§

impl Debug for StatsigClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more