Skip to content

Commit

Permalink
Add Host and RequestType in correlation context
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChen820 committed Jun 21, 2024
1 parent 5b4b626 commit e9dfdeb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 56 deletions.
4 changes: 4 additions & 0 deletions internal/controller/appconfigurationprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func (reconciler *AzureAppConfigurationProviderReconciler) Reconcile(ctx context
retriever = reconciler.Retriever
}

ctx = context.WithValue(ctx, loader.RequestTracingKey, loader.RequestTracing{
IsStartUp: reconciler.ProvidersReconcileState[req.NamespacedName].ConfigMapResourceVersion == nil,
})

// Initialize the processor setting in this reconcile
processor := &AppConfigurationProviderProcessor{
Context: ctx,
Expand Down
58 changes: 2 additions & 56 deletions internal/loader/configuration_setting_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -92,7 +91,6 @@ const (
CertTypePfx string = "application/x-pkcs12"
TlsKey string = "tls.key"
TlsCrt string = "tls.crt"
AzureExtensionContext string = "AZURE_EXTENSION_CONTEXT"
RequestTracingEnabled string = "REQUEST_TRACING_ENABLED"
)

Expand Down Expand Up @@ -462,12 +460,8 @@ func (csl *ConfigurationSettingLoader) ExecuteFailoverPolicy(ctx context.Context
}

if value, ok := os.LookupEnv(RequestTracingEnabled); ok {
enabled := true
enabled, _ = strconv.ParseBool(value)

if enabled {

ctx = policy.WithHTTPHeader(ctx, createCorrelationContextHeader(csl.AzureAppConfigurationProvider, csl.ClientManager))
if enabled, _ := strconv.ParseBool(value); enabled {
ctx = policy.WithHTTPHeader(ctx, createCorrelationContextHeader(ctx, csl.AzureAppConfigurationProvider, csl.ClientManager))
}
}

Expand Down Expand Up @@ -759,51 +753,3 @@ func MergeSecret(secret map[string]corev1.Secret, newSecret map[string]corev1.Se

return nil
}

func createCorrelationContextHeader(provider acpv1.AzureAppConfigurationProvider, clientManager ClientManager) http.Header {
header := http.Header{}
output := make([]string, 0)

if provider.Spec.Configuration.Refresh != nil {
output = append(output, "RefreshesKeyValue")
}

if provider.Spec.Secret != nil {
output = append(output, "UsesKeyVault")

if provider.Spec.Secret.Refresh != nil &&
provider.Spec.Secret.Refresh.Enabled {
output = append(output, "RefreshesKeyVault")
}
}

if provider.Spec.FeatureFlag != nil {
output = append(output, "UsesFeatureFlag")

if provider.Spec.FeatureFlag.Refresh != nil &&
provider.Spec.FeatureFlag.Refresh.Enabled {
output = append(output, "RefreshesFeatureFlag")
}
}

if provider.Spec.ReplicaDiscoveryEnabled {
if manager, ok := clientManager.(*ConfigurationClientManager); ok {
replicaCount := 0
if manager.DynamicClientWrappers != nil {
replicaCount = len(manager.DynamicClientWrappers)
}

output = append(output, fmt.Sprintf("ReplicaCount=%d", replicaCount))
}
}

if _, ok := os.LookupEnv(AzureExtensionContext); ok {
output = append(output, "InstalledBy=Extension")
} else {
output = append(output, "InstalledBy=Helm")
}

header.Add("Correlation-Context", strings.Join(output, ","))

return header
}
78 changes: 78 additions & 0 deletions internal/loader/request_tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package loader

import (
acpv1 "azappconfig/provider/api/v1"
"context"
"fmt"
"net/http"
"os"
"strings"
)

type TracingKey string

type RequestTracing struct {
IsStartUp bool
}

const (
RequestTracingKey TracingKey = TracingKey("tracing")
AzureExtensionContext string = "AZURE_EXTENSION_CONTEXT"
)

func createCorrelationContextHeader(ctx context.Context, provider acpv1.AzureAppConfigurationProvider, clientManager ClientManager) http.Header {
header := http.Header{}
output := make([]string, 0)

output = append(output, "Host=Kubernetes")

if tracing := ctx.Value(RequestTracingKey); tracing != nil {
if tracing.(RequestTracing).IsStartUp {
output = append(output, "RequestType=StartUp")
} else {
output = append(output, "RequestType=Watch")
}
}

if provider.Spec.Configuration.Refresh != nil &&
provider.Spec.Configuration.Refresh.Enabled {
output = append(output, "RefreshesKeyValue")
}

if provider.Spec.Secret != nil {
output = append(output, "UsesKeyVault")

if provider.Spec.Secret.Refresh != nil &&
provider.Spec.Secret.Refresh.Enabled {
output = append(output, "RefreshesKeyVault")
}
}

if provider.Spec.FeatureFlag != nil {
output = append(output, "UsesFeatureFlag")
}

if provider.Spec.ReplicaDiscoveryEnabled {
if manager, ok := clientManager.(*ConfigurationClientManager); ok {
replicaCount := 0
if manager.DynamicClientWrappers != nil {
replicaCount = len(manager.DynamicClientWrappers)
}

output = append(output, fmt.Sprintf("ReplicaCount=%d", replicaCount))
}
}

if _, ok := os.LookupEnv(AzureExtensionContext); ok {
output = append(output, "InstalledBy=Extension")
} else {
output = append(output, "InstalledBy=Helm")
}

header.Add("Correlation-Context", strings.Join(output, ","))

return header
}

0 comments on commit e9dfdeb

Please sign in to comment.