-
Notifications
You must be signed in to change notification settings - Fork 103
/
resourcemapping.go
286 lines (270 loc) · 10.5 KB
/
resourcemapping.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resourcemapping
import (
"strings"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
)
const (
ProjectIDAttributeKey = "gcp.project.id"
awsAccount = "aws_account"
awsEc2Instance = "aws_ec2_instance"
clusterName = "cluster_name"
containerName = "container_name"
gceInstance = "gce_instance"
genericNode = "generic_node"
genericTask = "generic_task"
instanceID = "instance_id"
job = "job"
k8sCluster = "k8s_cluster"
k8sContainer = "k8s_container"
k8sNode = "k8s_node"
k8sPod = "k8s_pod"
location = "location"
namespace = "namespace"
namespaceName = "namespace_name"
nodeID = "node_id"
nodeName = "node_name"
podName = "pod_name"
region = "region"
taskID = "task_id"
zone = "zone"
gaeInstance = "gae_instance"
gaeApp = "gae_app"
gaeModuleID = "module_id"
gaeVersionID = "version_id"
cloudRunRevision = "cloud_run_revision"
cloudFunction = "cloud_function"
cloudFunctionName = "function_name"
serviceName = "service_name"
configurationName = "configuration_name"
revisionName = "revision_name"
bmsInstance = "baremetalsolution.googleapis.com/Instance"
unknownServicePrefix = "unknown_service"
)
var (
// monitoredResourceMappings contains mappings of GCM resource label keys onto mapping config from OTel
// resource for a given monitored resource type.
monitoredResourceMappings = map[string]map[string]struct {
// If none of the otelKeys are present in the Resource, fallback to this literal value
fallbackLiteral string
// OTel resource keys to try and populate the resource label from. For entries with
// multiple OTel resource keys, the keys' values will be coalesced in order until there
// is a non-empty value.
otelKeys []string
}{
gceInstance: {
zone: {otelKeys: []string{string(semconv.CloudAvailabilityZoneKey)}},
instanceID: {otelKeys: []string{string(semconv.HostIDKey)}},
},
k8sContainer: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
clusterName: {otelKeys: []string{string(semconv.K8SClusterNameKey)}},
namespaceName: {otelKeys: []string{string(semconv.K8SNamespaceNameKey)}},
podName: {otelKeys: []string{string(semconv.K8SPodNameKey)}},
containerName: {otelKeys: []string{string(semconv.K8SContainerNameKey)}},
},
k8sPod: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
clusterName: {otelKeys: []string{string(semconv.K8SClusterNameKey)}},
namespaceName: {otelKeys: []string{string(semconv.K8SNamespaceNameKey)}},
podName: {otelKeys: []string{string(semconv.K8SPodNameKey)}},
},
k8sNode: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
clusterName: {otelKeys: []string{string(semconv.K8SClusterNameKey)}},
nodeName: {otelKeys: []string{string(semconv.K8SNodeNameKey)}},
},
k8sCluster: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
clusterName: {otelKeys: []string{string(semconv.K8SClusterNameKey)}},
},
gaeInstance: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
gaeModuleID: {otelKeys: []string{string(semconv.FaaSNameKey)}},
gaeVersionID: {otelKeys: []string{string(semconv.FaaSVersionKey)}},
instanceID: {otelKeys: []string{string(semconv.FaaSInstanceKey)}},
},
gaeApp: {
location: {otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
}},
gaeModuleID: {otelKeys: []string{string(semconv.FaaSNameKey)}},
gaeVersionID: {otelKeys: []string{string(semconv.FaaSVersionKey)}},
},
awsEc2Instance: {
instanceID: {otelKeys: []string{string(semconv.HostIDKey)}},
region: {
otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
},
},
awsAccount: {otelKeys: []string{string(semconv.CloudAccountIDKey)}},
},
bmsInstance: {
location: {otelKeys: []string{string(semconv.CloudRegionKey)}},
instanceID: {otelKeys: []string{string(semconv.HostIDKey)}},
},
genericTask: {
location: {
otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
},
fallbackLiteral: "global",
},
namespace: {otelKeys: []string{string(semconv.ServiceNamespaceKey)}},
job: {otelKeys: []string{string(semconv.ServiceNameKey), string(semconv.FaaSNameKey)}},
taskID: {otelKeys: []string{string(semconv.ServiceInstanceIDKey), string(semconv.FaaSInstanceKey)}},
},
genericNode: {
location: {
otelKeys: []string{
string(semconv.CloudAvailabilityZoneKey),
string(semconv.CloudRegionKey),
},
fallbackLiteral: "global",
},
namespace: {otelKeys: []string{string(semconv.ServiceNamespaceKey)}},
nodeID: {otelKeys: []string{string(semconv.HostIDKey), string(semconv.HostNameKey)}},
},
}
)
// ReadOnlyAttributes is an interface to abstract between pulling attributes from PData library or OTEL SDK.
type ReadOnlyAttributes interface {
GetString(string) (string, bool)
}
// ResourceAttributesToLoggingMonitoredResource converts from a set of OTEL resource attributes into a
// GCP monitored resource type and label set for Cloud Logging.
// E.g.
// This may output `gce_instance` type with appropriate labels.
func ResourceAttributesToLoggingMonitoredResource(attrs ReadOnlyAttributes) *monitoredrespb.MonitoredResource {
cloudPlatform, _ := attrs.GetString(string(semconv.CloudPlatformKey))
switch cloudPlatform {
case semconv.CloudPlatformGCPAppEngine.Value.AsString():
return createMonitoredResource(gaeApp, attrs)
default:
return commonResourceAttributesToMonitoredResource(cloudPlatform, attrs)
}
}
// ResourceAttributesToMonitoringMonitoredResource converts from a set of OTEL resource attributes into a
// GCP monitored resource type and label set for Cloud Monitoring
// E.g.
// This may output `gce_instance` type with appropriate labels.
func ResourceAttributesToMonitoringMonitoredResource(attrs ReadOnlyAttributes) *monitoredrespb.MonitoredResource {
cloudPlatform, _ := attrs.GetString(string(semconv.CloudPlatformKey))
switch cloudPlatform {
case semconv.CloudPlatformGCPAppEngine.Value.AsString():
return createMonitoredResource(gaeInstance, attrs)
default:
return commonResourceAttributesToMonitoredResource(cloudPlatform, attrs)
}
}
func commonResourceAttributesToMonitoredResource(cloudPlatform string, attrs ReadOnlyAttributes) *monitoredrespb.MonitoredResource {
switch cloudPlatform {
case semconv.CloudPlatformGCPComputeEngine.Value.AsString():
return createMonitoredResource(gceInstance, attrs)
case semconv.CloudPlatformAWSEC2.Value.AsString():
return createMonitoredResource(awsEc2Instance, attrs)
// TODO(alex-basinov): replace this string literal with semconv.CloudPlatformGCPBareMetalSolution
// once https://github.com/open-telemetry/semantic-conventions/pull/64 makes its way
// into the semconv module.
case "gcp_bare_metal_solution":
return createMonitoredResource(bmsInstance, attrs)
default:
// if k8s.cluster.name is set, pattern match for various k8s resources.
// this will also match non-cloud k8s platforms like minikube.
if _, ok := attrs.GetString(string(semconv.K8SClusterNameKey)); ok {
// Try for most to least specific k8s_container, k8s_pod, etc
if _, ok := attrs.GetString(string(semconv.K8SContainerNameKey)); ok {
return createMonitoredResource(k8sContainer, attrs)
} else if _, ok := attrs.GetString(string(semconv.K8SPodNameKey)); ok {
return createMonitoredResource(k8sPod, attrs)
} else if _, ok := attrs.GetString(string(semconv.K8SNodeNameKey)); ok {
return createMonitoredResource(k8sNode, attrs)
} else {
return createMonitoredResource(k8sCluster, attrs)
}
}
// Fallback to generic_task
_, hasServiceName := attrs.GetString(string(semconv.ServiceNameKey))
_, hasFaaSName := attrs.GetString(string(semconv.FaaSNameKey))
_, hasServiceInstanceID := attrs.GetString(string(semconv.ServiceInstanceIDKey))
_, hasFaaSInstance := attrs.GetString(string(semconv.FaaSInstanceKey))
if (hasServiceName && hasServiceInstanceID) || (hasFaaSInstance && hasFaaSName) {
return createMonitoredResource(genericTask, attrs)
}
// Everything else fallback to generic_node
return createMonitoredResource(genericNode, attrs)
}
}
func createMonitoredResource(
monitoredResourceType string,
resourceAttrs ReadOnlyAttributes,
) *monitoredrespb.MonitoredResource {
mappings := monitoredResourceMappings[monitoredResourceType]
mrLabels := make(map[string]string, len(mappings))
for mrKey, mappingConfig := range mappings {
mrValue := ""
ok := false
// Coalesce the possible keys in order
for _, otelKey := range mappingConfig.otelKeys {
mrValue, ok = resourceAttrs.GetString(otelKey)
if mrValue != "" && !strings.HasPrefix(mrValue, unknownServicePrefix) {
break
}
}
if mrValue == "" && contains(mappingConfig.otelKeys, string(semconv.ServiceNameKey)) {
// the service name started with unknown_service, and was ignored above
mrValue, ok = resourceAttrs.GetString(string(semconv.ServiceNameKey))
}
if !ok || mrValue == "" {
mrValue = mappingConfig.fallbackLiteral
}
mrLabels[mrKey] = sanitizeUTF8(mrValue)
}
return &monitoredrespb.MonitoredResource{
Type: monitoredResourceType,
Labels: mrLabels,
}
}
func contains(list []string, element string) bool {
for _, item := range list {
if item == element {
return true
}
}
return false
}
func sanitizeUTF8(s string) string {
return strings.ToValidUTF8(s, "�")
}