|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "slices" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/aws/amazon-cloudwatch-agent/cfg/envconfig" |
| 15 | + "github.com/aws/amazon-cloudwatch-agent/internal/merge/confmap" |
| 16 | + agenthealthtranslator "github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/agenthealth" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + featureFlagOtelMergeYAML = "otel_merge_yaml" |
| 21 | + featureFlagOtelMergeJSON = "otel_merge_json" |
| 22 | +) |
| 23 | + |
| 24 | +// mergeConfigs merges multiple OTEL configs together, including any config |
| 25 | +// provided via the CW_CONFIG_CONTENT environment variable when running in a |
| 26 | +// container. Returns nil without an error if there is nothing to merge (i.e. |
| 27 | +// a single config path with no env override). In practice, a single config |
| 28 | +// means no custom YAML was provided — the default agent YAML is always |
| 29 | +// accompanied by at least one custom YAML when custom configs are in use. |
| 30 | +func mergeConfigs(configPaths []string, isUsageDataEnabled bool) (*confmap.Conf, error) { |
| 31 | + var loaders []confmap.Loader |
| 32 | + if envconfig.IsRunningInContainer() { |
| 33 | + content, ok := os.LookupEnv(envconfig.CWOtelConfigContent) |
| 34 | + if ok && len(content) > 0 { |
| 35 | + log.Printf("D! Merging OTEL configuration from: %s", envconfig.CWOtelConfigContent) |
| 36 | + loaders = append(loaders, confmap.NewByteLoader(envconfig.CWOtelConfigContent, []byte(content))) |
| 37 | + } |
| 38 | + } |
| 39 | + // If using environment variable or passing in more than one config |
| 40 | + if len(loaders) > 0 || len(configPaths) > 1 { |
| 41 | + log.Printf("D! Merging OTEL configurations from: %s", configPaths) |
| 42 | + for _, configPath := range configPaths { |
| 43 | + loaders = append(loaders, confmap.NewFileLoader(configPath)) |
| 44 | + } |
| 45 | + var result *confmap.Conf |
| 46 | + for _, loader := range loaders { |
| 47 | + conf, err := loader.Load() |
| 48 | + if err != nil { |
| 49 | + if errors.Is(err, os.ErrNotExist) { |
| 50 | + log.Printf("D! Skipping non-existent OTEL config: %s", loader.ID()) |
| 51 | + continue |
| 52 | + } |
| 53 | + return nil, fmt.Errorf("failed to load OTEL configs: %w", err) |
| 54 | + } |
| 55 | + if result == nil { |
| 56 | + result = confmap.New() |
| 57 | + } |
| 58 | + if err = result.Merge(conf); err != nil { |
| 59 | + return nil, fmt.Errorf("failed to merge OTEL configs: %w", err) |
| 60 | + } |
| 61 | + } |
| 62 | + return mergeAgentHealth(result, isUsageDataEnabled), nil |
| 63 | + } |
| 64 | + return nil, nil |
| 65 | +} |
| 66 | + |
| 67 | +type exporterInfo struct { |
| 68 | + middlewareID string |
| 69 | + operations []any |
| 70 | +} |
| 71 | + |
| 72 | +var logsExporterInfo = exporterInfo{middlewareID: agenthealthtranslator.LogsID.String(), operations: []any{agenthealthtranslator.OperationPutLogEvents}} |
| 73 | + |
| 74 | +// supportedExporters maps exporter type names to their agenthealth middleware ID and operations. |
| 75 | +var supportedExporters = map[string]exporterInfo{ |
| 76 | + "awscloudwatch": {middlewareID: agenthealthtranslator.MetricsID.String(), operations: []any{agenthealthtranslator.OperationPutMetricData}}, |
| 77 | + "awsemf": logsExporterInfo, |
| 78 | + "awscloudwatchlogs": logsExporterInfo, |
| 79 | + "awsxray": {middlewareID: agenthealthtranslator.TracesID.String(), operations: []any{agenthealthtranslator.OperationPutTraceSegments}}, |
| 80 | +} |
| 81 | + |
| 82 | +// mergeAgentHealth scans the exporters in the config for supported AWS exporters |
| 83 | +// and adds the appropriate agenthealth extension with a middleware reference to each. |
| 84 | +// It also detects otlphttp exporters and sets their auth.authenticator to an |
| 85 | +// agenthealth extension, chaining with any existing auth extension. |
| 86 | +func mergeAgentHealth(conf *confmap.Conf, isUsageDataEnabled bool) *confmap.Conf { |
| 87 | + if conf == nil || !isUsageDataEnabled { |
| 88 | + return conf |
| 89 | + } |
| 90 | + |
| 91 | + cfgMap := conf.ToStringMap() |
| 92 | + |
| 93 | + exporters, ok := cfgMap["exporters"].(map[string]any) |
| 94 | + if !ok { |
| 95 | + return conf |
| 96 | + } |
| 97 | + |
| 98 | + // Track which agenthealth extensions are needed for AWS exporters |
| 99 | + neededExtensions := make(map[string]exporterInfo) |
| 100 | + for key := range exporters { |
| 101 | + typeName, _, _ := strings.Cut(key, "/") |
| 102 | + info, ok := supportedExporters[typeName] |
| 103 | + if !ok { |
| 104 | + continue |
| 105 | + } |
| 106 | + exporterCfg, ok := exporters[key].(map[string]any) |
| 107 | + if !ok || exporterCfg == nil { |
| 108 | + exporterCfg = make(map[string]any) |
| 109 | + exporters[key] = exporterCfg |
| 110 | + } |
| 111 | + if _, alreadySet := exporterCfg["middleware"]; !alreadySet { |
| 112 | + exporterCfg["middleware"] = info.middlewareID |
| 113 | + neededExtensions[info.middlewareID] = info |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Detect otlphttp exporters for auth-based agenthealth integration |
| 118 | + type otlphttpAuthEntry struct { |
| 119 | + exporterKey string |
| 120 | + ahExtName string |
| 121 | + additionalAuth string |
| 122 | + } |
| 123 | + var otlphttpEntries []otlphttpAuthEntry |
| 124 | + for key := range exporters { |
| 125 | + typeName, suffix, hasSuffix := strings.Cut(key, "/") |
| 126 | + if typeName != "otlphttp" { |
| 127 | + continue |
| 128 | + } |
| 129 | + ahName := "agenthealth/otlphttp" |
| 130 | + if hasSuffix { |
| 131 | + ahName = "agenthealth/otlphttp_" + suffix |
| 132 | + } |
| 133 | + exporterCfg, ok := exporters[key].(map[string]any) |
| 134 | + if !ok || exporterCfg == nil { |
| 135 | + exporterCfg = make(map[string]any) |
| 136 | + exporters[key] = exporterCfg |
| 137 | + } |
| 138 | + // Skip if already using an agenthealth auth extension |
| 139 | + var additionalAuth string |
| 140 | + if authMap, ok := exporterCfg["auth"].(map[string]any); ok { |
| 141 | + if authn, ok := authMap["authenticator"].(string); ok { |
| 142 | + if strings.HasPrefix(authn, "agenthealth/") { |
| 143 | + continue |
| 144 | + } |
| 145 | + additionalAuth = authn |
| 146 | + } |
| 147 | + } |
| 148 | + otlphttpEntries = append(otlphttpEntries, otlphttpAuthEntry{ |
| 149 | + exporterKey: key, |
| 150 | + ahExtName: ahName, |
| 151 | + additionalAuth: additionalAuth, |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + if len(neededExtensions) == 0 && len(otlphttpEntries) == 0 { |
| 156 | + return conf |
| 157 | + } |
| 158 | + |
| 159 | + // Ensure extensions section exists |
| 160 | + extensions, _ := cfgMap["extensions"].(map[string]any) |
| 161 | + if extensions == nil { |
| 162 | + extensions = make(map[string]any) |
| 163 | + cfgMap["extensions"] = extensions |
| 164 | + } |
| 165 | + |
| 166 | + // Ensure service section exists |
| 167 | + service, _ := cfgMap["service"].(map[string]any) |
| 168 | + if service == nil { |
| 169 | + service = make(map[string]any) |
| 170 | + cfgMap["service"] = service |
| 171 | + } |
| 172 | + |
| 173 | + var svcExtensions []any |
| 174 | + if existing, ok := service["extensions"].([]any); ok { |
| 175 | + svcExtensions = existing |
| 176 | + } |
| 177 | + |
| 178 | + for middlewareID, info := range neededExtensions { |
| 179 | + if _, exists := extensions[middlewareID]; !exists { |
| 180 | + extensions[middlewareID] = map[string]any{ |
| 181 | + "is_usage_data_enabled": true, |
| 182 | + "stats": map[string]any{ |
| 183 | + "operations": info.operations, |
| 184 | + }, |
| 185 | + } |
| 186 | + } |
| 187 | + if !slices.Contains(svcExtensions, any(middlewareID)) { |
| 188 | + svcExtensions = append(svcExtensions, middlewareID) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Configure agenthealth auth for otlphttp exporters |
| 193 | + for _, entry := range otlphttpEntries { |
| 194 | + exporterCfg := exporters[entry.exporterKey].(map[string]any) |
| 195 | + exporterCfg["auth"] = map[string]any{"authenticator": entry.ahExtName} |
| 196 | + if _, exists := extensions[entry.ahExtName]; !exists { |
| 197 | + extCfg := map[string]any{ |
| 198 | + "is_usage_data_enabled": true, |
| 199 | + "stats": map[string]any{ |
| 200 | + "operations": []any{"*"}, |
| 201 | + }, |
| 202 | + } |
| 203 | + if entry.additionalAuth != "" { |
| 204 | + extCfg["additional_auth"] = entry.additionalAuth |
| 205 | + } |
| 206 | + extensions[entry.ahExtName] = extCfg |
| 207 | + } |
| 208 | + if !slices.Contains(svcExtensions, any(entry.ahExtName)) { |
| 209 | + svcExtensions = append(svcExtensions, entry.ahExtName) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + service["extensions"] = svcExtensions |
| 214 | + return confmap.NewFromStringMap(cfgMap) |
| 215 | +} |
0 commit comments