77import com .azure .monitor .opentelemetry .autoconfigure .implementation .models .ContextTagKeys ;
88import io .opentelemetry .api .common .Attributes ;
99import io .opentelemetry .api .common .AttributesBuilder ;
10- import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
11- import io .opentelemetry .sdk .autoconfigure .spi .internal .DefaultConfigProperties ;
1210import io .opentelemetry .sdk .resources .Resource ;
1311import io .opentelemetry .semconv .ServiceAttributes ;
1412import io .opentelemetry .semconv .incubating .ServiceIncubatingAttributes ;
1513import org .junit .jupiter .api .BeforeEach ;
1614import org .junit .jupiter .api .Test ;
1715import reactor .util .annotation .Nullable ;
1816
19- import java .util .Collections ;
2017import java .util .HashMap ;
2118import java .util .Map ;
2219
2522
2623class ResourceParserTest {
2724
25+ private static final String DEFAULT_ROLE_NAME = "unknown_service:java" ;
2826 private static final String DEFAULT_ROLE_INSTANCE = HostName .get ();
2927 private MetricTelemetryBuilder builder ;
3028
@@ -35,54 +33,80 @@ void setup() {
3533
3634 @ Test
3735 void testDefaultResource () {
38- new ResourceParser ().updateRoleNameAndInstance (builder , Resource .create (Attributes .empty ()));
39- assertThat (builder .build ().getTags ())
40- .contains (entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), DEFAULT_ROLE_INSTANCE ));
36+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , Resource .create (Attributes .empty ()));
37+
38+ Map <String , String > tags = builder .build ().getTags ();
39+ assertThat (tags ).contains (entry (ContextTagKeys .AI_CLOUD_ROLE .toString (), DEFAULT_ROLE_NAME ),
40+ entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), DEFAULT_ROLE_INSTANCE )).hasSize (2 );
4141 }
4242
4343 @ Test
4444 void testServiceNameFromResource () {
4545 Resource resource = createTestResource ("fake-service-name" , null , null );
46- new ResourceParser ().updateRoleNameAndInstance (builder , resource );
46+
47+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
48+
4749 Map <String , String > tags = builder .build ().getTags ();
48- assertThat (tags . get ( ContextTagKeys .AI_CLOUD_ROLE .toString ())). isEqualTo ( "fake-service-name" );
49- assertThat ( tags . get ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString ())).isEqualTo ( DEFAULT_ROLE_INSTANCE );
50+ assertThat (tags ). contains ( entry ( ContextTagKeys .AI_CLOUD_ROLE .toString (), "fake-service-name" ),
51+ entry ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), DEFAULT_ROLE_INSTANCE )).hasSize ( 2 );
5052 }
5153
5254 @ Test
5355 void testServiceInstanceFromResource () {
5456 Resource resource = createTestResource (null , null , "fake-service-instance" );
55- new ResourceParser ().updateRoleNameAndInstance (builder , resource );
56- assertThat (builder .build ().getTags ())
57- .contains (entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), "fake-service-instance" ));
57+
58+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
59+
60+ Map <String , String > tags = builder .build ().getTags ();
61+ assertThat (tags ).contains (entry (ContextTagKeys .AI_CLOUD_ROLE .toString (), DEFAULT_ROLE_NAME ),
62+ entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), "fake-service-instance" )).hasSize (2 );
63+ }
64+
65+ @ Test
66+ void testVersionFromResource () {
67+ Resource resource = Resource .create (Attributes .of (ServiceAttributes .SERVICE_VERSION , "fake-service-version" ));
68+
69+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
70+
71+ Map <String , String > tags = builder .build ().getTags ();
72+ assertThat (tags ).contains (entry (ContextTagKeys .AI_CLOUD_ROLE .toString (), DEFAULT_ROLE_NAME ),
73+ entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), DEFAULT_ROLE_INSTANCE ),
74+ entry (ContextTagKeys .AI_APPLICATION_VER .toString (), "fake-service-version" )).hasSize (3 );
5875 }
5976
6077 @ Test
6178 void testServiceNamespaceFromResource () {
6279 Resource resource = createTestResource (null , "fake-service-namespace" , null );
63- new ResourceParser ().updateRoleNameAndInstance (builder , resource );
80+
81+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
82+
6483 Map <String , String > tags = builder .build ().getTags ();
65- assertThat (tags . get ( ContextTagKeys .AI_CLOUD_ROLE .toString ())). isEqualTo ( "[fake-service-namespace]" );
66- assertThat ( tags . get ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString ())).isEqualTo ( DEFAULT_ROLE_INSTANCE );
84+ assertThat (tags ). contains ( entry ( ContextTagKeys .AI_CLOUD_ROLE .toString (), "[fake-service-namespace]" ),
85+ entry ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), DEFAULT_ROLE_INSTANCE )).hasSize ( 2 );
6786 }
6887
6988 @ Test
7089 void testServiceNameAndInstanceFromResource () {
7190 Resource resource = createTestResource ("fake-service-name" , null , "fake-instance" );
72- new ResourceParser ().updateRoleNameAndInstance (builder , resource );
91+
92+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
93+
7394 Map <String , String > tags = builder .build ().getTags ();
74- assertThat (tags . get ( ContextTagKeys .AI_CLOUD_ROLE .toString ())). isEqualTo ( "fake-service-name" );
75- assertThat ( tags . get ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString ())). isEqualTo ( "fake-instance" );
95+ assertThat (tags ). contains ( entry ( ContextTagKeys .AI_CLOUD_ROLE .toString (), "fake-service-name" ),
96+ entry ( ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), "fake-instance" )). hasSize ( 2 );
7697 }
7798
7899 @ Test
79100 void testServiceNameAndInstanceAndNamespaceFromResource () {
80101 Resource resource = createTestResource ("fake-service-name" , "fake-service-namespace" , "fake-instance" );
81- new ResourceParser ().updateRoleNameAndInstance (builder , resource );
102+
103+ new ResourceParser ().updateRoleNameAndInstanceAndVersion (builder , resource );
104+
82105 Map <String , String > tags = builder .build ().getTags ();
83- assertThat (tags .get (ContextTagKeys .AI_CLOUD_ROLE .toString ()))
84- .isEqualTo ("[fake-service-namespace]/fake-service-name" );
85- assertThat (tags .get (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString ())).isEqualTo ("fake-instance" );
106+ assertThat (tags )
107+ .contains (entry (ContextTagKeys .AI_CLOUD_ROLE .toString (), "[fake-service-namespace]/fake-service-name" ),
108+ entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), "fake-instance" ))
109+ .hasSize (2 );
86110 }
87111
88112 @ Test
@@ -91,14 +115,12 @@ void testWebsiteSiteNameAndWebsiteInstanceId() {
91115 envVars .put ("WEBSITE_SITE_NAME" , "test_website_site_name" );
92116 envVars .put ("WEBSITE_INSTANCE_ID" , "test_website_instance_id" );
93117 Resource resource = createTestResource (null , null , null );
94- new ResourceParser (envVars ).updateRoleNameAndInstance (builder , resource );
95- Map <String , String > tags = builder .build ().getTags ();
96- assertThat (tags .get (ContextTagKeys .AI_CLOUD_ROLE .toString ())).isEqualTo ("test_website_site_name" );
97- assertThat (tags .get (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString ())).isEqualTo ("test_website_instance_id" );
98- }
99118
100- private static ConfigProperties getConfiguration () {
101- return DefaultConfigProperties .create (Collections .singletonMap ("HOSTNAME" , DEFAULT_ROLE_INSTANCE ));
119+ new ResourceParser (envVars ).updateRoleNameAndInstanceAndVersion (builder , resource );
120+
121+ Map <String , String > tags = builder .build ().getTags ();
122+ assertThat (tags ).contains (entry (ContextTagKeys .AI_CLOUD_ROLE .toString (), "test_website_site_name" ),
123+ entry (ContextTagKeys .AI_CLOUD_ROLE_INSTANCE .toString (), "test_website_instance_id" )).hasSize (2 );
102124 }
103125
104126 private static Resource createTestResource (@ Nullable String serviceName , @ Nullable String serviceNameSpace ,
0 commit comments