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
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ this, see [Self-Hosting and Open Source](/experimentation/self-hosting). The cha
- Flagsmith API 2.285.0 or later.
- Kafka with at least 3 brokers, for the default replication factor of 3, and a login for Flagsmith with the
[permissions below](#kafka-topics-and-permissions).
- A data warehouse that you host and run yourself, either ClickHouse or Databricks, reachable from both warehouse
delivery and the API.
- A data warehouse that you host and run yourself, one of ClickHouse, Databricks, PostgreSQL or Snowflake, reachable
from both warehouse delivery and the API.

Experiment events are stored in the warehouse rather than in the Flagsmith database, and results are computed by
querying it, so it is infrastructure you provision before enabling experimentation: the managed Flagsmith warehouse is
Expand Down
10 changes: 6 additions & 4 deletions docs/docs/experimentation/connect-a-warehouse/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The managed Flagsmith warehouse is only available on Flagsmith Cloud. When self-

:::note Plans

On Flagsmith Cloud, connecting your own ClickHouse, Databricks or PostgreSQL requires a
On Flagsmith Cloud, connecting your own ClickHouse, Databricks, PostgreSQL or Snowflake requires a
[Scale-Up or Enterprise subscription](https://www.flagsmith.com/pricing). Self-hosted Flagsmith has no plan restriction.

:::
Expand Down Expand Up @@ -61,8 +61,9 @@ The current status is shown on the warehouse card in **Environment Settings ›
- **Connected**: events have been received; you are ready to run experiments.
- **Errored**: the card shows the reason. See Troubleshooting for
[ClickHouse](/experimentation/connect-clickhouse#troubleshooting),
[Databricks](/experimentation/connect-databricks#troubleshooting) or
[PostgreSQL](/experimentation/connect-postgresql#troubleshooting); otherwise [contact support](/support/).
[Databricks](/experimentation/connect-databricks#troubleshooting),
[PostgreSQL](/experimentation/connect-postgresql#troubleshooting) or
[Snowflake](/experimentation/connect-snowflake#troubleshooting); otherwise [contact support](/support/).

## Bring your own warehouse

Expand All @@ -71,7 +72,8 @@ To store experiment events in your own warehouse, and have Flagsmith compute res
- [ClickHouse](/experimentation/connect-clickhouse)
- [Databricks](/experimentation/connect-databricks)
- [PostgreSQL](/experimentation/connect-postgresql), or a PostgreSQL-compatible service
- [Snowflake](/experimentation/connect-snowflake)

## Coming soon

Bring your own warehouse: connections for **Snowflake** and **BigQuery**.
Bring your own warehouse: a connection for **BigQuery**.
4 changes: 2 additions & 2 deletions docs/docs/experimentation/connect-a-warehouse/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ The host can't be changed. To use a different PostgreSQL database, disconnect an
## Performance and table growth

Results queries scan the events table, so they slow down as it grows. If results become too slow, use
[ClickHouse](/experimentation/connect-clickhouse) or [Databricks](/experimentation/connect-databricks), which are built
for this kind of query.
[ClickHouse](/experimentation/connect-clickhouse), [Databricks](/experimentation/connect-databricks) or
[Snowflake](/experimentation/connect-snowflake), which are built for this kind of query.

Flagsmith doesn't delete old events or partition the table. Monitor its size and manage retention yourself.

Expand Down
165 changes: 165 additions & 0 deletions docs/docs/experimentation/connect-a-warehouse/snowflake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
title: Connect Snowflake
sidebar_label: Snowflake
sidebar_position: 4
slug: /experimentation/connect-snowflake
description: Store and query experiment events in your own Snowflake account.
---

:::info Beta

Experimentation is in beta.

:::

Flagsmith writes experiment events to a table in your Snowflake account and computes experiment results by querying it
there. Each environment has one [warehouse connection](/experimentation/connect-a-warehouse).

## Before you start

- On Flagsmith Cloud, a [Scale-Up or Enterprise subscription](https://www.flagsmith.com/pricing).
- A Snowflake user with the `ACCOUNTADMIN` role, to run the setup SQL.
- A terminal with `openssl`, to generate a key pair.

## 1. Generate a key pair

Flagsmith signs in to Snowflake with a key pair. Run these commands on your machine:

```bash
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out flagsmith_key.p8 -nocrypt
openssl rsa -in flagsmith_key.p8 -pubout -out flagsmith_key.pub
Comment on lines +29 to +30

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Set a restrictive umask before generating the private key; otherwise it can be created readable by other local users.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/docs/experimentation/connect-a-warehouse/snowflake.md, line 29:

<comment>Set a restrictive `umask` before generating the private key; otherwise it can be created readable by other local users.</comment>

<file context>
@@ -0,0 +1,163 @@
+Flagsmith signs in to Snowflake with a key pair. Run these commands on your machine:
+
+```bash
+openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out flagsmith_key.p8 -nocrypt
+openssl rsa -in flagsmith_key.p8 -pubout -out flagsmith_key.pub
+```
</file context>
Suggested change
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out flagsmith_key.p8 -nocrypt
openssl rsa -in flagsmith_key.p8 -pubout -out flagsmith_key.pub
umask 077
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out flagsmith_key.p8 -nocrypt
openssl rsa -in flagsmith_key.p8 -pubout -out flagsmith_key.pub

```

`flagsmith_key.pub` is the public key, which you register in Snowflake in step 2. `flagsmith_key.p8` is the private key,
which you enter in Flagsmith in step 3. Keep the private key file safe.

## 2. Create the events table and service user

Open a SQL worksheet in Snowflake and paste this script. Replace `<PUBLIC_KEY>` with the contents of
`flagsmith_key.pub`, without the `BEGIN` and `END` lines. Then run it with **Run All**. Otherwise Snowflake runs only
the statement under the cursor, and the rest fails with "does not exist or not authorized". It is safe to run more than
once. The same SQL is available to copy from the Flagsmith dashboard.

```sql
-- Create the role, warehouse, database, schema and events table
USE ROLE ACCOUNTADMIN;

CREATE ROLE IF NOT EXISTS FLAGSMITH_ROLE;

CREATE WAREHOUSE IF NOT EXISTS FLAGSMITH_WH
WAREHOUSE_SIZE = XSMALL
AUTO_SUSPEND = 60
AUTO_RESUME = TRUE
INITIALLY_SUSPENDED = TRUE;

CREATE DATABASE IF NOT EXISTS FLAGSMITH;
CREATE SCHEMA IF NOT EXISTS FLAGSMITH.FLAGSMITH_EXP;

CREATE TABLE IF NOT EXISTS FLAGSMITH.FLAGSMITH_EXP.EVENTS (
ENVIRONMENT_KEY STRING NOT NULL,
EVENT STRING NOT NULL,
FEATURE_NAME STRING,
TIMESTAMP TIMESTAMP_NTZ NOT NULL,
COLLECTED_AT TIMESTAMP_NTZ,
IDENTIFIER STRING NOT NULL,
VALUE STRING,
TRAITS STRING,
METADATA STRING,
SDK_LANGUAGE STRING,
SDK_VERSION STRING
);

-- Allow the Flagsmith role to read and write experiment events
GRANT USAGE ON WAREHOUSE FLAGSMITH_WH TO ROLE FLAGSMITH_ROLE;
GRANT USAGE ON DATABASE FLAGSMITH TO ROLE FLAGSMITH_ROLE;
GRANT USAGE ON SCHEMA FLAGSMITH.FLAGSMITH_EXP TO ROLE FLAGSMITH_ROLE;
GRANT SELECT, INSERT ON TABLE FLAGSMITH.FLAGSMITH_EXP.EVENTS TO ROLE FLAGSMITH_ROLE;

-- Create the Flagsmith service user with your public key
CREATE USER IF NOT EXISTS FLAGSMITH_SERVICE
TYPE = SERVICE
DEFAULT_ROLE = FLAGSMITH_ROLE
DEFAULT_WAREHOUSE = FLAGSMITH_WH
RSA_PUBLIC_KEY = '<PUBLIC_KEY>';
GRANT ROLE FLAGSMITH_ROLE TO USER FLAGSMITH_SERVICE;
```

The warehouse suspends itself after 60 seconds idle and resumes when Flagsmith reads events; writing events doesn't wake
it.
Comment on lines +87 to +88

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: With AUTO_RESUME = TRUE, an event-write statement also resumes the warehouse; Snowflake auto-resumes for submitted DML as well as reads. Please clarify that writes can incur warehouse compute too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/docs/experimentation/connect-a-warehouse/snowflake.md, line 87:

<comment>With `AUTO_RESUME = TRUE`, an event-write statement also resumes the warehouse; Snowflake auto-resumes for submitted DML as well as reads. Please clarify that writes can incur warehouse compute too.</comment>

<file context>
@@ -0,0 +1,163 @@
+GRANT ROLE FLAGSMITH_ROLE TO USER FLAGSMITH_SERVICE;
+```
+
+The warehouse suspends itself after 60 seconds idle and resumes when Flagsmith reads events; writing events doesn't wake
+it.
+
</file context>
Suggested change
The warehouse suspends itself after 60 seconds idle and resumes when Flagsmith reads events; writing events doesn't wake
it.
The warehouse suspends itself after 60 seconds idle and automatically resumes when Flagsmith reads or writes events.


If you change any of the names in the script, use the same names in step 3. Names may contain only letters, numbers,
underscores and `$`, and must start with a letter or underscore. Flagsmith stores them in upper case.

Flagsmith doesn't create or alter the table, so keep exactly these columns and types.

## 3. Connect in Flagsmith

1. Go to **Environment Settings › Warehouse** and select **Snowflake**.
2. Fill in the connection details:
- **Account identifier**: `ORGNAME-ACCOUNTNAME`, from **Account details** in Snowflake. Pasting the account URL
(`….snowflakecomputing.com`) is fine. It can't be changed after saving.
- **Name this warehouse**: any name, e.g. `Production Snowflake`.
- **Warehouse**, **Database**, **Schema**, **Role** and **User**: the names from the script in step 2. The defaults
match the script. The role must be the user's default role.
- **Private key**: the contents of `flagsmith_key.p8`, including the `BEGIN PRIVATE KEY` and `END PRIVATE KEY` lines.
Flagsmith stores it encrypted and doesn't show it again.
3. Click **Test connection**. Once it succeeds, click **Save and continue**. The test checks that Flagsmith can sign in
with the key, and that the events table exists and has the expected columns. If the test can't connect, you can save
anyway and test again later, but events won't be delivered until it succeeds. If Flagsmith rejects a field, such as
the account identifier or private key, fix it before saving.
4. On Flagsmith Cloud, to verify events reach your table, click **Send your first event** on the connection card; the
button disappears once the first event arrives.

![The Snowflake connection form with account identifier, name, warehouse, database, schema, role, user and private key fields](/img/experimentation/warehouse-snowflake-form.png)

![A connected Snowflake warehouse card showing the last event received, total events, unique event names and the setup SQL](/img/experimentation/warehouse-snowflake-connected.png)

## Manage the connection

To change the warehouse, database, schema, role or user, edit the connection and enter the private key again, so that
Flagsmith can test the connection. Then click **Test connection** and **Save changes**.

### Rotate the key

Snowflake allows two public keys on a user, so you can rotate the key without downtime.

1. Generate a new key pair, as in [step 1](#1-generate-a-key-pair).
2. Register the new public key on the user, in a SQL worksheet. Replace `<NEW_PUBLIC_KEY>` with the contents of the new
public key file, without the `BEGIN` and `END` lines:

```sql
ALTER USER FLAGSMITH_SERVICE SET RSA_PUBLIC_KEY_2='<NEW_PUBLIC_KEY>';
```

3. Edit the connection in Flagsmith and enter the new private key. Leave the other fields as they are.
4. Click **Test connection**, then **Save changes**.
5. Remove the old public key from the user:

```sql
ALTER USER FLAGSMITH_SERVICE UNSET RSA_PUBLIC_KEY;
```

### Change account

The account identifier can't be changed. To use a different Snowflake account, disconnect and create a new connection.

## Troubleshooting

Errors show as the result of **Test connection**, or on the connection card with status **Errored** when event delivery
fails.

| Message | Where | What to do |
| ---------------------------------------------------------------------------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Authentication failed. | Test, card | Check **User**, that the user isn't disabled, and that the public key registered on it matches the private key entered in Flagsmith. |
| Snowflake account not found. | Test, card | Check **Account identifier**. Once saved, it can't be changed; see [Change account](#change-account). |
| The role does not exist or is not granted to the user. | Test, card | Check **Role**, and that the `GRANT ROLE … TO USER …` statement in [step 2](#2-create-the-events-table-and-service-user) ran. |
| Warehouse not found, or the role cannot use it. | Test, card | Check **Warehouse**, and that the role has `USAGE` on it. |
| Events table not found in the configured database. Run the setup SQL to create it. | Test, card | Snowflake reports a missing database, schema or table, and one the role can't see, the same way. Check **Database** and **Schema**, then run the script in step 2 again with **Run All**. |
| Permission denied on the events table. The user needs SELECT and INSERT on it. | Test | Run the `GRANT` statements in step 2 again for the connection's role. |
| Permission denied on the events table. | Card | Run the `GRANT` statements in step 2 again for the user's default role. |
| Database does not exist. | Card | Check **Database**, and that the user's default role has `USAGE` on it. |
| The events table's columns don't match the setup SQL. | Test, card | Make the table's columns and types match step 2 exactly. |
| The query timed out. | Test, card | Check the warehouse's load. |
| Could not connect to the host. | Test, card | Check **Account identifier**, then test again. |
| The Snowflake account rejected the request. | Test, card | Check that the role has the grants in step 2. Otherwise, [contact support](/support/). |
| Connection failed. | Test, card | [Contact support](/support/). |
3 changes: 2 additions & 1 deletion docs/docs/experimentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ metric and experiment, and instruments your application.
:::

1. **[Connect a warehouse](/experimentation/connect-a-warehouse)**: enable the managed Flagsmith warehouse, or connect
your own ClickHouse or Databricks. Self-hosted Flagsmith uses your own. Done once per environment.
your own ClickHouse, Databricks, PostgreSQL or Snowflake. Self-hosted Flagsmith uses your own. Done once per
environment.
2. **[Create metrics](/experimentation/create-metrics)**: define the outcomes you measure, computed from the events your
application sends.
3. **[Create an experiment](/experimentation/create-an-experiment)**: pick a multivariate flag, decide how much traffic
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/experimentation/self-hosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ table.
## Your own warehouse

Events are delivered to your own [ClickHouse](/experimentation/connect-clickhouse),
[Databricks](/experimentation/connect-databricks) or [PostgreSQL](/experimentation/connect-postgresql).
[Databricks](/experimentation/connect-databricks), [PostgreSQL](/experimentation/connect-postgresql) or
[Snowflake](/experimentation/connect-snowflake).

- Also run the event ingestion service, Kafka and warehouse delivery. See [Deploying](#deploying).
- Suits higher event volumes. See
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'fs'
import path from 'path'
import { getSnowflakeSetupSql } from 'components/pages/environment-settings/tabs/warehouse-tab/snowflakeSetupSql'

const normalise = (sql: string) => sql.replace(/\s+/g, ' ').trim()

describe('getSnowflakeSetupSql', () => {
it('matches the copy of the script in the documentation', () => {
const doc = fs.readFileSync(
path.join(
__dirname,
'../../../../../../../../docs/docs/experimentation/connect-a-warehouse/snowflake.md',
),
'utf8',
)
const fencedSql =
doc.match(
/```sql\n(-- Create the role, warehouse, database, schema and events table\n[\s\S]*?)```/,
)?.[1] ?? ''

expect(normalise(fencedSql)).toEqual(normalise(getSnowflakeSetupSql()))
})
})
Loading