| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package gcp provides a resource detector for GCP Cloud Function. |
| 5 |
package gcp // import "go.opentelemetry.io/contrib/detectors/gcp" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"os" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/sdk/resource" |
| 13 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
gcpFunctionNameKey = "K_SERVICE" |
| 18 |
) |
| 19 |
|
| 20 |
// NewCloudFunction will return a GCP Cloud Function resource detector. |
| 21 |
// |
| 22 |
// Deprecated: Use gcp.NewDetector() instead, which sets the same resource attributes. |
| 23 |
func NewCloudFunction() resource.Detector { |
| 24 |
return &cloudFunction{ |
| 25 |
cloudRun: NewCloudRun(), |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// cloudFunction collects resource information of GCP Cloud Function. |
| 30 |
type cloudFunction struct { |
| 31 |
cloudRun *CloudRun |
| 32 |
} |
| 33 |
|
| 34 |
// Detect detects associated resources when running in GCP Cloud Function. |
| 35 |
func (f *cloudFunction) Detect(context.Context) (*resource.Resource, error) { |
| 36 |
functionName, ok := f.googleCloudFunctionName() |
| 37 |
if !ok { |
| 38 |
return nil, nil |
| 39 |
} |
| 40 |
|
| 41 |
projectID, err := f.cloudRun.mc.ProjectID() |
| 42 |
if err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
region, err := f.cloudRun.cloudRegion() |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
attributes := []attribute.KeyValue{ |
| 51 |
semconv.CloudProviderGCP, |
| 52 |
semconv.CloudPlatformGCPCloudFunctions, |
| 53 |
semconv.FaaSName(functionName), |
| 54 |
semconv.CloudAccountID(projectID), |
| 55 |
semconv.CloudRegion(region), |
| 56 |
} |
| 57 |
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 58 |
} |
| 59 |
|
| 60 |
func (*cloudFunction) googleCloudFunctionName() (string, bool) { |
| 61 |
return os.LookupEnv(gcpFunctionNameKey) |
| 62 |
} |