Debian Codegraph @

detectors/gcp/cloud-run.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package gcp // import "go.opentelemetry.io/contrib/detectors/gcp"
5
6 import (
7 "context"
8 "fmt"
9 "os"
10 "strings"
11
12 "cloud.google.com/go/compute/metadata"
13 "go.opentelemetry.io/otel/attribute"
14 "go.opentelemetry.io/otel/sdk/resource"
15 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
16 )
17
18 const serviceNamespace = "cloud-run-managed"
19
20 // The minimal list of metadata.Client methods we use. Use an interface so we
21 // can replace it with a fake implementation in the unit test.
22 type metadataClient interface {
23 ProjectID() (string, error)
24 Get(string) (string, error)
25 InstanceID() (string, error)
26 }
27
28 // CloudRun collects resource information of Cloud Run instance.
29 //
30 // Deprecated: Use gcp.NewDetector() instead. Note that it sets faas.* resource attributes instead of service.* attributes.
31 type CloudRun struct {
32 mc metadataClient
33 onGCE func() bool
34 getenv func(string) string
35 }
36
37 // compile time assertion that CloudRun implements the resource.Detector
38 // interface.
39 var _ resource.Detector = (*CloudRun)(nil)
40
41 // NewCloudRun creates a CloudRun detector.
42 //
43 // Deprecated: Use gcp.NewDetector() instead. Note that it sets faas.* resource attributes instead of service.* attributes.
44 func NewCloudRun() *CloudRun {
45 return &CloudRun{
46 mc: metadata.NewClient(nil),
47 onGCE: metadata.OnGCE,
48 getenv: os.Getenv,
49 }
50 }
51
52 func (c *CloudRun) cloudRegion() (string, error) {
53 region, err := c.mc.Get("instance/region")
54 if err != nil {
55 return "", err
56 }
57 // Region from the metadata server is in the format /projects/123/regions/r.
58 // https://cloud.google.com/run/docs/reference/container-contract#metadata-server
59 return region[strings.LastIndex(region, "/")+1:], nil
60 }
61
62 // Detect detects associated resources when running on Cloud Run hosts.
63 // NOTE: the service.namespace attribute is currently hardcoded to be
64 // "cloud-run-managed". This may change in the future, please do not rely on
65 // this behavior yet.
66 func (c *CloudRun) Detect(context.Context) (*resource.Resource, error) {
67 // .OnGCE is actually testing whether the metadata server is available.
68 // Metadata server is supported on Cloud Run.
69 if !c.onGCE() {
70 return nil, nil
71 }
72
73 attributes := []attribute.KeyValue{
74 semconv.CloudProviderGCP,
75 semconv.ServiceNamespace(serviceNamespace),
76 }
77
78 var errInfo []string
79
80 if projectID, err := c.mc.ProjectID(); hasProblem(err) {
81 errInfo = append(errInfo, err.Error())
82 } else if projectID != "" {
83 attributes = append(attributes, semconv.CloudAccountID(projectID))
84 }
85
86 if region, err := c.cloudRegion(); hasProblem(err) {
87 errInfo = append(errInfo, err.Error())
88 } else if region != "" {
89 attributes = append(attributes, semconv.CloudRegion(region))
90 }
91
92 if instanceID, err := c.mc.InstanceID(); hasProblem(err) {
93 errInfo = append(errInfo, err.Error())
94 } else if instanceID != "" {
95 attributes = append(attributes, semconv.ServiceInstanceID(instanceID))
96 }
97
98 // Part of Cloud Run container runtime contract.
99 // See https://cloud.google.com/run/docs/reference/container-contract
100 if service := c.getenv("K_SERVICE"); service == "" {
101 errInfo = append(errInfo, "envvar K_SERVICE contains empty string.")
102 } else {
103 attributes = append(attributes, semconv.ServiceName(service))
104 }
105 res := resource.NewWithAttributes(semconv.SchemaURL, attributes...)
106
107 var aggregatedErr error
108 if len(errInfo) > 0 {
109 aggregatedErr = fmt.Errorf("detecting Cloud Run resources: %s", errInfo)
110 }
111
112 return res, aggregatedErr
113 }