openfeatureposthog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 5

README

PostHog OpenFeature Provider for Go

Go Reference

This repository provides an implementation of an OpenFeature provider for PostHog.

Getting started

This simple snippet should be enough to get you started to use the provider:

import (
	"fmt"
	"context"

	"github.com/dhaus67/openfeature-posthog-go"
	"github.com/open-feature/go-sdk/openfeature"
	"github.com/posthog/posthog-go"
)

func main() {
	// Start by creating a PostHog client with your desired configuration.
	client, err := posthog.NewWithConifg("<your api key>", posthog.Config{})
	if err != nil {
		panic(err)
	}

	// Create the provider and register it.
	openfeature.SetProvider(openfeatureposthog.NewProvider(client))

	client := openfeature.NewClient("my-client")

	// The targeting key is required with this provider. It is used to evaluate with PostHog
	// whether for the specific user the feature is enabled or not.
	evalCtx := openfeature.NewEvaluationContext("<distinct-user-id>", map[string]interface{}{})

	secretFeature, err := client.BooleanValue(context.Background(), "secret", false, evalCtx)
	if err != nil {
		panic(err)
	}

	if secretFeature {
		fmt.Println("Secret feature is enabled")
	}
}

In addition to the targeting key, it is also possible to specify additional values to filter on for the PostHog user: groups, groupProperties, and personProperties.

The documentation for the PostHog Go SDK has a rich documentation about these use-cases.

Documentation

Index

Constants

View Source
const (
	DistinctIDContextKey = openfeature.TargetingKey
	GroupsContextKey     = "groups"
	PropertiesContextKey = "properties"
)

All supported keys in the evaluation context.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostHogProperties

type PostHogProperties struct {
	GroupProperties  map[string]posthog.Properties
	PersonProperties posthog.Properties
}

type Provider

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

func NewProvider

func NewProvider(client posthog.Client) *Provider

NewProvider creates a new PostHog provider.

func (*Provider) BooleanEvaluation

func (p *Provider) BooleanEvaluation(_ context.Context, flag string, defaultValue bool, evalCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail

func (*Provider) FloatEvaluation

func (p *Provider) FloatEvaluation(_ context.Context, flag string, defaultValue float64, evalCtx openfeature.FlattenedContext) openfeature.FloatResolutionDetail

func (*Provider) Hooks

func (p *Provider) Hooks() []openfeature.Hook

Hooks returns the list of hooks of the provider. The PostHog provider does not have any hooks, an empty slice is returned.

func (*Provider) IntEvaluation

func (p *Provider) IntEvaluation(_ context.Context, flag string, defaultValue int64, evalCtx openfeature.FlattenedContext) openfeature.IntResolutionDetail

func (*Provider) Metadata

func (p *Provider) Metadata() openfeature.Metadata

Metadata returns the providers metadata.

func (*Provider) ObjectEvaluation

func (p *Provider) ObjectEvaluation(_ context.Context, flag string, defaultValue interface{}, evalCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail

func (*Provider) StringEvaluation

func (p *Provider) StringEvaluation(_ context.Context, flag string, defaultValue string, evalCtx openfeature.FlattenedContext) openfeature.StringResolutionDetail

Jump to

Keyboard shortcuts

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