configmanager

package module
v0.0.0-...-591e9e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2019 License: MIT Imports: 9 Imported by: 0

README

Configmanager

Configmanager is the what mixpanel uses to add dynamic configuration to our code across several services.

Configmanager does these things:

  • Reads a file configs.json under a specified path, and watches it for changes
  • Interprets the data in the file and gives an easy interface to access the configuration values

Here is a possible example for a hypothetical service called my-service

  cm, err := configmanager.NewClient("/etc/configs", "my-service", fr)
  if err != nil {
  	return err
  }
  defer cm.Close()
  if cm.GetBoolean("my-feature-enabled", false) {
  	// do the my-feature thing
  }

The only requirement for this example to work is placing marshalled configurations in the file /etc/configs/my-service/configs.json

How are configs written

Mixpanel writes configs using JSONNET and they are mounted using kubernetes configmaps. However configmanager only cares about the format and a file that follows the path /dir-path/[name of config group]/configs.json

Two key things you need to unerstand are:

  • Config group: A group of configs that belong together in same scope, because they are used by same feature or same service etc
  • Config: A key, value pair where value can be an arbitrary JSON and you call configmanager methods specifiying the key

A config group marshalled in a file looks like this:

[
  {
    "key": "feature_enabled_customers",
    "value": {
      "123": {},
      "456": {}
    }
  },
  {
    "key": "scaling_percentage",
    "value": 1
  },
  {
    "key": "timeout_secs",
    "value": 5
  }
]

If such a config is placed in the file /etc/configs/my-configs/configs.json then the configmanager will be constructed using configmanager.NewClient("/etc/configs", "my-configs", fr)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Unmarshal(key string, val interface{}) error
	GetBoolean(key string, defaultVal bool) bool
	GetInt64(key string, defaultVal int64) int64
	GetByte(key string, defaultVal uint8) uint8

	GetFloat64(key string, defaultVal float64) float64
	GetString(key string, defaultVal string) string
	GetRaw(key string) ([]byte, error)

	IsFeatureEnabled(key string, enabledByDefault bool) bool
	// we use project whitelisting quite a lot. This expects
	// map [int64]struct{}
	IsProjectWhitelisted(key string, projectID int64, defaultVal bool) bool
	IsTokenWhitelisted(key string, token string, defaultVal bool) bool
	Close()
}

Client is the interface for reading configs stored in configmap. It has helper methods for different data types so the user does not have to care about the structure of configs.

func NewClient

func NewClient(dirPath string, scope string, fr obs.FlightRecorder) (Client, error)

NewClient returns a config manager client for a scope specified. If you created the configs from the jsonnet config helper then your configs will be placed like /etc/configs/storage-server/configs. This client assumes that there will a file called configs.json for a scope and inside the file there will be configs specified in the model If there is an error initing the client, it will become a null client, return an error and just return default values on the Gets For your services instead of creating one type of config in a configmap, group all of your configs into logical scope and create the configmap using the jsonnet helper. With adoption of this client, you will at least every single service having one scope with bunch of configs that are relevant to that service.

func NewNullClient

func NewNullClient() Client

NewNullClient returns a client that will just echo back the default value you set in your Gets

type TestClient

type TestClient struct {
	// contains filtered or unexported fields
}

TestClient is to be used only for tests It is threadsafe but it can NOT be used for production Values can be set on the test client using the set methods and then it echoes back those values in the Get methods

func NewTestClient

func NewTestClient() *TestClient

NewTestClient returns a TestClient

func (TestClient) Close

func (c TestClient) Close()

func (TestClient) GetBoolean

func (c TestClient) GetBoolean(key string, defaultVal bool) bool

func (TestClient) GetByte

func (c TestClient) GetByte(key string, defaultVal uint8) uint8

func (TestClient) GetFloat64

func (c TestClient) GetFloat64(key string, defaultVal float64) float64

func (TestClient) GetInt64

func (c TestClient) GetInt64(key string, defaultVal int64) int64

func (TestClient) GetRaw

func (c TestClient) GetRaw(key string) ([]byte, error)

func (TestClient) GetString

func (c TestClient) GetString(key string, defaultVal string) string

func (TestClient) IsFeatureEnabled

func (c TestClient) IsFeatureEnabled(key string, enabledByDefault bool) bool

func (TestClient) IsProjectWhitelisted

func (c TestClient) IsProjectWhitelisted(key string, projectID int64, defaultVal bool) bool

func (TestClient) IsTokenWhitelisted

func (c TestClient) IsTokenWhitelisted(key string, token string, defaultVal bool) bool

func (*TestClient) SetBoolean

func (t *TestClient) SetBoolean(key string, val bool) *TestClient

func (*TestClient) SetByte

func (t *TestClient) SetByte(key string, val uint8) *TestClient

func (*TestClient) SetFloat64

func (t *TestClient) SetFloat64(key string, val float64) *TestClient

func (*TestClient) SetInt64

func (t *TestClient) SetInt64(key string, val int64) *TestClient

func (*TestClient) SetProjectsWhitelist

func (t *TestClient) SetProjectsWhitelist(key string, projects ...int) *TestClient

func (*TestClient) SetRaw

func (t *TestClient) SetRaw(key string, raw []byte) *TestClient

func (*TestClient) SetString

func (t *TestClient) SetString(key string, val string) *TestClient

func (TestClient) Unmarshal

func (c TestClient) Unmarshal(key string, val interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL