|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package awscloudwatchlogsexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter" |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "go.uber.org/zap" |
| 13 | +) |
| 14 | + |
| 15 | +var patternKeyToAttributeMap = map[string]string{ |
| 16 | + "ClusterName": "aws.ecs.cluster.name", |
| 17 | + "TaskId": "aws.ecs.task.id", |
| 18 | + "NodeName": "k8s.node.name", |
| 19 | + "PodName": "pod", |
| 20 | + "ServiceName": "service.name", |
| 21 | + "ContainerInstanceId": "aws.ecs.container.instance.id", |
| 22 | + "TaskDefinitionFamily": "aws.ecs.task.family", |
| 23 | + "InstanceId": "service.instance.id", |
| 24 | + "FaasName": "faas.name", |
| 25 | + "FaasVersion": "faas.version", |
| 26 | +} |
| 27 | + |
| 28 | +func isPatternValid(s string) (bool, string) { |
| 29 | + if !strings.Contains(s, "{") && !strings.Contains(s, "}") { |
| 30 | + return true, "" |
| 31 | + } |
| 32 | + |
| 33 | + re := regexp.MustCompile(`\{([^{}]*)\}`) |
| 34 | + matches := re.FindAllStringSubmatch(s, -1) |
| 35 | + |
| 36 | + for _, match := range matches { |
| 37 | + if len(match) > 1 { |
| 38 | + key := match[1] |
| 39 | + if _, exists := patternKeyToAttributeMap[key]; !exists { |
| 40 | + return false, key |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return true, "" |
| 45 | +} |
| 46 | + |
| 47 | +func replacePatterns(s string, attrMap map[string]string, logger *zap.Logger) (string, bool) { |
| 48 | + success := true |
| 49 | + var foundAndReplaced bool |
| 50 | + for key := range patternKeyToAttributeMap { |
| 51 | + s, foundAndReplaced = replacePatternWithAttrValue(s, key, attrMap, logger) |
| 52 | + success = success && foundAndReplaced |
| 53 | + } |
| 54 | + return s, success |
| 55 | +} |
| 56 | + |
| 57 | +func replacePatternWithAttrValue(s, patternKey string, attrMap map[string]string, logger *zap.Logger) (string, bool) { |
| 58 | + pattern := "{" + patternKey + "}" |
| 59 | + if strings.Contains(s, pattern) { |
| 60 | + if value, ok := attrMap[patternKey]; ok { |
| 61 | + return replace(s, pattern, value, logger) |
| 62 | + } else if value, ok := attrMap[patternKeyToAttributeMap[patternKey]]; ok { |
| 63 | + return replace(s, pattern, value, logger) |
| 64 | + } |
| 65 | + logger.Debug("No resource attribute found for pattern " + pattern) |
| 66 | + return strings.ReplaceAll(s, pattern, "undefined"), false |
| 67 | + } |
| 68 | + return s, true |
| 69 | +} |
| 70 | + |
| 71 | +func replace(s, pattern string, value string, logger *zap.Logger) (string, bool) { |
| 72 | + if value == "" { |
| 73 | + logger.Debug("Empty resource attribute value found for pattern " + pattern) |
| 74 | + return strings.ReplaceAll(s, pattern, "undefined"), false |
| 75 | + } |
| 76 | + return strings.ReplaceAll(s, pattern, value), true |
| 77 | +} |
| 78 | + |
| 79 | +// getLogInfo retrieves the log group and log stream names from a given set of metrics. |
| 80 | +func getLogInfo(resourceAttrs map[string]any, config *Config) (string, string, bool) { |
| 81 | + var logGroup, logStream string |
| 82 | + groupReplaced := true |
| 83 | + streamReplaced := true |
| 84 | + |
| 85 | + // Convert to map[string]string |
| 86 | + strAttributeMap := anyMapToStringMap(resourceAttrs) |
| 87 | + |
| 88 | + // Override log group/stream if specified in config. However, in this case, customer won't have correlation experience |
| 89 | + if len(config.LogGroupName) > 0 { |
| 90 | + logGroup, groupReplaced = replacePatterns(config.LogGroupName, strAttributeMap, config.logger) |
| 91 | + } |
| 92 | + if len(config.LogStreamName) > 0 { |
| 93 | + logStream, streamReplaced = replacePatterns(config.LogStreamName, strAttributeMap, config.logger) |
| 94 | + } |
| 95 | + |
| 96 | + return logGroup, logStream, (groupReplaced && streamReplaced) |
| 97 | +} |
| 98 | + |
| 99 | +func anyMapToStringMap(resourceAttrs map[string]any) map[string]string { |
| 100 | + strMap := make(map[string]string) |
| 101 | + for key, value := range resourceAttrs { |
| 102 | + switch v := value.(type) { |
| 103 | + case string: |
| 104 | + strMap[key] = v |
| 105 | + case int: |
| 106 | + strMap[key] = strconv.Itoa(v) |
| 107 | + case bool: |
| 108 | + strMap[key] = strconv.FormatBool(v) |
| 109 | + default: |
| 110 | + strMap[key] = fmt.Sprintf("%v", v) |
| 111 | + } |
| 112 | + } |
| 113 | + return strMap |
| 114 | +} |
0 commit comments