-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathutil.go
More file actions
114 lines (99 loc) · 3.46 KB
/
util.go
File metadata and controls
114 lines (99 loc) · 3.46 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awscloudwatchlogsexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter"
import (
"fmt"
"regexp"
"strconv"
"strings"
"go.uber.org/zap"
)
var patternKeyToAttributeMap = map[string]string{
"ClusterName": "aws.ecs.cluster.name",
"TaskId": "aws.ecs.task.id",
"NodeName": "k8s.node.name",
"PodName": "pod",
"ServiceName": "service.name",
"ContainerInstanceId": "aws.ecs.container.instance.id",
"TaskDefinitionFamily": "aws.ecs.task.family",
"InstanceId": "service.instance.id",
"FaasName": "faas.name",
"FaasVersion": "faas.version",
}
func isPatternValid(s string) (bool, string) {
if !strings.Contains(s, "{") && !strings.Contains(s, "}") {
return true, ""
}
re := regexp.MustCompile(`\{([^{}]*)\}`)
matches := re.FindAllStringSubmatch(s, -1)
for _, match := range matches {
if len(match) > 1 {
key := match[1]
if _, exists := patternKeyToAttributeMap[key]; !exists {
return false, key
}
}
}
return true, ""
}
func replacePatterns(s string, attrMap map[string]string, logger *zap.Logger) (string, bool) {
success := true
var foundAndReplaced bool
for key := range patternKeyToAttributeMap {
s, foundAndReplaced = replacePatternWithAttrValue(s, key, attrMap, logger)
success = success && foundAndReplaced
}
return s, success
}
func replacePatternWithAttrValue(s, patternKey string, attrMap map[string]string, logger *zap.Logger) (string, bool) {
pattern := "{" + patternKey + "}"
if strings.Contains(s, pattern) {
if value, ok := attrMap[patternKey]; ok {
return replace(s, pattern, value, logger)
} else if value, ok := attrMap[patternKeyToAttributeMap[patternKey]]; ok {
return replace(s, pattern, value, logger)
}
logger.Debug("No resource attribute found for pattern " + pattern)
return strings.ReplaceAll(s, pattern, "undefined"), false
}
return s, true
}
func replace(s, pattern, value string, logger *zap.Logger) (string, bool) {
if value == "" {
logger.Debug("Empty resource attribute value found for pattern " + pattern)
return strings.ReplaceAll(s, pattern, "undefined"), false
}
return strings.ReplaceAll(s, pattern, value), true
}
// getLogInfo retrieves the log group and log stream names from a given set of metrics.
func getLogInfo(resourceAttrs map[string]any, config *Config) (string, string, bool) {
var logGroup, logStream string
groupReplaced := true
streamReplaced := true
// Convert to map[string]string
strAttributeMap := anyMapToStringMap(resourceAttrs)
// Override log group/stream if specified in config. However, in this case, customer won't have correlation experience
if config.LogGroupName != "" {
logGroup, groupReplaced = replacePatterns(config.LogGroupName, strAttributeMap, config.logger)
}
if config.LogStreamName != "" {
logStream, streamReplaced = replacePatterns(config.LogStreamName, strAttributeMap, config.logger)
}
return logGroup, logStream, (groupReplaced && streamReplaced)
}
func anyMapToStringMap(resourceAttrs map[string]any) map[string]string {
strMap := make(map[string]string)
for key, value := range resourceAttrs {
switch v := value.(type) {
case string:
strMap[key] = v
case int:
strMap[key] = strconv.Itoa(v)
case bool:
strMap[key] = strconv.FormatBool(v)
default:
strMap[key] = fmt.Sprintf("%v", v)
}
}
return strMap
}