|
1 |
| -# Eppo SDK for Golang |
| 1 | +# Eppo Go SDK |
| 2 | + |
| 3 | +[](https://github.com/Eppo-exp/java-server-sdk/actions/workflows/lint-test-sdk.yml) |
2 | 4 |
|
3 | 5 | EppoClient is a client sdk for the `eppo.cloud` randomization API.
|
4 | 6 | It is used to retrieve the experiments data and put it to in-memory store, and then get assignments information.
|
5 | 7 |
|
6 |
| -## Getting Started |
| 8 | +[Eppo](https://www.geteppo.com/) is a modular flagging and experimentation analysis tool. Eppo's Go SDK is built to make assignments in multi-user server side contexts, compatible with Go v1.19 and above. Before proceeding you'll need an Eppo account. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Feature gates |
| 13 | +- Kill switches |
| 14 | +- Progressive rollouts |
| 15 | +- A/B/n experiments |
| 16 | +- Mutually exclusive experiments (Layers) |
| 17 | +- Dynamic configuration |
7 | 18 |
|
8 |
| -Refer to our [SDK documentation](https://docs.geteppo.com/feature-flags/sdks/server-sdks/go) for how to install and use the SDK. |
| 19 | +## Installation |
9 | 20 |
|
10 |
| -## Supported Go Versions |
11 |
| -This version of the SDK is compatible with Go v1.19 and above. |
| 21 | +In your `go.mod`, add the SDK package as a dependency: |
12 | 22 |
|
13 |
| -## Example |
| 23 | +``` |
| 24 | +require ( |
| 25 | + github.com/Eppo-exp/golang-sdk/v2 |
| 26 | +) |
| 27 | +``` |
14 | 28 |
|
| 29 | +Or you can install the SDK from the command line with: |
15 | 30 |
|
16 | 31 | ```
|
17 |
| - import ( |
18 |
| - "github.com/Eppo-exp/golang-sdk/v2/eppoclient" |
19 |
| - ) |
| 32 | +go get github.com/Eppo-exp/golang-sdk/v2 |
| 33 | +``` |
| 34 | + |
| 35 | +## Quick start |
| 36 | + |
| 37 | +Begin by initializing a singleton instance of Eppo's client. Once initialized, the client can be used to make assignments anywhere in your app. |
| 38 | + |
| 39 | +#### Initialize once |
20 | 40 |
|
21 |
| - var eppoClient = &eppoclient.EppoClient{} |
| 41 | +```go |
| 42 | +import ( |
| 43 | + "github.com/Eppo-exp/golang-sdk/v2/eppoclient" |
| 44 | +) |
22 | 45 |
|
23 |
| - func main() { |
24 |
| - eppoClient = eppoclient.InitClient(eppoclient.Config{ |
25 |
| - ApiKey: "<your_api_key>", |
26 |
| - AssignmentLogger: eppoclient.AssignmentLogger{}, |
27 |
| - }) |
28 |
| - } |
| 46 | +var eppoClient = &eppoclient.EppoClient{} |
29 | 47 |
|
30 |
| - func someBLFunc() { |
31 |
| - assignment, _ := eppoClient.GetStringAssignment("subject-1", "experiment_5", sbjAttrs) |
| 48 | +func main() { |
| 49 | + assignmentLogger := NewExampleAssignmentLogger() |
32 | 50 |
|
33 |
| - if assignment == "control" { |
34 |
| - // do something |
35 |
| - } |
36 |
| - } |
| 51 | + eppoClient = eppoclient.InitClient(eppoclient.Config{ |
| 52 | + ApiKey: "<your_sdk_key>", |
| 53 | + AssignmentLogger: assignmentLogger, |
| 54 | + }) |
| 55 | +} |
37 | 56 | ```
|
| 57 | + |
| 58 | + |
| 59 | +#### Assign anywhere |
| 60 | + |
| 61 | +```go |
| 62 | +import ( |
| 63 | + "github.com/Eppo-exp/golang-sdk/v2/eppoclient" |
| 64 | +) |
| 65 | + |
| 66 | +var eppoClient = &eppoclient.EppoClient{} |
| 67 | + |
| 68 | +variation := eppoClient.GetStringAssignment( |
| 69 | + 'new-user-onboarding', |
| 70 | + user.id, |
| 71 | + user.attributes, |
| 72 | + 'control' |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +## Assignment functions |
| 77 | + |
| 78 | +Every Eppo flag has a return type that is set once on creation in the dashboard. Once a flag is created, assignments in code should be made using the corresponding typed function: |
| 79 | + |
| 80 | +```go |
| 81 | +GetBooleanAssignment(...) |
| 82 | +GetNumericAssignment(...) |
| 83 | +GetIntegerAssignment(...) |
| 84 | +GetStringAssignment(...) |
| 85 | +GetJSONAssignment(...) |
| 86 | +``` |
| 87 | + |
| 88 | +Each function has the same signature, but returns the type in the function name. For booleans use `getBooleanAssignment`, which has the following signature: |
| 89 | + |
| 90 | +```go |
| 91 | +func getBooleanAssignment( |
| 92 | + flagKey string, |
| 93 | + subjectKey string, |
| 94 | + subjectAttributes map[string]interface{}, |
| 95 | + defaultValue string |
| 96 | +) bool |
| 97 | + ``` |
| 98 | + |
| 99 | +## Assignment logger |
| 100 | + |
| 101 | +If you are using the Eppo SDK for experiment assignment (i.e randomization), pass in a callback logging function to the `InitClient` function on SDK initialization. The SDK invokes the callback to capture assignment data whenever a variation is assigned. |
| 102 | + |
| 103 | +The code below illustrates an example implementation of a logging callback using Segment. You could also use your own logging system, the only requirement is that the SDK receives a `LogAssignment` function. Here we define an implementation of the Eppo `IAssignmentLogger` interface containing a single function named `LogAssignment`: |
| 104 | + |
| 105 | + |
| 106 | +```go |
| 107 | +import ( |
| 108 | + "github.com/Eppo-exp/golang-sdk/v2/eppoclient" |
| 109 | + "gopkg.in/segmentio/analytics-go.v3" |
| 110 | +) |
| 111 | + |
| 112 | +func main() { |
| 113 | + // Connect to Segment (or your own event-tracking system) |
| 114 | + client := analytics.New("YOUR_WRITE_KEY") |
| 115 | + defer client.Close() |
| 116 | + |
| 117 | + type ExampleAssignmentLogger struct { |
| 118 | + } |
| 119 | + |
| 120 | + func NewExampleAssignmentLogger() *ExampleAssignmentLogger { |
| 121 | + return &ExampleAssignmentLogger{} |
| 122 | + } |
| 123 | + |
| 124 | + func (al *ExampleAssignmentLogger) LogAssignment(event eppoclient.AssignmentEvent) { |
| 125 | + client.Enqueue(analytics.Track{ |
| 126 | + UserId: event.Subject, |
| 127 | + Event: "Eppo Randomization Event", |
| 128 | + Properties: event |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Philosophy |
| 135 | + |
| 136 | +Eppo's SDKs are built for simplicity, speed and reliability. Flag configurations are compressed and distributed over a global CDN (Fastly), typically reaching your servers in under 15ms. Server SDKs continue polling Eppo’s API at 30-second intervals. Configurations are then cached locally, ensuring that each assignment is made instantly. Evaluation logic within each SDK consists of a few lines of simple numeric and string comparisons. The typed functions listed above are all developers need to understand, abstracting away the complexity of the Eppo's underlying (and expanding) feature set. |
0 commit comments