-
Notifications
You must be signed in to change notification settings - Fork 170
/
experimentation.go
65 lines (56 loc) · 2.23 KB
/
experimentation.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
package middleware
import (
"context"
"log"
"net/http"
"os"
"strings"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/experimentation"
)
type ExperimentationMiddleware struct {
}
func NewExperimentationMiddleware() Middleware {
return &ExperimentationMiddleware{}
}
const assignmentEndpoint = "https://default.exp-tas.com/exptas49/b80dfe81-554e-48ec-a7bc-1dd773cd6a54-azdexpws/api/v1/tas"
func (m *ExperimentationMiddleware) Run(ctx context.Context, next NextFn) (*actions.ActionResult, error) {
endpoint := assignmentEndpoint
// Allow overriding the assignment endpoint, either for local development (where you want to hit a private instance)
// or testing (we use this in our end to end tests to control assignment behavior for the CLI under test)/
if override := os.Getenv("AZD_DEBUG_EXPERIMENTATION_TAS_ENDPOINT"); override != "" {
log.Printf("using override assignment endpoint: %s, from AZD_DEBUG_EXPERIMENTATION_TAS_ENDPOINT", override)
endpoint = override
}
if assignmentManager, err := experimentation.NewAssignmentsManager(
endpoint,
http.DefaultClient,
); err == nil {
if assignment, err := assignmentManager.Assignment(ctx); err != nil {
log.Printf("failed to get variant assignments: %v", err)
} else {
log.Printf("assignment context: %v", assignment.AssignmentContext)
tracing.SetGlobalAttributes(fields.ExpAssignmentContextKey.String(assignment.AssignmentContext))
for _, feature := range assignment.Configs {
for parameter, value := range feature.Parameters {
// Apply any alpha feature configuration from set parameters
if strings.HasPrefix(parameter, "alpha_") {
featureId := parameter[len("alpha_"):]
if enablement, ok := value.(bool); ok {
// set default enablement for the feature
alpha.SetDefaultEnablement(featureId, enablement)
} else {
log.Printf("could not parse value for alpha feature '%s' as a bool, ignoring", featureId)
}
}
}
}
}
} else {
log.Printf("failed to create assignment manager: %v", err)
}
return next(ctx)
}