|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gcp // import "go.opentelemetry.io/contrib/detectors/gcp" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "cloud.google.com/go/compute/metadata" |
| 22 | + "github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp" |
| 23 | + "go.opentelemetry.io/otel/attribute" |
| 24 | + "go.opentelemetry.io/otel/sdk/resource" |
| 25 | + semconv "go.opentelemetry.io/otel/semconv/v1.10.0" |
| 26 | +) |
| 27 | + |
| 28 | +// NewDetector returns a resource detector which detects resource attributes on: |
| 29 | +// * Google Compute Engine (GCE) |
| 30 | +// * Google Kubernetes Engine (GKE) |
| 31 | +// * Google App Engine (GAE) |
| 32 | +// * Cloud Run |
| 33 | +// * Cloud Functions |
| 34 | +func NewDetector() resource.Detector { |
| 35 | + return &detector{detector: gcp.NewDetector()} |
| 36 | +} |
| 37 | + |
| 38 | +type detector struct { |
| 39 | + detector gcpDetector |
| 40 | +} |
| 41 | + |
| 42 | +// Detect detects associated resources when running on GCE, GKE, GAE, |
| 43 | +// Cloud Run, and Cloud functions. |
| 44 | +func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) { |
| 45 | + if !metadata.OnGCE() { |
| 46 | + return nil, nil |
| 47 | + } |
| 48 | + projectID, err := d.detector.ProjectID() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + attributes := []attribute.KeyValue{semconv.CloudProviderGCP, semconv.CloudAccountIDKey.String(projectID)} |
| 53 | + |
| 54 | + switch d.detector.CloudPlatform() { |
| 55 | + case gcp.GKE: |
| 56 | + attributes = append(attributes, semconv.CloudPlatformGCPKubernetesEngine) |
| 57 | + v, locType, err := d.detector.GKEAvailabilityZoneOrRegion() |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + switch locType { |
| 62 | + case gcp.Zone: |
| 63 | + attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(v)) |
| 64 | + case gcp.Region: |
| 65 | + attributes = append(attributes, semconv.CloudRegionKey.String(v)) |
| 66 | + default: |
| 67 | + return nil, fmt.Errorf("location must be zone or region. Got %v", locType) |
| 68 | + } |
| 69 | + return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{ |
| 70 | + semconv.K8SClusterNameKey: d.detector.GKEClusterName, |
| 71 | + semconv.HostIDKey: d.detector.GKEHostID, |
| 72 | + semconv.HostNameKey: d.detector.GKEHostName, |
| 73 | + }) |
| 74 | + case gcp.CloudRun: |
| 75 | + attributes = append(attributes, semconv.CloudPlatformGCPCloudRun) |
| 76 | + return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{ |
| 77 | + semconv.FaaSNameKey: d.detector.FaaSName, |
| 78 | + semconv.FaaSVersionKey: d.detector.FaaSVersion, |
| 79 | + semconv.FaaSIDKey: d.detector.FaaSID, |
| 80 | + semconv.CloudRegionKey: d.detector.FaaSCloudRegion, |
| 81 | + }) |
| 82 | + case gcp.CloudFunctions: |
| 83 | + attributes = append(attributes, semconv.CloudPlatformGCPCloudFunctions) |
| 84 | + return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{ |
| 85 | + semconv.FaaSNameKey: d.detector.FaaSName, |
| 86 | + semconv.FaaSVersionKey: d.detector.FaaSVersion, |
| 87 | + semconv.FaaSIDKey: d.detector.FaaSID, |
| 88 | + semconv.CloudRegionKey: d.detector.FaaSCloudRegion, |
| 89 | + }) |
| 90 | + case gcp.AppEngine: |
| 91 | + attributes = append(attributes, semconv.CloudPlatformGCPAppEngine) |
| 92 | + zone, region, err := d.detector.AppEngineAvailabilityZoneAndRegion() |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone)) |
| 97 | + attributes = append(attributes, semconv.CloudRegionKey.String(region)) |
| 98 | + return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{ |
| 99 | + semconv.FaaSNameKey: d.detector.AppEngineServiceName, |
| 100 | + semconv.FaaSVersionKey: d.detector.AppEngineServiceVersion, |
| 101 | + semconv.FaaSIDKey: d.detector.AppEngineServiceInstance, |
| 102 | + }) |
| 103 | + case gcp.GCE: |
| 104 | + attributes = append(attributes, semconv.CloudPlatformGCPComputeEngine) |
| 105 | + zone, region, err := d.detector.GCEAvailabilityZoneAndRegion() |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone)) |
| 110 | + attributes = append(attributes, semconv.CloudRegionKey.String(region)) |
| 111 | + return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{ |
| 112 | + semconv.HostTypeKey: d.detector.GCEHostType, |
| 113 | + semconv.HostIDKey: d.detector.GCEHostID, |
| 114 | + semconv.HostNameKey: d.detector.GCEHostName, |
| 115 | + }) |
| 116 | + default: |
| 117 | + // We don't support this platform yet, so just return with what we have |
| 118 | + return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +type detectionFunc func() (string, error) |
| 123 | + |
| 124 | +// detectWithFuncs is a helper to reduce the amount of error handling code |
| 125 | +func detectWithFuncs(attributes []attribute.KeyValue, funcs map[attribute.Key]detectionFunc) (*resource.Resource, error) { |
| 126 | + for key, detect := range funcs { |
| 127 | + v, err := detect() |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + attributes = append(attributes, key.String(v)) |
| 132 | + } |
| 133 | + return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 134 | +} |
0 commit comments