-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolldice.go
92 lines (74 loc) · 2.34 KB
/
rolldice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"strconv"
"context"
"time"
flagd "github.com/open-feature/go-sdk-contrib/providers/flagd/pkg"
"github.com/open-feature/go-sdk/pkg/openfeature"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
var (
tracer = otel.Tracer("rolldice")
meter = otel.Meter("rolldice")
rollCnt metric.Int64Counter
openFeatureClient = openfeature.NewClient("app")
)
func init() {
var err error
rollCnt, err = meter.Int64Counter("dice.rolls",
metric.WithDescription("The number of rolls by roll value"),
metric.WithUnit("{roll}"))
if err != nil {
panic(err)
}
/* OpenFeature Initialisation
* Connect to feature flag system (flagd) here
*/
openfeature.SetProvider(flagd.NewProvider(
flagd.WithHost("localhost"),
flagd.WithPort(8013),
))
}
// Redirect homepage to /rolldice
func renderHomepage(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "rolldice", http.StatusPermanentRedirect)
}
func rolldice(w http.ResponseWriter, r *http.Request) {
feature_flag_key := "slow-your-roll"
// Pass UserAgent string to flagd
evaluationContext := openfeature.NewEvaluationContext("", map[string]interface{}{
"userAgent": r.UserAgent(),
})
// Get flag value...
// Evaluate your feature flag
slowYourRoll, _ := openFeatureClient.BooleanValue(
context.Background(), feature_flag_key, false, evaluationContext,
)
ctx, span := tracer.Start(r.Context(), "roll")
defer span.End()
evaluationContextString := fmt.Sprintf("%#v", evaluationContext)
// Add feature flag values
span.SetAttributes(attribute.String("feature_flag.key", feature_flag_key))
span.SetAttributes(attribute.String("feature_flag.provider_name", "flagd"))
span.SetAttributes(attribute.Bool("feature_flag.variant", slowYourRoll))
span.SetAttributes(attribute.String("feature_flag.evaluation_context", evaluationContextString))
if slowYourRoll {
time.Sleep(2 * time.Second)
}
roll := 1 + rand.Intn(6)
// Add the custom attribute to the span and counter.
rollValueAttr := attribute.Int("roll.value", roll)
span.SetAttributes(rollValueAttr)
rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr))
resp := strconv.Itoa(roll) + "\n"
if _, err := io.WriteString(w, resp); err != nil {
log.Printf("Write failed: %v\n", err)
}
}