@@ -168,6 +168,122 @@ public void testMapResourcesWithAwsEc2Instance() {
168168 });
169169 }
170170
171+ @ Test
172+ public void testMapResourcesWithGenericTaskFallback_FAASIgnored () {
173+ Map <AttributeKey <String >, String > testAttributes =
174+ java .util .stream .Stream .of (
175+ new Object [][] {
176+ {ResourceAttributes .SERVICE_NAME , "my-service-prevailed" },
177+ {ResourceAttributes .FAAS_NAME , "my-service-ignored" },
178+ {ResourceAttributes .SERVICE_NAMESPACE , "prod" },
179+ {ResourceAttributes .FAAS_INSTANCE , "1234" }
180+ })
181+ .collect (
182+ Collectors .toMap (data -> (AttributeKey <String >) data [0 ], data -> (String ) data [1 ]));
183+ AttributesBuilder attrBuilder = Attributes .builder ();
184+ testAttributes .forEach (attrBuilder ::put );
185+ Attributes attributes = attrBuilder .build ();
186+
187+ GcpResource monitoredResource =
188+ ResourceTranslator .mapResource (io .opentelemetry .sdk .resources .Resource .create (attributes ));
189+
190+ assertEquals ("generic_task" , monitoredResource .getResourceType ());
191+
192+ Map <String , String > monitoredResourceMap = monitoredResource .getResourceLabels ().getLabels ();
193+ assertEquals (4 , monitoredResourceMap .size ());
194+
195+ Map <String , String > expectedMappings =
196+ Stream .of (
197+ new Object [][] {
198+ {"job" , "my-service-prevailed" },
199+ {"namespace" , "prod" },
200+ {"task_id" , "1234" },
201+ {"location" , "global" },
202+ })
203+ .collect (Collectors .toMap (data -> (String ) data [0 ], data -> (String ) data [1 ]));
204+ expectedMappings .forEach (
205+ (key , value ) -> {
206+ assertEquals (value , monitoredResourceMap .get (key ));
207+ });
208+ }
209+
210+ @ Test
211+ public void testMapResourcesWithGenericTaskFallback_FAASPrevailed () {
212+ Map <AttributeKey <String >, String > testAttributes =
213+ java .util .stream .Stream .of (
214+ new Object [][] {
215+ {ResourceAttributes .SERVICE_NAME , "unknown_service_foo" },
216+ {ResourceAttributes .FAAS_NAME , "my-service-faas" },
217+ {ResourceAttributes .SERVICE_NAMESPACE , "prod" },
218+ {ResourceAttributes .FAAS_INSTANCE , "1234" }
219+ })
220+ .collect (
221+ Collectors .toMap (data -> (AttributeKey <String >) data [0 ], data -> (String ) data [1 ]));
222+ AttributesBuilder attrBuilder = Attributes .builder ();
223+ testAttributes .forEach (attrBuilder ::put );
224+ Attributes attributes = attrBuilder .build ();
225+
226+ GcpResource monitoredResource =
227+ ResourceTranslator .mapResource (io .opentelemetry .sdk .resources .Resource .create (attributes ));
228+
229+ assertEquals ("generic_task" , monitoredResource .getResourceType ());
230+
231+ Map <String , String > monitoredResourceMap = monitoredResource .getResourceLabels ().getLabels ();
232+ assertEquals (4 , monitoredResourceMap .size ());
233+
234+ Map <String , String > expectedMappings =
235+ Stream .of (
236+ new Object [][] {
237+ {"job" , "my-service-faas" },
238+ {"namespace" , "prod" },
239+ {"task_id" , "1234" },
240+ {"location" , "global" },
241+ })
242+ .collect (Collectors .toMap (data -> (String ) data [0 ], data -> (String ) data [1 ]));
243+ expectedMappings .forEach (
244+ (key , value ) -> {
245+ assertEquals (value , monitoredResourceMap .get (key ));
246+ });
247+ }
248+
249+ @ Test
250+ public void testMapResourcesWithGenericTaskFallback_UnknownService () {
251+ Map <AttributeKey <String >, String > testAttributes =
252+ java .util .stream .Stream .of (
253+ new Object [][] {
254+ {ResourceAttributes .SERVICE_NAME , "unknown_service_foo" },
255+ {ResourceAttributes .SERVICE_NAMESPACE , "prod" },
256+ {ResourceAttributes .FAAS_INSTANCE , "1234" }
257+ })
258+ .collect (
259+ Collectors .toMap (data -> (AttributeKey <String >) data [0 ], data -> (String ) data [1 ]));
260+ AttributesBuilder attrBuilder = Attributes .builder ();
261+ testAttributes .forEach (attrBuilder ::put );
262+ Attributes attributes = attrBuilder .build ();
263+
264+ GcpResource monitoredResource =
265+ ResourceTranslator .mapResource (io .opentelemetry .sdk .resources .Resource .create (attributes ));
266+
267+ assertEquals ("generic_task" , monitoredResource .getResourceType ());
268+
269+ Map <String , String > monitoredResourceMap = monitoredResource .getResourceLabels ().getLabels ();
270+ assertEquals (4 , monitoredResourceMap .size ());
271+
272+ Map <String , String > expectedMappings =
273+ Stream .of (
274+ new Object [][] {
275+ {"job" , "unknown_service_foo" },
276+ {"namespace" , "prod" },
277+ {"task_id" , "1234" },
278+ {"location" , "global" },
279+ })
280+ .collect (Collectors .toMap (data -> (String ) data [0 ], data -> (String ) data [1 ]));
281+ expectedMappings .forEach (
282+ (key , value ) -> {
283+ assertEquals (value , monitoredResourceMap .get (key ));
284+ });
285+ }
286+
171287 @ Test
172288 public void testMapResourcesWithGlobal () {
173289 Map <AttributeKey <String >, String > testAttributes =
@@ -180,10 +296,7 @@ public void testMapResourcesWithGlobal() {
180296 .collect (
181297 Collectors .toMap (data -> (AttributeKey <String >) data [0 ], data -> (String ) data [1 ]));
182298 AttributesBuilder attrBuilder = Attributes .builder ();
183- testAttributes .forEach (
184- (key , value ) -> {
185- attrBuilder .put (key , value );
186- });
299+ testAttributes .forEach (attrBuilder ::put );
187300 Attributes attributes = attrBuilder .build ();
188301
189302 GcpResource monitoredResource =
@@ -209,24 +322,59 @@ public void testMapResourcesWithGlobal() {
209322 });
210323 }
211324
325+ @ Test
326+ public void testMapResourcesFallbackServiceNameOnly () {
327+ Map <AttributeKey <String >, String > testAttributes =
328+ java .util .stream .Stream .of (
329+ new Object [][] {
330+ {ResourceAttributes .SERVICE_NAME , "unknown_service" },
331+ {ResourceAttributes .SERVICE_NAMESPACE , "prod" }
332+ })
333+ .collect (
334+ Collectors .toMap (data -> (AttributeKey <String >) data [0 ], data -> (String ) data [1 ]));
335+ AttributesBuilder attrBuilder = Attributes .builder ();
336+ testAttributes .forEach (attrBuilder ::put );
337+ Attributes attributes = attrBuilder .build ();
338+
339+ GcpResource monitoredResource =
340+ ResourceTranslator .mapResource (io .opentelemetry .sdk .resources .Resource .create (attributes ));
341+
342+ assertEquals ("generic_node" , monitoredResource .getResourceType ());
343+
344+ Map <String , String > monitoredResourceMap = monitoredResource .getResourceLabels ().getLabels ();
345+ assertEquals (3 , monitoredResourceMap .size ());
346+
347+ Map <String , String > expectedMappings =
348+ Stream .of (
349+ new Object [][] {
350+ {"namespace" , "prod" },
351+ {"node_id" , "" },
352+ {"location" , "global" },
353+ })
354+ .collect (Collectors .toMap (data -> (String ) data [0 ], data -> (String ) data [1 ]));
355+ expectedMappings .forEach (
356+ (key , value ) -> {
357+ assertEquals (value , monitoredResourceMap .get (key ));
358+ });
359+ }
360+
212361 @ Test
213362 public void testMapResourcesFallback () {
214363 Attributes attributes = Attributes .builder ().build ();
215364
216365 GcpResource monitoredResource =
217366 ResourceTranslator .mapResource (io .opentelemetry .sdk .resources .Resource .create (attributes ));
218367
219- assertEquals ("generic_task " , monitoredResource .getResourceType ());
368+ assertEquals ("generic_node " , monitoredResource .getResourceType ());
220369
221370 Map <String , String > monitoredResourceMap = monitoredResource .getResourceLabels ().getLabels ();
222- assertEquals (4 , monitoredResourceMap .size ());
371+ assertEquals (3 , monitoredResourceMap .size ());
223372
224373 Map <String , String > expectedMappings =
225374 Stream .of (
226375 new Object [][] {
227- {"job" , "" },
228376 {"namespace" , "" },
229- {"task_id " , "" },
377+ {"node_id " , "" },
230378 {"location" , "global" },
231379 })
232380 .collect (Collectors .toMap (data -> (String ) data [0 ], data -> (String ) data [1 ]));
0 commit comments