Skip to content

Commit

Permalink
Merge branch 'main' into autoprop
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Jun 2, 2022
2 parents 0e495e2 + b7910af commit ded3cb7
Show file tree
Hide file tree
Showing 20 changed files with 906 additions and 81 deletions.
2 changes: 1 addition & 1 deletion detectors/aws/ec2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module go.opentelemetry.io/contrib/detectors/aws/ec2
go 1.17

require (
github.com/aws/aws-sdk-go v1.44.20
github.com/aws/aws-sdk-go v1.44.24
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/sdk v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions detectors/aws/ec2/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/aws-sdk-go v1.44.20 h1:nllTRN24EfhDSeKsNbIc6HoC8Ogd2NCJTRB8l84kDlM=
github.com/aws/aws-sdk-go v1.44.20/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.44.24 h1:3nOkwJBJLiGBmJKWp3z0utyXuBkxyGkRRwWjrTItJaY=
github.com/aws/aws-sdk-go v1.44.24/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
46 changes: 28 additions & 18 deletions detectors/aws/eks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,49 @@ require (
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/sdk v1.7.0
k8s.io/apimachinery v0.22.4
k8s.io/client-go v0.22.4
k8s.io/apimachinery v0.24.1
k8s.io/client-go v0.24.1
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.26.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.22.4 // indirect
k8s.io/klog/v2 v2.30.0 //indirect
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
k8s.io/api v0.24.1 // indirect
k8s.io/klog/v2 v2.60.1 //indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
273 changes: 232 additions & 41 deletions detectors/aws/eks/go.sum

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions detectors/gcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# GCP Resource detector

The GCP resource detector supports detecting resources on:

* Google Compute Engine (GCE)
* Google Kubernetes Engine (GKE)
* Google App Engine (GAE)
* Cloud Run
* Cloud Functions

## Usage

```golang
ctx := context.Background()
// Detect your resources
res, err := resource.New(ctx,
// Use the GCP resource detector!
resource.WithDetectors(gcp.NewDetector()),
// Keep the default detectors
resource.WithTelemetrySDK(),
// Add your own custom attributes to identify your application
resource.WithAttributes(
semconv.ServiceNameKey.String("my-application"),
semconv.ServiceNamespaceKey.String("my-company-frontend-team"),
),
)
if err != nil {
// Handle err
}
// Use the resource in your tracerprovider (or meterprovider)
tp := trace.NewTracerProvider(
// ... other options
trace.WithResource(res),
)
```

## Setting Kubernetes attributes

Previous iterations of GCP resource detection attempted to detect
`container.name`, `k8s.pod.name` and `k8s.namespace.name`. When using this detector,
you should use this in your Pod Spec to set these using
`OTEL_RESOURCE_ATTRIBUTES`:

```yaml
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NAMESPACE_NAME
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONTAINER_NAME
value: my-container-name
- name: OTEL_RESOURCE_ATTRIBUTES
value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME)
1 change: 1 addition & 0 deletions detectors/gcp/cloud-function.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
)

// NewCloudFunction will return a GCP Cloud Function resource detector.
// Deprecated: Use gcp.NewDetector() instead, which sets the same resource attributes.
func NewCloudFunction() resource.Detector {
return &cloudFunction{
cloudRun: NewCloudRun(),
Expand Down
2 changes: 2 additions & 0 deletions detectors/gcp/cloud-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type metadataClient interface {
}

// CloudRun collects resource information of Cloud Run instance.
// Deprecated: Use gcp.NewDetector() instead. Note that it sets faas.* resource attributes instead of service.* attributes.
type CloudRun struct {
mc metadataClient
onGCE func() bool
Expand All @@ -49,6 +50,7 @@ type CloudRun struct {
var _ resource.Detector = (*CloudRun)(nil)

// NewCloudRun creates a CloudRun detector.
// Deprecated: Use gcp.NewDetector() instead. Note that it sets faas.* resource attributes instead of service.* attributes.
func NewCloudRun() *CloudRun {
return &CloudRun{
mc: metadata.NewClient(nil),
Expand Down
135 changes: 135 additions & 0 deletions detectors/gcp/detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// 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 // import "go.opentelemetry.io/contrib/detectors/gcp"

import (
"context"
"fmt"

"cloud.google.com/go/compute/metadata"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
)

// NewDetector returns a resource detector which detects resource attributes on:
// * Google Compute Engine (GCE).
// * Google Kubernetes Engine (GKE).
// * Google App Engine (GAE).
// * Cloud Run.
// * Cloud Functions.
func NewDetector() resource.Detector {
return &detector{detector: gcp.NewDetector()}
}

type detector struct {
detector gcpDetector
}

// Detect detects associated resources when running on GCE, GKE, GAE,
// Cloud Run, and Cloud functions.
func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) {
if !metadata.OnGCE() {
return nil, nil
}
b := &resourceBuilder{}
b.attrs = append(b.attrs, semconv.CloudProviderGCP)
b.add(semconv.CloudAccountIDKey, d.detector.ProjectID)

switch d.detector.CloudPlatform() {
case gcp.GKE:
b.attrs = append(b.attrs, semconv.CloudPlatformGCPKubernetesEngine)
b.addZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion)
b.add(semconv.K8SClusterNameKey, d.detector.GKEClusterName)
b.add(semconv.HostIDKey, d.detector.GKEHostID)
case gcp.CloudRun:
b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudRun)
b.add(semconv.FaaSNameKey, d.detector.FaaSName)
b.add(semconv.FaaSVersionKey, d.detector.FaaSVersion)
b.add(semconv.FaaSIDKey, d.detector.FaaSID)
b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion)
case gcp.CloudFunctions:
b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudFunctions)
b.add(semconv.FaaSNameKey, d.detector.FaaSName)
b.add(semconv.FaaSVersionKey, d.detector.FaaSVersion)
b.add(semconv.FaaSIDKey, d.detector.FaaSID)
b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion)
case gcp.AppEngine:
b.attrs = append(b.attrs, semconv.CloudPlatformGCPAppEngine)
b.addZoneAndRegion(d.detector.AppEngineAvailabilityZoneAndRegion)
b.add(semconv.FaaSNameKey, d.detector.AppEngineServiceName)
b.add(semconv.FaaSVersionKey, d.detector.AppEngineServiceVersion)
b.add(semconv.FaaSIDKey, d.detector.AppEngineServiceInstance)
case gcp.GCE:
b.attrs = append(b.attrs, semconv.CloudPlatformGCPComputeEngine)
b.addZoneAndRegion(d.detector.GCEAvailabilityZoneAndRegion)
b.add(semconv.HostTypeKey, d.detector.GCEHostType)
b.add(semconv.HostIDKey, d.detector.GCEHostID)
b.add(semconv.HostNameKey, d.detector.GCEHostName)
default:
// We don't support this platform yet, so just return with what we have
}
return b.build()
}

// resourceBuilder simplifies constructing resources using GCP detection
// library functions.
type resourceBuilder struct {
errs []error
attrs []attribute.KeyValue
}

func (r *resourceBuilder) add(key attribute.Key, detect func() (string, error)) {
if v, err := detect(); err == nil {
r.attrs = append(r.attrs, key.String(v))
} else {
r.errs = append(r.errs, err)
}
}

// zoneAndRegion functions are expected to return zone, region, err.
func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error)) {
if zone, region, err := detect(); err == nil {
r.attrs = append(r.attrs, semconv.CloudAvailabilityZoneKey.String(zone))
r.attrs = append(r.attrs, semconv.CloudRegionKey.String(region))
} else {
r.errs = append(r.errs, err)
}
}

func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationType, error)) {
if v, locType, err := detect(); err == nil {
switch locType {
case gcp.Zone:
r.attrs = append(r.attrs, semconv.CloudAvailabilityZoneKey.String(v))
case gcp.Region:
r.attrs = append(r.attrs, semconv.CloudRegionKey.String(v))
default:
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType))
}
} else {
r.errs = append(r.errs, err)
}
}

func (r *resourceBuilder) build() (*resource.Resource, error) {
var err error
if len(r.errs) > 0 {
err = fmt.Errorf("%w: %s", resource.ErrPartialResource, r.errs)
}
return resource.NewWithAttributes(semconv.SchemaURL, r.attrs...), err
}
Loading

0 comments on commit ded3cb7

Please sign in to comment.