golang-sdk

module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: Apache-2.0

README

OpenFeature SDK for Golang

a Go Report Card codecov Specification

This is the Golang implementation of OpenFeature, a vendor-agnostic abstraction library for evaluating feature flags.

We support multiple data types for flags (floats, integers, strings, booleans, objects) as well as hooks, which can alter the lifecycle of a flag evaluation.

Installation

go get github.com/open-feature/golang-sdk

Usage

To configure the sdk you'll need to add a provider to the openfeature global singleton. From there, you can generate a Client which is usable by your code. While you'll likely want a provider for your specific backend, we've provided a NoopProvider, which simply returns the default passed in.

package main

import (
	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	openfeature.SetProvider(openfeature.NoopProvider{})
	client := openfeature.NewClient("app")
	value, err := client.BooleanValue("v2_enabled", false, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{})
}
Hooks

Implement your own hook by conforming to the Hook interface.

To satisfy the interface all methods (Before/After/Finally/Error) need to be defined. To avoid defining empty functions make use of the UnimplementedHook struct (which already implements all the empty functions).

type MyHook struct {
  openfeature.UnimplementedHook
}

// overrides UnimplementedHook's Error function
func (h MyHook) Error(hookContext openfeature.HookContext, err error, hookHints openfeature.HookHints) {
	log.Println(err)
}

Register the hook at global, client or invocation level.

Configuration

Logging

If not configured, the logger falls back to the standard Go log package at error level only.

In order to avoid coupling to any particular logging implementation the sdk uses the structured logging logr API. This allows integration to any package that implements the layer between their logger and this API. Thankfully there is already integration implementations for many of the popular logger packages.

var l logr.Logger
l = integratedlogr.New() // replace with your chosen integrator

openfeature.SetLogger(l) // set the logger at global level

c := openfeature.NewClient("log").WithLogger(l) // set the logger at client level

logr uses incremental verbosity levels (akin to named levels but in integer form). The sdk logs info at level 0 and debug at level 1. Errors are always logged.

Development

Installation and Dependencies

Install dependencies with go get ./...

We value having as few runtime dependencies as possible. The addition of any dependencies requires careful consideration and review.

Testing

Run tests with make test.

Contacting us

We hold regular meetings which you can see here.

We are also present in the #openfeature channel in the CNCF slack.

Contributors

Thanks so much to our contributors.

Made with contrib.rocks.

License

Apache License 2.0

Directories

Path Synopsis
pkg
openfeature
Package openfeature provides global access to the OpenFeature API.
Package openfeature provides global access to the OpenFeature API.

Jump to

Keyboard shortcuts

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