| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package ec2 provides a resource detector for EC2 instances using aws-sdk-go-v2. |
| 5 |
package ec2 // import "go.opentelemetry.io/contrib/detectors/aws/ec2/v2" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"net/http" |
| 13 |
|
| 14 |
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" |
| 15 |
awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 16 |
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds" |
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/sdk/resource" |
| 19 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 20 |
) |
| 21 |
|
| 22 |
var errClient = errors.New("EC2 Client Error") |
| 23 |
|
| 24 |
// client implements methods to capture EC2 environment metadata information. |
| 25 |
type client interface { |
| 26 |
GetInstanceIdentityDocument(ctx context.Context, params *imds.GetInstanceIdentityDocumentInput, optFns ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error) |
| 27 |
GetMetadata(ctx context.Context, params *imds.GetMetadataInput, optFns ...func(*imds.Options)) (*imds.GetMetadataOutput, error) |
| 28 |
} |
| 29 |
|
| 30 |
// resource detector collects resource information from EC2 environment. |
| 31 |
type resourceDetector struct { |
| 32 |
c client |
| 33 |
} |
| 34 |
|
| 35 |
// compile time assertion that imds.Client implements client. |
| 36 |
var _ client = (*imds.Client)(nil) |
| 37 |
|
| 38 |
// compile time assertion that resourceDetector implements the resource.Detector interface. |
| 39 |
var _ resource.Detector = (*resourceDetector)(nil) |
| 40 |
|
| 41 |
// NewResourceDetector returns a resource detector that will detect AWS EC2 resources. |
| 42 |
func NewResourceDetector() resource.Detector { |
| 43 |
return &resourceDetector{c: newClient()} |
| 44 |
} |
| 45 |
|
| 46 |
func (detector *resourceDetector) getClient() client { |
| 47 |
return detector.c |
| 48 |
} |
| 49 |
|
| 50 |
// Detect detects associated resources when running in AWS environment. |
| 51 |
func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { |
| 52 |
// Return nil if not able to establish valid client |
| 53 |
client := detector.getClient() |
| 54 |
if client == nil { |
| 55 |
return nil, errClient |
| 56 |
} |
| 57 |
|
| 58 |
// Available method removed in aws-sdk-go-v2, return empty resource if client returns error |
| 59 |
doc, err := client.GetInstanceIdentityDocument(ctx, nil) |
| 60 |
if err != nil { |
| 61 |
return resource.Empty(), nil |
| 62 |
} |
| 63 |
|
| 64 |
attributes := []attribute.KeyValue{ |
| 65 |
semconv.CloudProviderAWS, |
| 66 |
semconv.CloudPlatformAWSEC2, |
| 67 |
semconv.CloudRegion(doc.Region), |
| 68 |
semconv.CloudAvailabilityZone(doc.AvailabilityZone), |
| 69 |
semconv.CloudAccountID(doc.AccountID), |
| 70 |
semconv.HostID(doc.InstanceID), |
| 71 |
semconv.HostImageID(doc.ImageID), |
| 72 |
semconv.HostType(doc.InstanceType), |
| 73 |
} |
| 74 |
|
| 75 |
m := &metadata{client: client} |
| 76 |
m.add(ctx, semconv.HostNameKey, "hostname") |
| 77 |
|
| 78 |
attributes = append(attributes, m.attributes...) |
| 79 |
|
| 80 |
if len(m.errs) > 0 { |
| 81 |
err = fmt.Errorf("%w: %s", resource.ErrPartialResource, m.errs) |
| 82 |
} |
| 83 |
|
| 84 |
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), err |
| 85 |
} |
| 86 |
|
| 87 |
func newClient() client { |
| 88 |
cfg, err := awsconfig.LoadDefaultConfig(context.Background()) |
| 89 |
if err != nil { |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
return imds.NewFromConfig(cfg) |
| 94 |
} |
| 95 |
|
| 96 |
type metadata struct { |
| 97 |
client client |
| 98 |
errs []error |
| 99 |
attributes []attribute.KeyValue |
| 100 |
} |
| 101 |
|
| 102 |
func (m *metadata) add(ctx context.Context, k attribute.Key, n string) { |
| 103 |
metadataInput := &imds.GetMetadataInput{Path: n} |
| 104 |
md, err := m.client.GetMetadata(ctx, metadataInput) |
| 105 |
if err != nil { |
| 106 |
m.recordError(n, err) |
| 107 |
return |
| 108 |
} |
| 109 |
data, err := io.ReadAll(md.Content) |
| 110 |
if err != nil { |
| 111 |
m.recordError(n, err) |
| 112 |
return |
| 113 |
} |
| 114 |
m.attributes = append(m.attributes, k.String(string(data))) |
| 115 |
} |
| 116 |
|
| 117 |
func (m *metadata) recordError(path string, err error) { |
| 118 |
var rf *awshttp.ResponseError |
| 119 |
ok := errors.As(err, &rf) |
| 120 |
if !ok { |
| 121 |
m.errs = append(m.errs, fmt.Errorf("%q: %w", path, err)) |
| 122 |
return |
| 123 |
} |
| 124 |
|
| 125 |
if rf.HTTPStatusCode() == http.StatusNotFound { |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
m.errs = append(m.errs, fmt.Errorf("%q: %d %s", path, rf.HTTPStatusCode(), rf.Error())) |
| 130 |
} |