| 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 |
"strconv" |
| 10 |
|
| 11 |
"cloud.google.com/go/compute/metadata" |
| 12 |
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp" |
| 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 |
// NewDetector returns a resource detector which detects resource attributes on: |
| 19 |
// * Google Compute Engine (GCE). |
| 20 |
// * Google Kubernetes Engine (GKE). |
| 21 |
// * Google App Engine (GAE). |
| 22 |
// * Cloud Run. |
| 23 |
// * Cloud Functions. |
| 24 |
func NewDetector() resource.Detector { |
| 25 |
return &detector{detector: gcp.NewDetector()} |
| 26 |
} |
| 27 |
|
| 28 |
type detector struct { |
| 29 |
detector gcpDetector |
| 30 |
} |
| 31 |
|
| 32 |
// Detect detects associated resources when running on GCE, GKE, GAE, |
| 33 |
// Cloud Run, and Cloud functions. |
| 34 |
func (d *detector) Detect(context.Context) (*resource.Resource, error) { |
| 35 |
if !metadata.OnGCE() { |
| 36 |
return nil, nil |
| 37 |
} |
| 38 |
b := &resourceBuilder{} |
| 39 |
b.attrs = append(b.attrs, semconv.CloudProviderGCP) |
| 40 |
b.add(semconv.CloudAccountIDKey, d.detector.ProjectID) |
| 41 |
|
| 42 |
switch d.detector.CloudPlatform() { |
| 43 |
case gcp.GKE: |
| 44 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPKubernetesEngine) |
| 45 |
b.addZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion) |
| 46 |
b.add(semconv.K8SClusterNameKey, d.detector.GKEClusterName) |
| 47 |
b.add(semconv.HostIDKey, d.detector.GKEHostID) |
| 48 |
case gcp.CloudRun: |
| 49 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudRun) |
| 50 |
b.add(semconv.FaaSNameKey, d.detector.FaaSName) |
| 51 |
b.add(semconv.FaaSVersionKey, d.detector.FaaSVersion) |
| 52 |
b.add(semconv.FaaSInstanceKey, d.detector.FaaSID) |
| 53 |
b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion) |
| 54 |
case gcp.CloudRunJob: |
| 55 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudRun) |
| 56 |
b.add(semconv.FaaSNameKey, d.detector.FaaSName) |
| 57 |
b.add(semconv.FaaSInstanceKey, d.detector.FaaSID) |
| 58 |
b.add(semconv.GCPCloudRunJobExecutionKey, d.detector.CloudRunJobExecution) |
| 59 |
b.addInt(semconv.GCPCloudRunJobTaskIndexKey, d.detector.CloudRunJobTaskIndex) |
| 60 |
b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion) |
| 61 |
case gcp.CloudFunctions: |
| 62 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudFunctions) |
| 63 |
b.add(semconv.FaaSNameKey, d.detector.FaaSName) |
| 64 |
b.add(semconv.FaaSVersionKey, d.detector.FaaSVersion) |
| 65 |
b.add(semconv.FaaSInstanceKey, d.detector.FaaSID) |
| 66 |
b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion) |
| 67 |
case gcp.AppEngineFlex: |
| 68 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPAppEngine) |
| 69 |
b.addZoneAndRegion(d.detector.AppEngineFlexAvailabilityZoneAndRegion) |
| 70 |
b.add(semconv.FaaSNameKey, d.detector.AppEngineServiceName) |
| 71 |
b.add(semconv.FaaSVersionKey, d.detector.AppEngineServiceVersion) |
| 72 |
b.add(semconv.FaaSInstanceKey, d.detector.AppEngineServiceInstance) |
| 73 |
case gcp.AppEngineStandard: |
| 74 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPAppEngine) |
| 75 |
b.add(semconv.CloudAvailabilityZoneKey, d.detector.AppEngineStandardAvailabilityZone) |
| 76 |
b.add(semconv.CloudRegionKey, d.detector.AppEngineStandardCloudRegion) |
| 77 |
b.add(semconv.FaaSNameKey, d.detector.AppEngineServiceName) |
| 78 |
b.add(semconv.FaaSVersionKey, d.detector.AppEngineServiceVersion) |
| 79 |
b.add(semconv.FaaSInstanceKey, d.detector.AppEngineServiceInstance) |
| 80 |
case gcp.GCE: |
| 81 |
b.attrs = append(b.attrs, semconv.CloudPlatformGCPComputeEngine) |
| 82 |
b.addZoneAndRegion(d.detector.GCEAvailabilityZoneAndRegion) |
| 83 |
b.add(semconv.HostTypeKey, d.detector.GCEHostType) |
| 84 |
b.add(semconv.HostIDKey, d.detector.GCEHostID) |
| 85 |
b.add(semconv.HostNameKey, d.detector.GCEHostName) |
| 86 |
b.add(semconv.GCPGCEInstanceNameKey, d.detector.GCEInstanceName) |
| 87 |
b.add(semconv.GCPGCEInstanceHostnameKey, d.detector.GCEInstanceHostname) |
| 88 |
default: |
| 89 |
// We don't support this platform yet, so just return with what we have |
| 90 |
} |
| 91 |
return b.build() |
| 92 |
} |
| 93 |
|
| 94 |
// resourceBuilder simplifies constructing resources using GCP detection |
| 95 |
// library functions. |
| 96 |
type resourceBuilder struct { |
| 97 |
errs []error |
| 98 |
attrs []attribute.KeyValue |
| 99 |
} |
| 100 |
|
| 101 |
func (r *resourceBuilder) add(key attribute.Key, detect func() (string, error)) { |
| 102 |
if v, err := detect(); err == nil { |
| 103 |
r.attrs = append(r.attrs, key.String(v)) |
| 104 |
} else { |
| 105 |
r.errs = append(r.errs, err) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func (r *resourceBuilder) addInt(key attribute.Key, detect func() (string, error)) { |
| 110 |
if v, err := detect(); err == nil { |
| 111 |
if vi, err := strconv.Atoi(v); err == nil { |
| 112 |
r.attrs = append(r.attrs, key.Int(vi)) |
| 113 |
} else { |
| 114 |
r.errs = append(r.errs, err) |
| 115 |
} |
| 116 |
} else { |
| 117 |
r.errs = append(r.errs, err) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// zoneAndRegion functions are expected to return zone, region, err. |
| 122 |
func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error)) { |
| 123 |
if zone, region, err := detect(); err == nil { |
| 124 |
r.attrs = append( |
| 125 |
r.attrs, |
| 126 |
semconv.CloudAvailabilityZone(zone), |
| 127 |
semconv.CloudRegion(region), |
| 128 |
) |
| 129 |
} else { |
| 130 |
r.errs = append(r.errs, err) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationType, error)) { |
| 135 |
if v, locType, err := detect(); err == nil { |
| 136 |
switch locType { |
| 137 |
case gcp.Zone: |
| 138 |
r.attrs = append(r.attrs, semconv.CloudAvailabilityZone(v)) |
| 139 |
case gcp.Region: |
| 140 |
r.attrs = append(r.attrs, semconv.CloudRegion(v)) |
| 141 |
default: |
| 142 |
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType)) |
| 143 |
} |
| 144 |
} else { |
| 145 |
r.errs = append(r.errs, err) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func (r *resourceBuilder) build() (*resource.Resource, error) { |
| 150 |
var err error |
| 151 |
if len(r.errs) > 0 { |
| 152 |
err = fmt.Errorf("%w: %s", resource.ErrPartialResource, r.errs) |
| 153 |
} |
| 154 |
return resource.NewWithAttributes(semconv.SchemaURL, r.attrs...), err |
| 155 |
} |