Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCE Resource Detector #132

Merged
merged 16 commits into from
Jul 24, 2020
90 changes: 90 additions & 0 deletions detect/gcp/gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gcp
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"log"
"strings"

"cloud.google.com/go/compute/metadata"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/standard"
"go.opentelemetry.io/otel/sdk/resource"
)

// GCP is a detector that implements Detect() function
type GCP struct{}

// Detect detects associated resources when running on GCE hosts.
func (gcp *GCP) Detect(ctx context.Context) (*resource.Resource, error) {
if !metadata.OnGCE() {
return nil, nil
}

labels := []kv.KeyValue{
standard.CloudProviderGCP,
standard.CloudRegionKey.String(""),
}

projectID, err := metadata.ProjectID()
logError(err)
if projectID != "" {
labels = append(labels, standard.CloudAccountIDKey.String(projectID))
}

zone, err := metadata.Zone()
logError(err)
if zone != "" {
labels = append(labels, standard.CloudZoneKey.String(zone))
}

instanceID, err := metadata.InstanceID()
logError(err)
if instanceID != "" {
labels = append(labels, standard.HostIDKey.String(instanceID))
}

name, err := metadata.InstanceName()
logError(err)
if instanceID != "" {
YANYZP marked this conversation as resolved.
Show resolved Hide resolved
labels = append(labels, standard.HostNameKey.String(name))
}

hostname, err := metadata.Hostname()
YANYZP marked this conversation as resolved.
Show resolved Hide resolved
logError(err)
if instanceID != "" {
YANYZP marked this conversation as resolved.
Show resolved Hide resolved
labels = append(labels, standard.HostHostNameKey.String(hostname))
}

hostType, err := metadata.Get("instance/machine-type")
logError(err)
if instanceID != "" {
YANYZP marked this conversation as resolved.
Show resolved Hide resolved
labels = append(labels, standard.HostTypeKey.String(hostType))
}

return resource.New(labels...), nil
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

}

//logError logs error only if the error is present and it is not 'not defined'
func logError(err error) {
if err != nil {
if !strings.Contains(err.Error(), "not defined") {
log.Printf("Error retrieving gcp metadata: %v", err)
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module go.opentelemetry.io/contrib
go 1.14

require (
cloud.google.com/go v0.26.0
go.opentelemetry.io/otel v0.8.0
google.golang.org/grpc v1.30.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7 h1:qELHH0AWCvf98Yf+CNIJx9vOZOfHFDDzgDRYsnNk/vs=
Expand Down Expand Up @@ -61,6 +62,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down