From 835566a50e228d053d784ae97ade50ff874673d0 Mon Sep 17 00:00:00 2001 From: Daniel <95646168+daniel-statsig@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:52:02 -0700 Subject: [PATCH] chore: env var eval project (#64) --- benchmarking/dotnet/dotnet/Program.cs | 3 ++- benchmarking/hello_world/src/main.rs | 3 ++- benchmarking/node/pure.js | 2 +- benchmarking/pure.py | 5 +++-- benchmarking/pure.rb | 4 +++- examples/dotnet/dotnet/Program.cs | 19 +++---------------- examples/example.py | 2 +- examples/example.rb | 2 +- examples/go/main.go | 4 +++- .../src/main/java/example/statsig/Main.java | 3 ++- examples/main.c | 2 +- examples/node/example.js | 2 +- examples/php/src/main.php | 3 ++- examples/rust/src/main.rs | 5 ++++- statsig-ffi/bindings/node/index.js | 2 +- .../statsig_event_logging_adapter.rs | 8 +++++++- statsig-lib/src/statsig.rs | 15 +++++++++------ 17 files changed, 46 insertions(+), 38 deletions(-) diff --git a/benchmarking/dotnet/dotnet/Program.cs b/benchmarking/dotnet/dotnet/Program.cs index 46d2b55..4e721ea 100644 --- a/benchmarking/dotnet/dotnet/Program.cs +++ b/benchmarking/dotnet/dotnet/Program.cs @@ -2,7 +2,8 @@ using Statsig; using Statsig.Server; -var statsig = await StatsigServer.Initialize("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW"); +var secret = Environment.GetEnvironmentVariable("test_api_key"); +var statsig = await StatsigServer.Initialize(secret); var watch = Stopwatch.StartNew(); diff --git a/benchmarking/hello_world/src/main.rs b/benchmarking/hello_world/src/main.rs index ef5a3b7..d5e7078 100644 --- a/benchmarking/hello_world/src/main.rs +++ b/benchmarking/hello_world/src/main.rs @@ -5,7 +5,8 @@ use std::time::Instant; #[tokio::main] async fn main() { - Statsig::initialize("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW").await; + let secret_key = env::var("test_api_key").expect("test_api_key must be set"); + Statsig::initialize(secret_key).await; let start = Instant::now(); let mut init_res = String::new(); diff --git a/benchmarking/node/pure.js b/benchmarking/node/pure.js index 3367b32..ee39a5b 100644 --- a/benchmarking/node/pure.js +++ b/benchmarking/node/pure.js @@ -5,7 +5,7 @@ const email = "daniel@statsig.com"; const user = { userID: name, email }; -Statsig.initialize("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW").then( +Statsig.initialize(process.env.test_api_key).then( () => { const gate_name = "test_public"; diff --git a/benchmarking/pure.py b/benchmarking/pure.py index cb2d513..73f1514 100644 --- a/benchmarking/pure.py +++ b/benchmarking/pure.py @@ -1,9 +1,10 @@ +import os from time import perf_counter from statsig import statsig, StatsigUser - -statsig.initialize("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW") +statsig_secret = os.environ.get('test_api_key') +statsig.initialize(statsig_secret) user = StatsigUser("Dan") diff --git a/benchmarking/pure.rb b/benchmarking/pure.rb index 52bd3be..ff2339e 100644 --- a/benchmarking/pure.rb +++ b/benchmarking/pure.rb @@ -1,7 +1,8 @@ require 'statsig' require 'benchmark' -Statsig.initialize('secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW') +secret = ENV['test_api_key'] +Statsig.initialize(secret) user = StatsigUser.new({'userID' => 'Dan'}) @@ -9,6 +10,7 @@ init_res = {} 1000.times do init_res = Statsig.get_client_initialize_response(user) + end puts "Client init res: #{init_res}" } diff --git a/examples/dotnet/dotnet/Program.cs b/examples/dotnet/dotnet/Program.cs index 8993f6c..1d006a2 100644 --- a/examples/dotnet/dotnet/Program.cs +++ b/examples/dotnet/dotnet/Program.cs @@ -1,21 +1,7 @@ using System.Diagnostics; using StatsigServer; -// var statsig = await StatsigServer.Create("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW"); -// -// var watch = Stopwatch.StartNew(); -// -// var result = ""; -// for (var i = 0; i < 1000; i++) -// { -// var user = new User("user_" + i, "daniel@statsig.com"); -// var exp = statsig.GetExperiment(user, "running_exp_in_unlayered_with_holdout"); -// result = statsig.GetClientInitResponse(user); -// } -// -// watch.Stop(); -// -// statsig.Dispose(); + void Foo() { @@ -33,7 +19,8 @@ void Foo() async Task Bar() { var options = new StatsigOptions(); - var statsig = new Statsig("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW", options); + var sdkKey = Environment.GetEnvironmentVariable("test_api_key"); + var statsig = new Statsig(sdkKey, options); await statsig.Initialize(); var user = new StatsigUser("a-user", "daniel@statsig.com"); diff --git a/examples/example.py b/examples/example.py index 6a6e38d..d825abc 100644 --- a/examples/example.py +++ b/examples/example.py @@ -11,7 +11,7 @@ email = "daniel@statsig.com" user = User(user_id, email) -statsig = Statsig("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW") +statsig = Statsig(os.environ['test_api_key']) gate_name = "test_public" diff --git a/examples/example.rb b/examples/example.rb index 9048840..53b4fe8 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -5,7 +5,7 @@ email = "daniel@statsig.com" user = User.create(name, email) -statsig = Statsig.for_user(user, "secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW") +statsig = Statsig.for_user(user, ENV['test_api_key']) # gate_name = "example_gate" # result = statsig.check_gate(gate_name) diff --git a/examples/go/main.go b/examples/go/main.go index 956b292..c0d88fe 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "time" + "os" "statsig.com/sdk/statsig" ) @@ -14,7 +15,8 @@ func main() { user := statsig.NewUser(name, email) defer user.Destroy() - statsigInstance := statsig.NewStatsig(user, "secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW") + sdkKey := os.Getenv("test_api_key") + statsigInstance := statsig.NewStatsig(user, sdkKey) defer statsigInstance.Destroy() // gateName := "test_public" diff --git a/examples/java/src/main/java/example/statsig/Main.java b/examples/java/src/main/java/example/statsig/Main.java index d17609e..eb05349 100644 --- a/examples/java/src/main/java/example/statsig/Main.java +++ b/examples/java/src/main/java/example/statsig/Main.java @@ -11,7 +11,8 @@ public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException { StatsigOptions options = new StatsigOptions.Builder().setOutputLoggerLevel(OutputLogger.LogLevel.DEBUG).build(); - Statsig statsig = new Statsig("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW", options); + String sdkKey = System.getenv("test_api_key"); + Statsig statsig = new Statsig(sdkKey, options); statsig.initialize().get(); diff --git a/examples/main.c b/examples/main.c index 612a07a..104f5a5 100644 --- a/examples/main.c +++ b/examples/main.c @@ -31,7 +31,7 @@ int main() { CUser* user = create_user(name, email); - const char* sdk_key = "secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW"; + const char* sdk_key = getenv("test_api_key"); Statsig* statsig = initialize_statsig(sdk_key); const char* gate_name = "test_public"; diff --git a/examples/node/example.js b/examples/node/example.js index a34ab6f..bcc468b 100644 --- a/examples/node/example.js +++ b/examples/node/example.js @@ -1,7 +1,7 @@ import { Statsig, StatsigOptions, StatsigUser } from "statsig-napi"; const statsig = new Statsig( - "secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW", + process.env.test_api_key, {} ); diff --git a/examples/php/src/main.php b/examples/php/src/main.php index 3ec458a..c1e664a 100644 --- a/examples/php/src/main.php +++ b/examples/php/src/main.php @@ -5,7 +5,8 @@ use Statsig\StatsigFFI\Statsig; use Statsig\StatsigFFI\StatsigUser; -$statsig = new Statsig("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW"); +$secret_key = getenv('test_api_key'); +$statsig = new Statsig($secret_key); $statsig->initialize(function () use ($statsig) { $user = new StatsigUser("a-user", "daniel@statsig.com"); $gcir = $statsig->getClientInitializeResponse($user); diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs index eab0df6..9fc2ccc 100644 --- a/examples/rust/src/main.rs +++ b/examples/rust/src/main.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use tokio::time::{sleep, Duration}; use sigstat::{LogLevel, Statsig, StatsigOptions, StatsigUser}; +use std::env; #[tokio::main] async fn main() { @@ -9,7 +10,9 @@ async fn main() { ..StatsigOptions::new() }); - let statsig = Statsig::new("secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW", Some(opts)); + let sdk_key = env::var("test_api_key").expect("test_api_key environment variable not set"); + + let statsig = Statsig::new(sdk_key, Some(opts)); let _ = statsig.initialize().await; let user = StatsigUser::with_user_id("a-user".to_string()); loop { diff --git a/statsig-ffi/bindings/node/index.js b/statsig-ffi/bindings/node/index.js index e125022..2a10f47 100644 --- a/statsig-ffi/bindings/node/index.js +++ b/statsig-ffi/bindings/node/index.js @@ -106,7 +106,7 @@ function statsig_create(sdkKey, optionsRef) { } const statsig = statsig_create( - 'secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW', + process.env.test_api_key, options, ); console.log('[JS]: statsig_create', statsig); diff --git a/statsig-lib/src/event_logging_adapter/statsig_event_logging_adapter.rs b/statsig-lib/src/event_logging_adapter/statsig_event_logging_adapter.rs index 8218bfd..5517e3d 100644 --- a/statsig-lib/src/event_logging_adapter/statsig_event_logging_adapter.rs +++ b/statsig-lib/src/event_logging_adapter/statsig_event_logging_adapter.rs @@ -105,11 +105,17 @@ impl EventLoggingAdapter for StatsigEventLoggingAdapter { } } + + #[tokio::test] async fn test_event_logging() { + use std::env; + let adapter = StatsigEventLoggingAdapter::new(); + let sdk_key = env::var("test_api_key").expect("test_api_key environment variable not set"); + adapter.bind( - "secret-IiDuNzovZ5z9x75BEjyZ4Y2evYa94CJ9zNtDViKBVdv", + &sdk_key, &StatsigOptions::new(), ); diff --git a/statsig-lib/src/statsig.rs b/statsig-lib/src/statsig.rs index 994fb57..8c57478 100644 --- a/statsig-lib/src/statsig.rs +++ b/statsig-lib/src/statsig.rs @@ -596,8 +596,11 @@ fn create_runtime_if_required() -> (Option, Handle) { mod tests { use super::*; use crate::{evaluation::evaluation_types::AnyConfigEvaluation, StatsigHttpIdListsAdapter}; + use std::env; - const SDK_KEY: &str = "secret-9IWfdzNwExEYHEW4YfOQcFZ4xreZyFkbOXHaNbPsMwW"; + fn get_sdk_key() -> String { + env::var("test_api_key").expect("test_api_key environment variable not set") + } #[tokio::test] async fn test_check_gate() { @@ -606,7 +609,7 @@ mod tests { ..StatsigUser::with_user_id("a-user".to_string()) }; - let statsig = Statsig::new(SDK_KEY, None); + let statsig = Statsig::new(&get_sdk_key(), None); statsig.initialize().await.unwrap(); let gate_result = statsig.check_gate(&user, "test_50_50"); @@ -627,10 +630,10 @@ mod tests { let mut opts = StatsigOptions::new(); - let adapter = Arc::new(StatsigHttpIdListsAdapter::new(SDK_KEY, &opts)); + let adapter = Arc::new(StatsigHttpIdListsAdapter::new(&get_sdk_key(), &opts)); opts.id_lists_adapter = Some(adapter); - let statsig = Statsig::new(SDK_KEY, Some(Arc::new(opts))); + let statsig = Statsig::new(&get_sdk_key(), Some(Arc::new(opts))); statsig.initialize().await.unwrap(); let gate_result = statsig.check_gate(&user, "test_id_list"); @@ -645,7 +648,7 @@ mod tests { ..StatsigUser::with_user_id("a-user".to_string()) }; - let statsig = Statsig::new(SDK_KEY, None); + let statsig = Statsig::new(&get_sdk_key(), None); statsig.initialize().await.unwrap(); let experiment = statsig.get_experiment(&user, "running_exp_in_unlayered_with_holdout"); @@ -664,7 +667,7 @@ mod tests { ..StatsigOptions::new() }; - let statsig = Statsig::new(SDK_KEY, Some(Arc::new(opts))); + let statsig = Statsig::new(&get_sdk_key(), Some(Arc::new(opts))); statsig.initialize().await.unwrap(); let response = statsig.get_client_init_response(&user);