Skip to content

feat(detectors): add EC2 resource detection via IMDSv2 - #23

Merged
karthikeyangs9 merged 2 commits into
mainfrom
feat/ec2-resource-detection
Mar 23, 2026
Merged

feat(detectors): add EC2 resource detection via IMDSv2#23
karthikeyangs9 merged 2 commits into
mainfrom
feat/ec2-resource-detection

Conversation

@karthikeyangs9

Copy link
Copy Markdown
Contributor

Summary

  • Add native EC2 instance metadata and tag detection to the go-agent using IMDSv2
  • New LAST9_CLOUD_RESOURCE_DETECTION=aws env var to opt-in (zero cost when unset)
  • Custom resource.Detector in detectors/aws/ — no AWS SDK dependency

Motivation

The go-agent's createResource() has no cloud resource detection. Users running on EC2 miss valuable attributes (cloud.region, host.id, instance tags) that are critical for filtering and grouping telemetry in Last9.

Previously, the only workaround was a shell script enriching OTEL_RESOURCE_ATTRIBUTES before the binary starts. This PR makes it native to the agent.

What it does

When LAST9_CLOUD_RESOURCE_DETECTION=aws is set, the agent's createResource() adds an EC2 detector that:

  1. Fetches IMDSv2 token (PUT /latest/api/token with TTL header)
  2. Reads instance metadata: instance-id, availability-zone, instance-type, hostname
  3. Derives region from AZ (e.g., us-east-1aus-east-1)
  4. Fetches instance tags from /latest/meta-data/tags/instance/ (requires "Allow tags in instance metadata" enabled on the EC2 instance)

Resource attributes produced

Attribute Source
cloud.provider=aws Always (when detector runs)
cloud.region Derived from AZ
cloud.availability_zone IMDS
host.id Instance ID
host.type Instance type
host.name Hostname
ec2.tag.<Key> IMDS tags endpoint

Graceful degradation

  • Not on EC2? IMDS is unreachable → detector returns empty resource, zero attributes, no error
  • Tags not enabled? /latest/meta-data/tags/instance returns 404 → tags skipped, metadata still collected
  • Partial IMDS failure? Each metadata field is fetched independently; missing fields are simply omitted
  • 2-second timeout on all IMDS calls — never blocks startup

Files changed

File Change
config/config.go Added CloudResourceDetection field, reads LAST9_CLOUD_RESOURCE_DETECTION
agent.go Imports detector, adds resource.WithDetectors() when config is "aws"
detectors/aws/ec2.go New — EC2 resource detector (IMDSv2 metadata + tags)
detectors/aws/ec2_test.go New — 5 unit tests with mock IMDS server

Usage

export LAST9_CLOUD_RESOURCE_DETECTION=aws

No code changes required — agent.Start() picks it up automatically.

Test plan

  • TestDetect_FullMetadataAndTags — all metadata + 3 tags returned correctly
  • TestDetect_NoTags — tags endpoint missing, core metadata still works
  • TestDetect_IMDSUnreachable — non-EC2 env returns empty resource, no error
  • TestDetect_PartialMetadata — only instance-id available, no region/AZ
  • TestDetect_TokenValidation — IMDSv2 token sent on all metadata requests
  • All existing tests pass (go test ./... -race)
  • go vet ./... clean

Add native EC2 instance metadata and tag detection to the go-agent,
enabled via LAST9_CLOUD_RESOURCE_DETECTION=aws. Uses IMDSv2 with
graceful fallback — zero overhead on non-EC2 environments.
- Reorder EC2Detector struct fields to fix fieldalignment (govet)
- Run gofmt on test file to fix formatting
@muktaa
muktaa self-requested a review March 21, 2026 09:29
@karthikeyangs9
karthikeyangs9 merged commit 48a497e into main Mar 23, 2026
10 checks passed
karthikeyangs9 added a commit that referenced this pull request Mar 23, 2026
prathamesh-sonpatki added a commit that referenced this pull request Mar 23, 2026
Revert: feat(detectors): add EC2 resource detection via IMDSv2 (#23)
@prathamesh-sonpatki
prathamesh-sonpatki deleted the feat/ec2-resource-detection branch April 7, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants