Giter VIP home page Giter VIP logo

go-sdk's Introduction

Optimizely Go SDK

Go Report Card Coverage Status

This repository houses the Go SDK for use with Optimizely Feature Experimentation and Optimizely Full Stack (legacy).

Optimizely Feature Experimentation is an A/B testing and feature management tool for product development teams that enables you to experiment at every step. Using Optimizely Feature Experimentation allows for every feature on your roadmap to be an opportunity to discover hidden insights. Learn more at Optimizely.com, or see the developer documentation.

Optimizely Rollouts is free feature flags for development teams. You can easily roll out and roll back features in any application without code deploys, mitigating risk for every feature on your roadmap.

Get Started

Refer to the Go SDK's developer documentation for detailed instructions on getting started with using the SDK.

Requirements

Requires Golang version:

SDK Version Required Golang Version
v2.0.0 1.21.0 or higher
< v2.0.0 1.13 or higher

Install the SDK

Install from github:

go get github.com/optimizely/go-sdk/v2

Install from source:

go get github.com/optimizely/go-sdk/v2
cd $GOPATH/src/github.com/optimizely/go-sdk/v2
go install

NOTE: We practice trunk-based development, and as such our default branch, master might not always be the most stable. We do tag releases on Github and you can pin your installation to those particular release versions. One way to do this is to use Go Modules for managing external dependencies:

Install using go.mod:

module mymodule

go 1.21.0

require (
	github.com/optimizely/go-sdk/v2 v2.0.0
)

If you are already using go.mod in your application you can run the following:

go mod edit -require github.com/optimizely/go-sdk/[email protected]

NOTE:

go get github.com/optimizely/go-sdk/v2/...

or

go get github.com/optimizely/go-sdk/v2/pkg

will install it as a package to pkg directory, rather than src directory. It could be useful for future development and vendoring.

Use the Go SDK

See the example file in examples/main.go.

Initialization

import optly "github.com/optimizely/go-sdk/v2"
import "github.com/optimizely/go-sdk/v2/client"

// Simple one-line initialization with the SDK key    
optlyClient, err := optly.Client("SDK_KEY")    

// You can also instantiate with a hard-coded datafile using our client factory method    
optimizelyFactory := &client.OptimizelyFactory {        
  Datafile: []byte("datafile_string"),   
}    
optlyClient, err = optimizelyFactory.Client()

Make Decisions

import ( 
  optly "github.com/optimizely/go-sdk/v2" 
) 

// instantiate a client 
client, err := optly.Client("SDK_KEY") 

// User attributes are optional and used for targeting and results segmentation 
attributes := map[string]interface{}{ 
  "state": "California", 
  "likes_donuts": true, 
} 

user := client.CreateUserContext("optimizely end user", attributes) 
options := []decide.OptimizelyDecideOptions{decide.IncludeReasons} 

decision := user.Decide("my_flag", options) 
var variationKey string 
if variationKey = decision.VariationKey; variationKey == "" { 
  fmt.Printf("[decide] error: %v", decision.Reasons) 
  return 
} 

if variationKey == "control" { 
	// Execute code for control variation 
} else if variationKey == "treatment" { 
	// Execute code for treatment variation 
}

SDK Development

Unit Tests

Run

make test 

Contributing

Please see CONTRIBUTING.

Credits

This software is distributed with code from the following open source projects:

murmur3 Copyright 2013, Sébastien Paolacci. License (BSD-3 Clause): https://github.com/twmb/murmur3/blob/master/LICENSE

uuid Copyright (c) 2009, 2014 Google Inc. All rights reserved. License (BSD-3 Clause): https://github.com/google/uuid/blob/master/LICENSE

testify Copyright (c) 2012-2018 Mat Ryer and Tyler Bunnell. License (MIT): https://github.com/stretchr/testify/blob/master/LICENSE

json-iterator Copyright (c) 2016 json-iterator License (MIT): https://github.com/json-iterator/go/blob/master/LICENSE

subset Copyright (c) 2015, Facebook, Inc. All rights reserved. License (BSD): https://github.com/facebookarchive/subset/blob/master/license

profile Copyright (c) 2013 Dave Cheney. All rights reserved. License (BSD): https://github.com/pkg/profile/blob/master/LICENSE

sync Copyright (c) 2009 The Go Authors. All rights reserved. https://github.com/golang/sync/blob/master/LICENSE

opentelemetry-go Copyright The OpenTelemetry Authors License (Apache-2.0): https://github.com/open-telemetry/opentelemetry-go/blob/main/LICENSE

Other Optimizely SDKs

go-sdk's People

Contributors

aliabbasrizvi avatar andrewleap-optimizely avatar fscelliott avatar juancarlostong avatar keppel2 avatar kirkeaton avatar mat001 avatar mjc1283 avatar mnoman09 avatar msohailhussain avatar oakbani avatar ozayr-zaviar avatar pawels-optimizely avatar piyushhbhutoria avatar pulak-opti avatar shaharyar123 avatar thomaszurkan-optimizely avatar tjj5036 avatar yasirfolio3 avatar zashraf1985 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-sdk's Issues

