From 321a63aac3723a791cb1b0aadb98880ce244e77d Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 26 Feb 2025 15:46:35 -0500 Subject: [PATCH] feat(nx-dev): allow sampling rate to be configured through env var (#30193) The current sample rate for Grafana Faro is the default 100% and is currently around $200 for the month. We want to be able to make changes to it without needing code changes. Once this PR is merged, we can just update the env var, and then redeploy. --- nx-dev/nx-dev/lib/components/frontend-observability.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nx-dev/nx-dev/lib/components/frontend-observability.tsx b/nx-dev/nx-dev/lib/components/frontend-observability.tsx index fd2e1902cf..de7f2da923 100644 --- a/nx-dev/nx-dev/lib/components/frontend-observability.tsx +++ b/nx-dev/nx-dev/lib/components/frontend-observability.tsx @@ -2,6 +2,12 @@ import { useEffect, useRef } from 'react'; import { getWebInstrumentations, initializeFaro } from '@grafana/faro-web-sdk'; +const DEFAULT_SAMPLE_RATE = 0.5; +let samplingRate = process.env.NEXT_PUBLIC_FARO_SAMPLING_RATE + ? parseFloat(process.env.NEXT_PUBLIC_FARO_SAMPLING_RATE) + : DEFAULT_SAMPLE_RATE; +if (isNaN(samplingRate) || samplingRate > 1) samplingRate = DEFAULT_SAMPLE_RATE; + export function FrontendObservability() { const initialized = useRef(false); useEffect(() => { @@ -24,6 +30,9 @@ export function FrontendObservability() { version, environment, }, + sessionTracking: { + samplingRate, + }, instrumentations: [...getWebInstrumentations()], }); }, []);