|
| 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 |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "cloud.google.com/go/compute/metadata" |
| 24 | + |
| 25 | + "go.opentelemetry.io/otel/api/kv" |
| 26 | + "go.opentelemetry.io/otel/api/standard" |
| 27 | + "go.opentelemetry.io/otel/sdk/resource" |
| 28 | +) |
| 29 | + |
| 30 | +// GCE collects resource information of GCE computing instances |
| 31 | +type GCE struct{} |
| 32 | + |
| 33 | +// compile time assertion that GCE implements the resource.Detector interface. |
| 34 | +var _ resource.Detector = (*GCE)(nil) |
| 35 | + |
| 36 | +// Detect detects associated resources when running on GCE hosts. |
| 37 | +func (gce *GCE) Detect(ctx context.Context) (*resource.Resource, error) { |
| 38 | + if !metadata.OnGCE() { |
| 39 | + return nil, nil |
| 40 | + } |
| 41 | + |
| 42 | + labels := []kv.KeyValue{ |
| 43 | + standard.CloudProviderGCP, |
| 44 | + } |
| 45 | + |
| 46 | + var errInfo []string |
| 47 | + |
| 48 | + if projectID, err := metadata.ProjectID(); hasProblem(err) { |
| 49 | + errInfo = append(errInfo, err.Error()) |
| 50 | + } else if projectID != "" { |
| 51 | + labels = append(labels, standard.CloudAccountIDKey.String(projectID)) |
| 52 | + } |
| 53 | + |
| 54 | + if zone, err := metadata.Zone(); hasProblem(err) { |
| 55 | + errInfo = append(errInfo, err.Error()) |
| 56 | + } else if zone != "" { |
| 57 | + labels = append(labels, standard.CloudZoneKey.String(zone)) |
| 58 | + |
| 59 | + splitArr := strings.SplitN(zone, "-", 3) |
| 60 | + if len(splitArr) == 3 { |
| 61 | + standard.CloudRegionKey.String(strings.Join(splitArr[0:2], "-")) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if instanceID, err := metadata.InstanceID(); hasProblem(err) { |
| 66 | + errInfo = append(errInfo, err.Error()) |
| 67 | + } else if instanceID != "" { |
| 68 | + labels = append(labels, standard.HostIDKey.String(instanceID)) |
| 69 | + } |
| 70 | + |
| 71 | + if name, err := metadata.InstanceName(); hasProblem(err) { |
| 72 | + errInfo = append(errInfo, err.Error()) |
| 73 | + } else if name != "" { |
| 74 | + labels = append(labels, standard.HostNameKey.String(name)) |
| 75 | + } |
| 76 | + |
| 77 | + if hostname, err := os.Hostname(); hasProblem(err) { |
| 78 | + errInfo = append(errInfo, err.Error()) |
| 79 | + } else if hostname != "" { |
| 80 | + labels = append(labels, standard.HostHostNameKey.String(hostname)) |
| 81 | + } |
| 82 | + |
| 83 | + if hostType, err := metadata.Get("instance/machine-type"); hasProblem(err) { |
| 84 | + errInfo = append(errInfo, err.Error()) |
| 85 | + } else if hostType != "" { |
| 86 | + labels = append(labels, standard.HostTypeKey.String(hostType)) |
| 87 | + } |
| 88 | + |
| 89 | + var aggregatedErr error |
| 90 | + if len(errInfo) > 0 { |
| 91 | + aggregatedErr = fmt.Errorf("detecting GCE resources: %s", errInfo) |
| 92 | + } |
| 93 | + |
| 94 | + return resource.New(labels...), aggregatedErr |
| 95 | +} |
| 96 | + |
| 97 | +// hasProblem checks if the err is not nil or for missing resources |
| 98 | +func hasProblem(err error) bool { |
| 99 | + if err == nil { |
| 100 | + return false |
| 101 | + } |
| 102 | + if _, undefined := err.(metadata.NotDefinedError); undefined { |
| 103 | + return false |
| 104 | + } |
| 105 | + return true |
| 106 | +} |
0 commit comments