Activate and GetVariation roadmap

Question

What does the roadmap look like for the go-sdk in terms of feature parity? At the moment only IsFeatureEnabled is supported.

Is Optimizely client thread/concurrency safe?

Hi all, I'm currently using the Optimizely Go client in my back-end, and couldn't find any documentation if it's thread/concurrency safe or not.

I'm instantiating optClient once whenever my app starts, and using it as follows:

optClient.IsFeatureEnabled(featureName)

Should I handle concurrency here (mutex lock, waitgroup, etc) or is this thread safe out of the box?

Thanks in advance.

Optimizely CloudQuery Source Plugin

Hi Team, hopefully this is right place to ask, if not, I'd appreciate if you can direct me.

I'm the founder of cloudquery.io, a high performance open source ELT framework.

Our users are interested in an Optimizely plugin, but as we cannot maintain all the plugins ourselves, I was curious if this would be an interesting collaboration, where we would help implement an initial source plugin, and you will help maintain it.

This will give your users the ability to sync Optimizely data to any of their datalakes/data-warehouses/databases easily using any of the growing list of CQ destination plugins.

Best,
Yevgeny

Offline / no-op initialisation option

Hi,

Our product is available either as cloud software, or an on premises deployment.

One of the things I'd like to do with this SDK, is disable it for on premises deployments of our product, as the users will not have Optimizely subscriptions.

I would also prefer not to have to wrap the SDK everywhere we would use it throughout our codebases, so some kind of initialization option to facilitate this would be very helpful.

(Edit: sorry for the half-completed original issue)

Random datafile fetching failures

Hi all, I am currently using the Optimizely Go client and am having sporadic difficulties with fetching the datafile. My service regularly attempts to fetch the datafile and it is not always able to successfully find it. Is there any particular reason why it would be able to sometimes find the datafile and sometimes not? The error message we receive can be found below. Thank you.

failed to send request &{GET https://cdn.optimizely.com/datafiles/<DATAFILEHASH>.json HTTP/1.1 1 1 map[Accept:[application/json] Content-Type:[application/json] If-Modified-Since:[Wed, 06 Oct 2021 15:20:08 GMT]] <nil> <nil> 0 [] false cdn.optimizely.com map[] map[] <nil> map[] <nil> <nil> <nil> 0xc000046040}

Request for OptimizelyClient to be an interface

Today OptimizelyClient is exposed directly as a struct. This doesn't allow users of the sdk to easily test integrations via tools like gomock or counterfeiter, as these tools are built around generating fake implementations of interfacing for mocking out in tests.

I considered just putting up a PR but, didn't for a couple reasons:

  1. Would be a pity to put the PR together and be told "nope, we do not want to do that" :D

  2. It is an API incompatible change. If you have something like the following, it would break on the ConfigManager line b/c interfaces don't have members (so the change would, at the least, have to change it to a call to ConfigManager()).

client := factory.Client(...)

client.ConfigManager.Whatever()

Does this seem like a viable change? Today we simply wrap calls to factory.Client() so that they return an interface we define in our own code, which is just an exact mirroring of the subset of the OptimizelyClient methods we use, so that we can generate mocks on that interface for testing. This of course isn't a joy in terms of maintenance or boiler plate though... thus this request!

Valid non-object JSON features cause panics

Cc @simone-coelho .

This bug is in relation to Optimizely support ticket #1001007469 .

The bug described in this ticket is in these locations:
https://github.com/optimizely/go-sdk/blob/master/pkg/client/client.go#L935

https://github.com/optimizely/go-sdk/blob/master/pkg/client/client.go#L500

The code always assumes that valid JSON will be returned as a map. It does not check first whether the JSON is a map, array, or scalar value. However, the optimizely web interface allows any valid json value.

We put in a JSON array into a feature in optimizely. This was passed to the optimizely agent, which in turn passed it to this go-sdk. The sdk assumed that because the value was JSON it was necessarily a map and tried to convert it to a map in the linked line of code. It wasn't a map in our case, but an array. This causes a panic, which causes the optimizely agent to sever the TCP connection with the client without so much as a 500 error.

The code herein linked should be changed so that it checks to make sure the JSON is indeed a map before it attempts conversion and issue a polite error if it isn't as expected so that the optimizely agent can give back a polite http code error in turn. Or, change the code to allow for arrays, as is allowed by the optimizely UI.

We appreciate your time.

Client option to provide a custom http.Client

Hi all, could we add an OptionFunc that takes in a custom *http.Client for the Client to use?

The use case is that we have our own http.Client that includes middlewares for things like metrics and tracing, and it would be ideal if we can use that instead of writing custom wrappers over the Optimizely Client to do the same.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.