Debian Codegraph @

service/internal/resource/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package resource // import "go.opentelemetry.io/collector/service/internal/resource"
5
6 import (
7 "github.com/google/uuid"
8 "go.opentelemetry.io/otel/attribute"
9 "go.opentelemetry.io/otel/sdk/resource"
10 semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
11
12 "go.opentelemetry.io/collector/component"
13 )
14
15 // New resource from telemetry configuration.
16 func New(buildInfo component.BuildInfo, resourceCfg map[string]*string) *resource.Resource {
17 var telAttrs []attribute.KeyValue
18
19 for k, v := range resourceCfg {
20 // nil value indicates that the attribute should not be included in the telemetry.
21 if v != nil {
22 telAttrs = append(telAttrs, attribute.String(k, *v))
23 }
24 }
25
26 if _, ok := resourceCfg[string(semconv.ServiceNameKey)]; !ok {
27 // AttributeServiceName is not specified in the config. Use the default service name.
28 telAttrs = append(telAttrs, semconv.ServiceNameKey.String(buildInfo.Command))
29 }
30
31 if _, ok := resourceCfg[string(semconv.ServiceInstanceIDKey)]; !ok {
32 // AttributeServiceInstanceID is not specified in the config. Auto-generate one.
33 instanceUUID, _ := uuid.NewRandom()
34 instanceID := instanceUUID.String()
35 telAttrs = append(telAttrs, semconv.ServiceInstanceIDKey.String(instanceID))
36 }
37
38 if _, ok := resourceCfg[string(semconv.ServiceVersionKey)]; !ok {
39 // AttributeServiceVersion is not specified in the config. Use the actual
40 // build version.
41 telAttrs = append(telAttrs, semconv.ServiceVersionKey.String(buildInfo.Version))
42 }
43 return resource.NewWithAttributes(semconv.SchemaURL, telAttrs...)
44 }