Skip to content

Commit d67914f

Browse files
tilakchowdaryzeck-ops
authored andcommitted
Add cloud account ID detection in EKS environment (open-telemetry#37179)
#### Description This enhancement detects AWS EKS cloud account ID. Account ID is obtained by querying the EC2 Metadata service for the Instance Identity Document and extracting the AccountID field from this document #### Testing Manual testing in EKS cluster with EC2 instances. Unit tests for basic functionality #### Documentation Updated metadata
1 parent 59c51da commit d67914f

File tree

10 files changed

+140
-3
lines changed

10 files changed

+140
-3
lines changed

.chloggen/eks_cloud_account_id.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: resourcedetectionprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: This enhancement detects AWS EKS cloud account ID
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37179]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

processor/resourcedetectionprocessor/internal/aws/eks/detector.go

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type detectorUtils interface {
4343
getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error)
4444
getClusterName(ctx context.Context, logger *zap.Logger) string
4545
getClusterNameTagFromReservations([]*ec2.Reservation) string
46+
getCloudAccountID(ctx context.Context, logger *zap.Logger) string
4647
}
4748

4849
type eksDetectorUtils struct {
@@ -87,6 +88,10 @@ func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
8788

8889
d.rb.SetCloudProvider(conventions.AttributeCloudProviderAWS)
8990
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAWSEKS)
91+
if d.ra.CloudAccountID.Enabled {
92+
accountID := d.utils.getCloudAccountID(ctx, d.logger)
93+
d.rb.SetCloudAccountID(accountID)
94+
}
9095

9196
if d.ra.K8sClusterName.Enabled {
9297
clusterName := d.utils.getClusterName(ctx, d.logger)
@@ -194,3 +199,21 @@ func (e eksDetectorUtils) getClusterNameTagFromReservations(reservations []*ec2.
194199

195200
return ""
196201
}
202+
203+
func (e eksDetectorUtils) getCloudAccountID(ctx context.Context, logger *zap.Logger) string {
204+
defaultErrorMessage := "Unable to get EKS cluster account ID"
205+
sess, err := session.NewSession()
206+
if err != nil {
207+
logger.Warn(defaultErrorMessage, zap.Error(err))
208+
return ""
209+
}
210+
211+
ec2Svc := ec2metadata.New(sess)
212+
instanceIdentityDocument, err := ec2Svc.GetInstanceIdentityDocumentWithContext(ctx)
213+
if err != nil {
214+
logger.Warn(defaultErrorMessage, zap.Error(err))
215+
return ""
216+
}
217+
218+
return instanceIdentityDocument.AccountID
219+
}

processor/resourcedetectionprocessor/internal/aws/eks/detector_test.go

+60-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
)
2020

2121
const (
22-
clusterName = "my-cluster"
22+
clusterName = "my-cluster"
23+
cloudAccountID = "cloud1234"
2324
)
2425

2526
type MockDetectorUtils struct {
@@ -40,6 +41,10 @@ func (detectorUtils *MockDetectorUtils) getClusterNameTagFromReservations(_ []*e
4041
return clusterName
4142
}
4243

44+
func (detectorUtils *MockDetectorUtils) getCloudAccountID(_ context.Context, _ *zap.Logger) string {
45+
return cloudAccountID
46+
}
47+
4348
func TestNewDetector(t *testing.T) {
4449
dcfg := CreateDefaultConfig()
4550
detector, err := NewDetector(processortest.NewNopSettings(), dcfg)
@@ -72,3 +77,57 @@ func TestNotEKS(t *testing.T) {
7277
require.NoError(t, err)
7378
assert.Equal(t, 0, r.Attributes().Len(), "Resource object should be empty")
7479
}
80+
81+
func TestEKSResourceDetection_ForCloudAccountID(t *testing.T) {
82+
tests := []struct {
83+
name string
84+
ra metadata.ResourceAttributesConfig
85+
expectedOutput map[string]any
86+
shouldError bool
87+
}{
88+
{
89+
name: "Detects CloudAccountID when enabled",
90+
ra: metadata.ResourceAttributesConfig{
91+
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: true},
92+
},
93+
expectedOutput: map[string]any{
94+
"cloud.account.id": "cloud1234",
95+
},
96+
shouldError: false,
97+
},
98+
{
99+
name: "Does not detect CloudAccountID when disabled",
100+
ra: metadata.ResourceAttributesConfig{
101+
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: false},
102+
},
103+
expectedOutput: map[string]any{},
104+
shouldError: false,
105+
},
106+
}
107+
108+
for _, tt := range tests {
109+
t.Run(tt.name, func(t *testing.T) {
110+
detectorUtils := new(MockDetectorUtils)
111+
ctx := context.Background()
112+
113+
t.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
114+
detectorUtils.On("getConfigMap", authConfigmapNS, authConfigmapName).Return(map[string]string{conventions.AttributeK8SClusterName: clusterName}, nil)
115+
116+
eksResourceDetector := &detector{
117+
utils: detectorUtils,
118+
err: nil,
119+
ra: tt.ra,
120+
rb: metadata.NewResourceBuilder(tt.ra),
121+
}
122+
res, _, err := eksResourceDetector.Detect(ctx)
123+
124+
if tt.shouldError {
125+
assert.Error(t, err)
126+
return
127+
}
128+
129+
assert.NoError(t, err)
130+
assert.Equal(t, tt.expectedOutput, res.Attributes().AsRaw())
131+
})
132+
}
133+
}

processor/resourcedetectionprocessor/internal/aws/eks/documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
| Name | Description | Values | Enabled |
1010
| ---- | ----------- | ------ | ------- |
11+
| cloud.account.id | The cloud account id | Any Str | false |
1112
| cloud.platform | The cloud.platform | Any Str | true |
1213
| cloud.provider | The cloud.provider | Any Str | true |
1314
| k8s.cluster.name | The EKS cluster name. This attribute is currently only available when running on EC2 instances, and requires permission to run the EC2:DescribeInstances action. | Any Str | false |

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_config.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_config_test.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_resource.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_resource_test.go

+8-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/testdata/config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
default:
22
all_set:
33
resource_attributes:
4+
cloud.account.id:
5+
enabled: true
46
cloud.platform:
57
enabled: true
68
cloud.provider:
@@ -9,6 +11,8 @@ all_set:
911
enabled: true
1012
none_set:
1113
resource_attributes:
14+
cloud.account.id:
15+
enabled: false
1216
cloud.platform:
1317
enabled: false
1418
cloud.provider:

processor/resourcedetectionprocessor/internal/aws/eks/metadata.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ resource_attributes:
1111
description: The cloud.platform
1212
type: string
1313
enabled: true
14+
cloud.account.id:
15+
description: The cloud account id
16+
type: string
17+
enabled: false
1418
k8s.cluster.name:
1519
description: The EKS cluster name. This attribute is currently only available when running on EC2 instances, and requires permission to run the EC2:DescribeInstances action.
1620
type: string

0 commit comments

Comments
 (0)