1717package com .google .cloud .bigtable .data .v2 .internal .csm .attributes ;
1818
1919import static com .google .common .truth .Truth .assertThat ;
20- import static org .mockito .Mockito .when ;
2120
22- import com .google .cloud .opentelemetry .detection .DetectedPlatform ;
23- import com .google .cloud .opentelemetry .detection .GCPPlatformDetector .SupportedPlatform ;
2421import com .google .common .base .Function ;
2522import com .google .common .base .Supplier ;
2623import com .google .common .collect .ImmutableMap ;
24+ import io .opentelemetry .api .common .Attributes ;
2725import java .util .Map ;
2826import org .junit .jupiter .api .Test ;
29- import org .junit .jupiter .api .extension .ExtendWith ;
30- import org .mockito .Mock ;
31- import org .mockito .junit .jupiter .MockitoExtension ;
3227
33- @ ExtendWith (MockitoExtension .class )
3428class EnvInfoTest {
3529 private static final Supplier <String > NULL_HOST = () -> null ;
3630
3731 @ SuppressWarnings ("UnnecessaryLambda" )
3832 private static final Function <String , String > NULL_ENV = (ignored ) -> null ;
3933
40- @ Mock private DetectedPlatform detectedPlatform ;
41-
4234 @ Test
4335 void testUid () {
44- when (detectedPlatform .getSupportedPlatform ()).thenReturn (SupportedPlatform .UNKNOWN_PLATFORM );
45-
46- EnvInfo info1 = EnvInfo .detect (detectedPlatform , NULL_ENV , NULL_HOST );
47- EnvInfo info2 = EnvInfo .detect (detectedPlatform , NULL_ENV , NULL_HOST );
36+ EnvInfo info1 = EnvInfo .detect (Attributes .empty (), NULL_ENV , NULL_HOST );
37+ EnvInfo info2 = EnvInfo .detect (Attributes .empty (), NULL_ENV , NULL_HOST );
4838
4939 assertThat (info1 .getUid ()).isNotEmpty ();
5040 assertThat (info2 .getUid ()).isNotEmpty ();
@@ -53,8 +43,7 @@ void testUid() {
5343
5444 @ Test
5545 void testUnknown () {
56- when (detectedPlatform .getSupportedPlatform ()).thenReturn (SupportedPlatform .UNKNOWN_PLATFORM );
57- EnvInfo envInfo = EnvInfo .detect (detectedPlatform , NULL_ENV , NULL_HOST );
46+ EnvInfo envInfo = EnvInfo .detect (Attributes .empty (), NULL_ENV , NULL_HOST );
5847 assertThat (envInfo .getHostName ()).isEmpty ();
5948 assertThat (envInfo .getHostId ()).isEmpty ();
6049 assertThat (envInfo .getPlatform ()).isEqualTo ("unknown" );
@@ -63,19 +52,16 @@ void testUnknown() {
6352
6453 @ Test
6554 void testGce () {
66- when (detectedPlatform .getSupportedPlatform ())
67- .thenReturn (SupportedPlatform .GOOGLE_COMPUTE_ENGINE );
68- when (detectedPlatform .getProjectId ()).thenReturn ("my-project" );
69- when (detectedPlatform .getAttributes ())
70- .thenReturn (
71- ImmutableMap .of (
72- "machine_type" , "n2-standard-8" ,
73- "availability_zone" , "us-central1-c" ,
74- "instance_id" , "1234567890" ,
75- "instance_name" , "my-vm-name" ,
76- "cloud_region" , "us-central1" ,
77- "instance_hostname" , "my-vm-name.us-central1-c.c.my-project.google.com.internal" ));
78- EnvInfo envInfo = EnvInfo .detect (detectedPlatform , NULL_ENV , NULL_HOST );
55+ Attributes attributes =
56+ Attributes .builder ()
57+ .put ("cloud.platform" , "gcp_compute_engine" )
58+ .put ("cloud.account.id" , "my-project" )
59+ .put ("cloud.region" , "us-central1" )
60+ .put ("cloud.availability_zone" , "us-central1-c" )
61+ .put ("host.id" , "1234567890" )
62+ .put ("host.name" , "my-vm-name" )
63+ .build ();
64+ EnvInfo envInfo = EnvInfo .detect (attributes , NULL_ENV , NULL_HOST );
7965 assertThat (envInfo .getPlatform ()).isEqualTo ("gcp_compute_engine" );
8066 assertThat (envInfo .getProject ()).isEqualTo ("my-project" );
8167 assertThat (envInfo .getRegion ()).isEqualTo ("us-central1" );
@@ -84,20 +70,37 @@ void testGce() {
8470 }
8571
8672 @ Test
87- void testGke () {
88- when (detectedPlatform .getSupportedPlatform ())
89- .thenReturn (SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
90- when (detectedPlatform .getProjectId ()).thenReturn ("my-project" );
91- when (detectedPlatform .getAttributes ())
92- .thenReturn (
93- ImmutableMap .of (
94- "gke_cluster_name" , "my-cluster" ,
95- "gke_cluster_location" , "us-central1" ,
96- "gke_cluster_location_type" , "country-region" ,
97- "instance_id" , "1234567890" ));
73+ void testGkeRegionalCluster () {
74+ Attributes attributes =
75+ Attributes .builder ()
76+ .put ("cloud.platform" , "gcp_kubernetes_engine" )
77+ .put ("cloud.account.id" , "my-project" )
78+ .put ("cloud.region" , "us-central1" )
79+ .put ("host.id" , "1234567890" )
80+ .build ();
81+ Map <String , String > env = ImmutableMap .of ("HOSTNAME" , "my-hostname" );
82+
83+ EnvInfo envInfo = EnvInfo .detect (attributes , env ::get , NULL_HOST );
84+ assertThat (envInfo .getPlatform ()).isEqualTo ("gcp_kubernetes_engine" );
85+ assertThat (envInfo .getProject ()).isEqualTo ("my-project" );
86+ assertThat (envInfo .getRegion ()).isEqualTo ("us-central1" );
87+ assertThat (envInfo .getHostId ()).isEqualTo ("1234567890" );
88+ assertThat (envInfo .getHostName ()).isEqualTo ("my-hostname" );
89+ }
90+
91+ @ Test
92+ void testGkeZonalCluster () {
93+ // Zonal GKE clusters report their location via cloud.availability_zone instead of cloud.region.
94+ Attributes attributes =
95+ Attributes .builder ()
96+ .put ("cloud.platform" , "gcp_kubernetes_engine" )
97+ .put ("cloud.account.id" , "my-project" )
98+ .put ("cloud.availability_zone" , "us-central1-c" )
99+ .put ("host.id" , "1234567890" )
100+ .build ();
98101 Map <String , String > env = ImmutableMap .of ("HOSTNAME" , "my-hostname" );
99102
100- EnvInfo envInfo = EnvInfo .detect (detectedPlatform , env ::get , NULL_HOST );
103+ EnvInfo envInfo = EnvInfo .detect (attributes , env ::get , NULL_HOST );
101104 assertThat (envInfo .getPlatform ()).isEqualTo ("gcp_kubernetes_engine" );
102105 assertThat (envInfo .getProject ()).isEqualTo ("my-project" );
103106 assertThat (envInfo .getRegion ()).isEqualTo ("us-central1" );
@@ -106,18 +109,15 @@ void testGke() {
106109 }
107110
108111 @ Test
109- void testGkeHostanmeFallback () {
110- when (detectedPlatform .getSupportedPlatform ())
111- .thenReturn (SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
112- when (detectedPlatform .getProjectId ()).thenReturn ("my-project" );
113- when (detectedPlatform .getAttributes ())
114- .thenReturn (
115- ImmutableMap .of (
116- "gke_cluster_name" , "my-cluster" ,
117- "gke_cluster_location" , "us-central1" ,
118- "gke_cluster_location_type" , "country-region" ,
119- "instance_id" , "1234567890" ));
120- EnvInfo envInfo = EnvInfo .detect (detectedPlatform , NULL_ENV , () -> "my-hostname" );
112+ void testGkeHostnameFallback () {
113+ Attributes attributes =
114+ Attributes .builder ()
115+ .put ("cloud.platform" , "gcp_kubernetes_engine" )
116+ .put ("cloud.account.id" , "my-project" )
117+ .put ("cloud.region" , "us-central1" )
118+ .put ("host.id" , "1234567890" )
119+ .build ();
120+ EnvInfo envInfo = EnvInfo .detect (attributes , NULL_ENV , () -> "my-hostname" );
121121 assertThat (envInfo .getPlatform ()).isEqualTo ("gcp_kubernetes_engine" );
122122 assertThat (envInfo .getProject ()).isEqualTo ("my-project" );
123123 assertThat (envInfo .getRegion ()).isEqualTo ("us-central1" );
0 commit comments