Skip to content

Commit 5c2cfc3

Browse files
author
YANYZP
authoredJul 24, 2020
GCE Resource Detector (#132)
Implements detection of GCP resource tags.
1 parent 2baa898 commit 5c2cfc3

File tree

5 files changed

+494
-0
lines changed

5 files changed

+494
-0
lines changed
 

‎.github/dependabot.yml

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ updates:
99
directory: "/" # Location of package manifests
1010
schedule:
1111
interval: "daily"
12+
- package-ecosystem: "gomod" # See documentation for possible values
13+
directory: "/detectors/gcp" # Location of package manifests
14+
schedule:
15+
interval: "daily"
1216
- package-ecosystem: "gomod" # See documentation for possible values
1317
directory: "/tools" # Location of package manifests
1418
schedule:

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1010

1111
### Added
1212

13+
- Create a detector that collects resources from GCE machines. (#132)
1314
- Add instrumentation for Kafka (github.com/Shopify/sarama). (#134)
1415
- Add links and status message for mock span. (#134)
1516

17+
1618
## [0.9.0] - 2020-07-20
1719

1820
This release upgrades its [go.opentelemetry.io/otel](https://github.com/open-telemetry/opentelemetry-go/releases/tag/v0.9.0) dependency to v0.9.0.
@@ -23,6 +25,7 @@ This release upgrades its [go.opentelemetry.io/otel](https://github.com/open-tel
2325
- Update dependabot configuration to correctly check all included packages. (#131)
2426
- Update `RELEASING.md` with correct `tag.sh` command. (#130)
2527

28+
2629
## [0.8.0] - 2020-07-10
2730

2831
This release upgrades its [go.opentelemetry.io/otel](https://github.com/open-telemetry/opentelemetry-go/releases/tag/v0.8.0) dependency to v0.8.0, includes minor fixes, and new instrumentation.

‎detectors/gcp/gce.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

‎detectors/gcp/go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module go.opentelemetry.io/contrib/detectors/gcp
2+
3+
go 1.14
4+
5+
require (
6+
cloud.google.com/go v0.61.0
7+
go.opentelemetry.io/otel v0.9.0
8+
)

‎detectors/gcp/go.sum

+373
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.