• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Reading flags from a file

Read time: 3 minutes
Last edited: Dec 28, 2021

Overview

This topic explains how to run feature flags from a file when you're using a server-side SDK.

Do not use file-based flag values in production environments

Always configure production environments to receive flag updates from LaunchDarkly. Only use file-based flags in testing and pre-production environments. We do not support using flags from files in a production environment.

If you use flags from files in a production environment, you need to propagate flag changes to the file whenever one of your flags is updated in the LaunchDarkly UI.

If you perform automated tests or prototyping, you might want to run application code that uses feature flags without connecting to LaunchDarkly. LaunchDarkly SDKs in offline mode return the default value for each flag evaluation. Default is not an actual property of the flag. It is the value that you specified as a fallback in your program code when you evaluated the flag.

However, in some server-side SDKs, you can also use files to configure the feature flag state you desire.

Creating a flag data file

Flag data files can be either JSON or YAML.

They contain up to three properties:

  • flags: These are feature flag definitions. These can contain all the same kinds of rules and targets that you can define in a LaunchDarkly feature flag, which allows the flag to produce different values for different users.
  • flagValues: These are simplified feature flags that specify only a value, and produce the same value for all users.
  • segments: These are user segment definitions. You only use this property if you have feature flags that use segments.
YAML files have limitations

In some of the SDKs, YAML support requires an additional dependency. YAML is not available in PHP.

The format of the data in flags and segments is defined by the LaunchDarkly application and is subject to change. Rather than trying to construct these objects yourself, it's simpler to request existing flags directly from the LaunchDarkly server in JSON format and use this output as the starting point for your file.

Get the flags from https://app.launchdarkly.com/sdk/latest-all. Pass your SDK key in the Authorization header.

For instance, in Linux, you could use this command:

curl -H "Authorization: EXAMPLE_SDK_KEY" https://app.launchdarkly.com/sdk/latest-all >flagdata.json

The output looks like this, but with many more properties:

{
"flags": {
"flag-key-1": {
"key": "flag-key-1",
"on": true,
"variations": [ "a", "b" ]
}
},
"segments": {
"segment-key-1": {
"key": "segment-key-1",
"includes": [ "user-key-1" ]
}
}
}

Data in this format lets the SDK exactly duplicate all the kinds of flag behavior LaunchDarkly supports. However, in most cases you do not need this level of complexity. You may want to simply set specific flag keys to specific values.

For that, you can use a much simpler format.

Here's how:

{
"flagValues": {
"my-string-flag-key": "value-1",
"my-boolean-flag-key": true,
"my-integer-flag-key": 3
}
}

If you want some flags to have simple values and others to have complex behavior, you can specify both flags and flagValues. However, it generates an error if you use the same flag key or segment key more than once, either in a single file or across multiple files.

Configuring the client to use a file

You can specify either a single file or multiple files. In all of the SDKs that support this feature except PHP, you can also specify whether the SDK should reload the file data if it detects that you have modified a file. For example, you could verify that your application behaves correctly when a flag changes.

The examples below show how to configure the client to use two data files called file1.json and file2.json. The client assumes these two files are in the current working directory, but you can specify any relative or absolute file path. It also enables automatic reloading, if supported.

If you do not want your code to connect to LaunchDarkly at all, you must also prevent the SDK from sending analytics events. We've included the option to disable events in these examples.

Because there is no connection to LaunchDarkly, you do not have to use a valid SDK key. The SDK key parameter is still required, but you can use any string.

If any of the specified files is missing or invalid, the SDK does not use any of the file data and logs an error message instead.

Server-side SDKs

This feature is available in the following server-side SDKs:

  • .NET (server-side)
  • Erlang
  • Go
  • Java
  • Node.js (server-side)
  • Python
  • PHP
  • Ruby

.NET (server-side)

To configure the client:

using LaunchDarkly.Sdk.Server;
using LaunchDarkly.Sdk.Server.Integrations;
var config = Configuration.Builder("sdk key")
.DataSource(
FileData.DataSource()
.FilePaths("file1.json", "file2.json")
.AutoUpdate(true)
)
.Events(Components.NoEvents())
.Build()
var client = new LDClient(config);

If you want to use YAML instead of JSON, you must provide a YAML parser.

To learn more, read FileData.

Erlang

To configure the client:

ldclient:start_instance("sdk key", #{
file_datasource => true,
send_events => false,
file_paths => ["file1.json", "file2.yaml"],
feature_store => ldclient_storage_map,
file_auto_update => true,
file_poll_interval => 1000
})
%% In the Erlang SDK automatic reloading uses a polling mechanism.
%% The default interval is 1000ms, but you can control it with
%% the file_poll_interval configuration.

Go

import (
ld "gopkg.in/launchdarkly/go-server-sdk.v5"
"gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
"gopkg.in/launchdarkly/go-server-sdk.v5/ldfiledata"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldfilewatch"
)
var config ld.Config
config.DataSource = ldfiledata.DataSource().
FilePaths("file1.json", "file2.json").
UseReloader(ldfilewatch.WatchFiles)
config.Events = ldcomponents.NoEvents()
client := ld.MakeCustomClient("sdk key", config, 5*time.Second)

Java

To configure the client:

import com.launchdarkly.sdk.server.*;
import com.launchdarkly.sdk.server.integrations.*;
LDConfig config = new LDConfig.Builder()
.dataSource(
FileData.dataSource()
.filePaths("file1.json", "file2.json")
.autoUpdate(true)
)
.events(Components.noEvents())
.build();
LDClient client = new LDClient("sdk key", config);

To learn more, read FileData.

Node.js (server-side)

To configure the client:

const LaunchDarkly = require('ldclient-node');
const dataSource = LaunchDarkly.FileDataSource({
paths: [ "file1.json", "file2.json" ]
});
const config = {
updateProcessor: dataSource
};
const client = LaunchDarkly.init(sdkKey, config);

To learn more, read FileDataSource.

Python

To configure the client:

import ldclient
from ldclient.config import Config
from ldclient.integrations import Files
data_source = Files.new_data_source(paths=["file1.json", "file2.json"],
auto_update=True)
config = Config(update_processor=data_source, send_events=False)
ldclient.set_config(config)
ldclient.set_sdk_key("sdk key")
client = ldclient.get()

In the Python SDK, if you want to use YAML files instead of JSON you must install the pyyaml package. Also, automatic reloading uses an inefficient file-polling mechanism. We recommend installing the watchdog package.

To learn more, read Files.

PHP

To configure the client:

// Automatic reloading is not supported in PHP, because normally in PHP
// the entire in-memory application state is recreated for each request.
$fr = LaunchDarkly\Integrations\Files::featureRequester([
'file1.json',
'file2.json'
]);
$client = new LaunchDarkly\LDClient("your_sdk_key", [
'feature_requester' => $fr,
'send_events' => false
]);

To learn more, read Files.

Ruby

To configure the client:

require 'ldclient-rb'
data_source = LaunchDarkly::Integrations::FileData.data_source(
paths: [ "file1.json", "file2.json" ],
auto_update: true
)
config = LaunchDarkly::Config.new(
data_source: data_source,
send_events: false
)
client = LaunchDarkly::LDClient.new("sdk key", config)

In the Ruby SDK, automatic reloading uses an inefficient file-polling mechanism unless you install the listen gem.

To learn more, read FileData.