-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtracing.go
More file actions
176 lines (147 loc) · 5.35 KB
/
tracing.go
File metadata and controls
176 lines (147 loc) · 5.35 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package tracing
import (
"context"
"net/http"
"os"
"strconv"
"strings"
)
type RequestType string
type RequestTracingKey string
type HostType string
const (
RequestTypeStartUp RequestType = "StartUp"
RequestTypeWatch RequestType = "Watch"
HostTypeAzureFunction HostType = "AzureFunction"
HostTypeAzureWebApp HostType = "AzureWebApp"
HostTypeContainerApp HostType = "ContainerApp"
HostTypeKubernetes HostType = "Kubernetes"
HostTypeServiceFabric HostType = "ServiceFabric"
EnvVarTracingDisabled = "AZURE_APP_CONFIGURATION_TRACING_DISABLED"
EnvVarAzureFunction = "FUNCTIONS_EXTENSION_VERSION"
EnvVarAzureWebApp = "WEBSITE_SITE_NAME"
EnvVarContainerApp = "CONTAINER_APP_NAME"
EnvVarKubernetes = "KUBERNETES_PORT"
// Documentation : https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference
EnvVarServiceFabric = "Fabric_NodeName"
RequestTypeKey = "RequestType"
HostTypeKey = "Host"
KeyVaultConfiguredTag = "UsesKeyVault"
KeyVaultRefreshConfiguredTag = "RefreshesKeyVault"
FeaturesKey = "Features"
AIConfigurationTag = "AI"
AIChatCompletionConfigurationTag = "AICC"
FailoverRequestTag = "Failover"
ReplicaCountKey = "ReplicaCount"
LoadBalancingEnabledTag = "LB"
SnapshotReferenceTag = "SnapshotRef"
// Feature flag usage tracing
FMGoVerEnv = "MS_FEATURE_MANAGEMENT_GO_VERSION"
FMGoVerKey = "FMGoVer"
FeatureFilterTypeKey = "Filter"
CustomFilterKey = "CSTM"
TimeWindowFilterKey = "TIME"
TargetingFilterKey = "TRGT"
FFTelemetryUsedTag = "Telemetry"
FFMaxVariantsKey = "MaxVariants"
FFSeedUsedTag = "Seed"
FFFeaturesKey = "FFFeatures"
TimeWindowFilterName = "Microsoft.TimeWindow"
TargetingFilterName = "Microsoft.Targeting"
AIMimeProfile = "https://azconfig.io/mime-profiles/ai"
AIChatCompletionMimeProfile = "https://azconfig.io/mime-profiles/ai/chat-completion"
DelimiterPlus = "+"
DelimiterComma = ","
CorrelationContextHeader = "Correlation-Context"
)
type Options struct {
Enabled bool
InitialLoadFinished bool
Host HostType
KeyVaultConfigured bool
KeyVaultRefreshConfigured bool
UseAIConfiguration bool
UseAIChatCompletionConfiguration bool
UseSnapshotReference bool
IsFailoverRequest bool
ReplicaCount int
IsLoadBalancingEnabled bool
FeatureFlagTracing *FeatureFlagTracing
FMVersion string
}
func GetHostType() HostType {
if _, ok := os.LookupEnv(EnvVarAzureFunction); ok {
return HostTypeAzureFunction
} else if _, ok := os.LookupEnv(EnvVarAzureWebApp); ok {
return HostTypeAzureWebApp
} else if _, ok := os.LookupEnv(EnvVarContainerApp); ok {
return HostTypeContainerApp
} else if _, ok := os.LookupEnv(EnvVarKubernetes); ok {
return HostTypeKubernetes
} else if _, ok := os.LookupEnv(EnvVarServiceFabric); ok {
return HostTypeServiceFabric
}
return ""
}
func GetFeatureManagementVersion() string {
return os.Getenv(FMGoVerEnv)
}
func CreateCorrelationContextHeader(ctx context.Context, options Options) http.Header {
header := http.Header{}
output := make([]string, 0)
if !options.InitialLoadFinished {
output = append(output, RequestTypeKey+"="+string(RequestTypeStartUp))
} else {
output = append(output, RequestTypeKey+"="+string(RequestTypeWatch))
}
if options.Host != "" {
output = append(output, HostTypeKey+"="+string(options.Host))
}
if options.ReplicaCount > 0 {
output = append(output, ReplicaCountKey+"="+strconv.Itoa(options.ReplicaCount))
}
if options.KeyVaultConfigured {
output = append(output, KeyVaultConfiguredTag)
}
if options.KeyVaultRefreshConfigured {
output = append(output, KeyVaultRefreshConfiguredTag)
}
if options.IsFailoverRequest {
output = append(output, FailoverRequestTag)
}
features := make([]string, 0)
if options.UseAIConfiguration {
features = append(features, AIConfigurationTag)
}
if options.UseAIChatCompletionConfiguration {
features = append(features, AIChatCompletionConfigurationTag)
}
if options.IsLoadBalancingEnabled {
features = append(features, LoadBalancingEnabledTag)
}
if options.UseSnapshotReference {
features = append(features, SnapshotReferenceTag)
}
if len(features) > 0 {
featureStr := FeaturesKey + "=" + strings.Join(features, DelimiterPlus)
output = append(output, featureStr)
}
if options.FMVersion != "" {
output = append(output, FMGoVerKey+"="+options.FMVersion)
}
if options.FeatureFlagTracing != nil {
if options.FeatureFlagTracing.UsesAnyFeatureFilter() {
output = append(output, FeatureFilterTypeKey+"="+options.FeatureFlagTracing.CreateFeatureFiltersString())
}
if options.FeatureFlagTracing.UsesAnyTracingFeature() {
output = append(output, FFFeaturesKey+"="+options.FeatureFlagTracing.CreateFeaturesString())
}
if options.FeatureFlagTracing.MaxVariants > 0 {
output = append(output, FFMaxVariantsKey+"="+strconv.Itoa(options.FeatureFlagTracing.MaxVariants))
}
}
header.Add(CorrelationContextHeader, strings.Join(output, DelimiterComma))
return header
}