@@ -285,7 +285,7 @@ func TestStartSuccessWithNoTagsVolumesUpdate(t *testing.T) {
285285 cfg := createDefaultConfig ().(* Config )
286286 cfg .RefreshTagsInterval = 0 * time .Second
287287 cfg .RefreshVolumesInterval = 0 * time .Second
288- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
288+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
289289 cfg .EC2InstanceTagKeys = []string {tagKey1 , tagKey2 , "AutoScalingGroupName" }
290290 cfg .EBSDeviceKeys = []string {device1 , device2 }
291291 _ , cancel := context .WithCancel (context .Background ())
@@ -330,7 +330,7 @@ func TestStartSuccessWithTagsVolumesUpdate(t *testing.T) {
330330 //use millisecond rather than second to speed up test execution
331331 cfg .RefreshTagsInterval = 20 * time .Millisecond
332332 cfg .RefreshVolumesInterval = 20 * time .Millisecond
333- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
333+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
334334 cfg .EC2InstanceTagKeys = []string {tagKey1 , tagKey2 , "AutoScalingGroupName" }
335335 cfg .EBSDeviceKeys = []string {device1 , device2 }
336336 _ , cancel := context .WithCancel (context .Background ())
@@ -387,7 +387,7 @@ func TestStartSuccessWithWildcardTagVolumeKey(t *testing.T) {
387387 cfg := createDefaultConfig ().(* Config )
388388 cfg .RefreshTagsInterval = 0 * time .Second
389389 cfg .RefreshVolumesInterval = 0 * time .Second
390- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
390+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
391391 cfg .EC2InstanceTagKeys = []string {"*" }
392392 cfg .EBSDeviceKeys = []string {"*" }
393393 _ , cancel := context .WithCancel (context .Background ())
@@ -432,7 +432,7 @@ func TestApplyWithTagsVolumesUpdate(t *testing.T) {
432432 //use millisecond rather than second to speed up test execution
433433 cfg .RefreshTagsInterval = 20 * time .Millisecond
434434 cfg .RefreshVolumesInterval = 20 * time .Millisecond
435- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
435+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
436436 cfg .EC2InstanceTagKeys = []string {tagKey1 , tagKey2 , "AutoScalingGroupName" }
437437 cfg .EBSDeviceKeys = []string {device1 , device2 }
438438 cfg .DiskDeviceTagKey = "device"
@@ -499,7 +499,16 @@ func TestApplyWithTagsVolumesUpdate(t *testing.T) {
499499 //assume one second is long enough for the api to be called many times
500500 //so that all tags/volumes are updated
501501 time .Sleep (time .Second )
502- updatedOutput , err := tagger .processMetrics (context .Background (), md )
502+ // Create fresh metrics for the second processing to test updated cache values
503+ freshMd := createTestMetrics ([]map [string ]string {
504+ {
505+ "host" : "example.org" ,
506+ },
507+ {
508+ "device" : device2 ,
509+ },
510+ })
511+ updatedOutput , err := tagger .processMetrics (context .Background (), freshMd )
503512 assert .Nil (t , err )
504513 expectedUpdatedOutput := createTestMetrics ([]map [string ]string {
505514 map [string ]string {
@@ -527,7 +536,7 @@ func TestMetricsDroppedBeforeStarted(t *testing.T) {
527536 cfg := createDefaultConfig ().(* Config )
528537 cfg .RefreshTagsInterval = 0 * time .Millisecond
529538 cfg .RefreshVolumesInterval = 0 * time .Millisecond
530- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
539+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
531540 cfg .EC2InstanceTagKeys = []string {"*" }
532541 cfg .EBSDeviceKeys = []string {"*" }
533542 _ , cancel := context .WithCancel (context .Background ())
@@ -593,7 +602,7 @@ func TestTaggerStartDoesNotBlock(t *testing.T) {
593602 cfg := createDefaultConfig ().(* Config )
594603 cfg .RefreshTagsInterval = 0 * time .Second
595604 cfg .RefreshVolumesInterval = 0 * time .Second
596- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
605+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
597606 cfg .EC2InstanceTagKeys = []string {"*" }
598607 cfg .EBSDeviceKeys = []string {"*" }
599608 _ , cancel := context .WithCancel (context .Background ())
@@ -632,12 +641,77 @@ func TestTaggerStartDoesNotBlock(t *testing.T) {
632641 close (inited )
633642}
634643
644+ // Test that existing attributes are not overwritten by ec2tagger
645+ func TestExistingAttributesNotOverwritten (t * testing.T ) {
646+ cfg := createDefaultConfig ().(* Config )
647+ cfg .RefreshTagsInterval = 0 * time .Second
648+ cfg .RefreshVolumesInterval = 0 * time .Second
649+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
650+ cfg .EC2InstanceTagKeys = []string {tagKey1 , tagKey2 }
651+ cfg .EBSDeviceKeys = []string {device1 }
652+ cfg .DiskDeviceTagKey = "device"
653+ _ , cancel := context .WithCancel (context .Background ())
654+ ec2Client := & mockEC2Client {
655+ tagsCallCount : 0 ,
656+ tagsFailLimit : 0 ,
657+ tagsPartialLimit : 1 ,
658+ UseUpdatedTags : false ,
659+ }
660+ ec2Provider := func (* configaws.CredentialConfig ) ec2iface.EC2API {
661+ return ec2Client
662+ }
663+ volumeCache := & mockVolumeCache {cache : make (map [string ]string )}
664+ BackoffSleepArray = []time.Duration {10 * time .Millisecond , 20 * time .Millisecond , 30 * time .Millisecond }
665+ defaultRefreshInterval = 50 * time .Millisecond
666+ tagger := & Tagger {
667+ Config : cfg ,
668+ logger : processortest .NewNopSettings (component .MustNewType ("ec2tagger" )).Logger ,
669+ cancelFunc : cancel ,
670+ metadataProvider : & mockMetadataProvider {InstanceIdentityDocument : mockedInstanceIdentityDoc },
671+ ec2Provider : ec2Provider ,
672+ volumeSerialCache : volumeCache ,
673+ }
674+ err := tagger .Start (context .Background (), componenttest .NewNopHost ())
675+ assert .Nil (t , err )
676+
677+ // Wait for tags and volumes to be retrieved
678+ time .Sleep (time .Second )
679+
680+ // Create metrics with existing attributes that should not be overwritten
681+ md := createTestMetrics ([]map [string ]string {
682+ {
683+ "InstanceId" : "i-100000" , // This should NOT be overwritten
684+ "InstanceType" : "t2.micro" , // This should NOT be overwritten
685+ tagKey1 : "existing-value" , // This should NOT be overwritten
686+ "device" : device1 ,
687+ },
688+ })
689+
690+ output , err := tagger .processMetrics (context .Background (), md )
691+ assert .Nil (t , err )
692+
693+ // Expected output should preserve existing values and only add missing ones
694+ expectedOutput := createTestMetrics ([]map [string ]string {
695+ {
696+ "InstanceId" : "i-100000" , // Original value preserved
697+ "InstanceType" : "t2.micro" , // Original value preserved
698+ "ImageId" : "ami-09edd32d9b0990d49" , // Added from metadata (not existing)
699+ tagKey1 : "existing-value" , // Original value preserved
700+ tagKey2 : tagVal2 , // Added from EC2 tags (not existing)
701+ "VolumeId" : volumeId1 , // Added from volume cache (not existing)
702+ "device" : device1 ,
703+ },
704+ })
705+
706+ checkAttributes (t , expectedOutput , output )
707+ }
708+
635709// Test ec2tagger Start does not block for a long time
636710func TestTaggerStartsWithoutTagOrVolume (t * testing.T ) {
637711 cfg := createDefaultConfig ().(* Config )
638712 cfg .RefreshTagsInterval = 0 * time .Second
639713 cfg .RefreshVolumesInterval = 0 * time .Second
640- cfg .EC2MetadataTags = []string {mdKeyInstanceId , mdKeyImageId , mdKeyInstanceType }
714+ cfg .EC2MetadataTags = []string {MdKeyInstanceID , MdKeyImageID , MdKeyInstanceType }
641715 _ , cancel := context .WithCancel (context .Background ())
642716
643717 tagger := & Tagger {
0 commit comments