Skip to content

Commit 45be0c7

Browse files
committed
Handle NumberFormatException when parsing numbers
1 parent 0435d65 commit 45be0c7

4 files changed

Lines changed: 103 additions & 61 deletions

File tree

azure-resources/src/main/java/io/opentelemetry/contrib/azure/resource/AzureFunctionsResourceProvider.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
import io.opentelemetry.sdk.resources.Resource;
1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.logging.Level;
22+
import java.util.logging.Logger;
2123

2224
public final class AzureFunctionsResourceProvider extends CloudResourceProvider {
2325

26+
private static final Logger logger =
27+
Logger.getLogger(AzureFunctionsResourceProvider.class.getName());
28+
2429
static final String FUNCTIONS_VERSION = "FUNCTIONS_EXTENSION_VERSION";
2530
private static final String FUNCTIONS_MEM_LIMIT = "WEBSITE_MEMORY_LIMIT_MB";
2631

@@ -60,7 +65,11 @@ public Attributes getAttributes() {
6065

6166
String limit = env.get(FUNCTIONS_MEM_LIMIT);
6267
if (limit != null) {
63-
builder.put(FAAS_MAX_MEMORY, Long.parseLong(limit));
68+
try {
69+
builder.put(FAAS_MAX_MEMORY, Long.parseLong(limit));
70+
} catch (NumberFormatException e) {
71+
logger.log(Level.WARNING, "Can't parse Azure Functions memory limit", e);
72+
}
6473
}
6574

6675
AzureEnvVarPlatform.addAttributesFromEnv(ENV_VAR_MAPPING, env, builder);

azure-resources/src/test/java/io/opentelemetry/contrib/azure/resource/AzureFunctionsResourceProviderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ void isNotFunction() {
5353
createResource(map).isEmpty();
5454
}
5555

56+
@Test
57+
void malformedMemoryLimit() {
58+
HashMap<String, String> map = new HashMap<>(DEFAULT_ENV_VARS);
59+
map.put("WEBSITE_MEMORY_LIMIT_MB", "not-a-number");
60+
61+
createResource(map)
62+
.containsEntry(CLOUD_PROVIDER, "azure")
63+
.containsEntry(CLOUD_PLATFORM, "azure.functions")
64+
.doesNotContainKey(FAAS_MAX_MEMORY);
65+
}
66+
5667
@NotNull
5768
private static AttributesAssert createResource(Map<String, String> map) {
5869
return assertThat(new AzureFunctionsResourceProvider(map).createResource(null).getAttributes());

gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import io.opentelemetry.sdk.resources.Resource;
5858
import java.util.Map;
5959
import java.util.Optional;
60+
import java.util.logging.Level;
6061
import java.util.logging.Logger;
6162

6263
@SuppressWarnings("MemberName")
@@ -244,10 +245,15 @@ private static void addGcrJobAttributes(
244245
attrBuilder.put(IncubatingAttributes.GCP_CLOUD_RUN_JOB_EXECUTION, jobExecutionKey));
245246
Optional.ofNullable(attributesMap.get(GCR_JOB_TASK_INDEX))
246247
.ifPresent(
247-
jobTaskIndex ->
248+
jobTaskIndex -> {
249+
try {
248250
attrBuilder.put(
249251
IncubatingAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX,
250-
Integer.parseInt(jobTaskIndex)));
252+
Integer.parseInt(jobTaskIndex));
253+
} catch (NumberFormatException e) {
254+
LOGGER.log(Level.WARNING, "Can't parse GCP Cloud Run job task index", e);
255+
}
256+
});
251257
}
252258

253259
/**

gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_TYPE;
5454
import static io.opentelemetry.semconv.incubating.K8sIncubatingAttributes.K8S_CLUSTER_NAME;
5555
import static org.assertj.core.api.Assertions.fail;
56+
import static org.mockito.Mockito.mock;
57+
import static org.mockito.Mockito.times;
5658
import static org.mockito.Mockito.verify;
59+
import static org.mockito.Mockito.when;
5760

5861
import com.google.common.collect.ImmutableMap;
5962
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
@@ -64,11 +67,10 @@
6467
import java.util.Map;
6568
import java.util.ServiceLoader;
6669
import org.junit.jupiter.api.Test;
67-
import org.mockito.Mockito;
6870

6971
class GCPResourceProviderTest {
7072
private static final String DUMMY_PROJECT_ID = "google-pid";
71-
private final ConfigProperties mockConfigProps = Mockito.mock(ConfigProperties.class);
73+
private final ConfigProperties mockConfigProps = mock(ConfigProperties.class);
7274
private final Map<String, String> mockGKECommonAttributes =
7375
new HashMap<>(
7476
ImmutableMap.of(
@@ -86,11 +88,11 @@ private static DetectedPlatform generateMockGcePlatform() {
8688
GCE_INSTANCE_NAME, "instance-name",
8789
GCE_MACHINE_TYPE, "gce-m2",
8890
GCE_INSTANCE_HOSTNAME, "instance-hostname"));
89-
DetectedPlatform mockGCEPlatform = Mockito.mock(DetectedPlatform.class);
90-
Mockito.when(mockGCEPlatform.getSupportedPlatform())
91+
DetectedPlatform mockGCEPlatform = mock(DetectedPlatform.class);
92+
when(mockGCEPlatform.getSupportedPlatform())
9193
.thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE);
92-
Mockito.when(mockGCEPlatform.getAttributes()).thenReturn(mockAttributes);
93-
Mockito.when(mockGCEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
94+
when(mockGCEPlatform.getAttributes()).thenReturn(mockAttributes);
95+
when(mockGCEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
9496
return mockGCEPlatform;
9597
}
9698

@@ -103,11 +105,11 @@ private DetectedPlatform generateMockGkePlatform(String gkeClusterLocationType)
103105
}
104106
mockAttributes.put(GKE_CLUSTER_LOCATION_TYPE, gkeClusterLocationType);
105107

106-
DetectedPlatform mockGKEPlatform = Mockito.mock(DetectedPlatform.class);
107-
Mockito.when(mockGKEPlatform.getSupportedPlatform())
108+
DetectedPlatform mockGKEPlatform = mock(DetectedPlatform.class);
109+
when(mockGKEPlatform.getSupportedPlatform())
108110
.thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
109-
Mockito.when(mockGKEPlatform.getAttributes()).thenReturn(mockAttributes);
110-
Mockito.when(mockGKEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
111+
when(mockGKEPlatform.getAttributes()).thenReturn(mockAttributes);
112+
when(mockGKEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
111113
return mockGKEPlatform;
112114
}
113115

@@ -128,10 +130,10 @@ private static DetectedPlatform generateMockServerlessPlatform(
128130
SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id",
129131
SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1",
130132
SERVERLESS_COMPUTE_AVAILABILITY_ZONE, "us-central1-b"));
131-
DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class);
132-
Mockito.when(mockServerlessPlatform.getSupportedPlatform()).thenReturn(platform);
133-
Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
134-
Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
133+
DetectedPlatform mockServerlessPlatform = mock(DetectedPlatform.class);
134+
when(mockServerlessPlatform.getSupportedPlatform()).thenReturn(platform);
135+
when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
136+
when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
135137
return mockServerlessPlatform;
136138
}
137139

@@ -144,11 +146,11 @@ private static DetectedPlatform generateMockGcrJobPlatform() {
144146
SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1",
145147
GCR_JOB_TASK_INDEX, "1",
146148
GCR_JOB_EXECUTION_KEY, "serverless-job-a1b2c3"));
147-
DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class);
148-
Mockito.when(mockServerlessPlatform.getSupportedPlatform())
149+
DetectedPlatform mockServerlessPlatform = mock(DetectedPlatform.class);
150+
when(mockServerlessPlatform.getSupportedPlatform())
149151
.thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB);
150-
Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
151-
Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
152+
when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
153+
when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
152154
return mockServerlessPlatform;
153155
}
154156

@@ -161,11 +163,11 @@ private static DetectedPlatform generateMockGaePlatform() {
161163
GAE_INSTANCE_ID, "gae-instance-id",
162164
GAE_CLOUD_REGION, "us-central1",
163165
GAE_AVAILABILITY_ZONE, "us-central1-b"));
164-
DetectedPlatform mockGAEPlatform = Mockito.mock(DetectedPlatform.class);
165-
Mockito.when(mockGAEPlatform.getSupportedPlatform())
166+
DetectedPlatform mockGAEPlatform = mock(DetectedPlatform.class);
167+
when(mockGAEPlatform.getSupportedPlatform())
166168
.thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_APP_ENGINE);
167-
Mockito.when(mockGAEPlatform.getAttributes()).thenReturn(mockAttributes);
168-
Mockito.when(mockGAEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
169+
when(mockGAEPlatform.getAttributes()).thenReturn(mockAttributes);
170+
when(mockGAEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
169171
return mockGAEPlatform;
170172
}
171173

@@ -176,22 +178,22 @@ private static DetectedPlatform generateMockUnknownPlatform() {
176178
GCE_INSTANCE_ID, "instance-id",
177179
GCE_CLOUD_REGION, "australia-southeast1"));
178180

179-
DetectedPlatform mockUnknownPlatform = Mockito.mock(DetectedPlatform.class);
180-
Mockito.when(mockUnknownPlatform.getSupportedPlatform())
181+
DetectedPlatform mockUnknownPlatform = mock(DetectedPlatform.class);
182+
when(mockUnknownPlatform.getSupportedPlatform())
181183
.thenReturn(GcpPlatformDetector.SupportedPlatform.UNKNOWN_PLATFORM);
182-
Mockito.when(mockUnknownPlatform.getAttributes()).thenReturn(mockAttributes);
184+
when(mockUnknownPlatform.getAttributes()).thenReturn(mockAttributes);
183185
return mockUnknownPlatform;
184186
}
185187

186188
@Test
187189
void testGceResourceAttributesMapping() {
188-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
190+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
189191
DetectedPlatform mockPlatform = generateMockGcePlatform();
190-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
192+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
191193
Map<String, String> detectedAttributes = mockPlatform.getAttributes();
192194

193195
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
194-
verify(mockPlatform, Mockito.times(1)).getProjectId();
196+
verify(mockPlatform, times(1)).getProjectId();
195197

196198
assertThat(gotResource.getAttributes())
197199
.hasSize(10)
@@ -209,12 +211,12 @@ void testGceResourceAttributesMapping() {
209211

210212
@Test
211213
void testGkeResourceAttributesMapping_LocationTypeRegion() {
212-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
214+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
213215
DetectedPlatform mockPlatform = generateMockGkePlatform(GKE_LOCATION_TYPE_REGION);
214-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
216+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
215217

216218
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
217-
verify(mockPlatform, Mockito.times(1)).getProjectId();
219+
verify(mockPlatform, times(1)).getProjectId();
218220

219221
verifyGkeMapping(gotResource, mockPlatform);
220222
assertThat(gotResource.getAttributes())
@@ -226,12 +228,12 @@ void testGkeResourceAttributesMapping_LocationTypeRegion() {
226228

227229
@Test
228230
void testGkeResourceAttributesMapping_LocationTypeZone() {
229-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
231+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
230232
DetectedPlatform mockPlatform = generateMockGkePlatform(GKE_LOCATION_TYPE_ZONE);
231-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
233+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
232234

233235
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
234-
verify(mockPlatform, Mockito.times(1)).getProjectId();
236+
verify(mockPlatform, times(1)).getProjectId();
235237

236238
verifyGkeMapping(gotResource, mockPlatform);
237239
assertThat(gotResource.getAttributes())
@@ -248,18 +250,18 @@ void testGkeResourceAttributesMapping_LocationTypeInvalid() {
248250
mockGKEAttributes.put(GKE_CLUSTER_LOCATION_TYPE, "INVALID");
249251
mockGKEAttributes.put(GKE_CLUSTER_LOCATION, "some-location");
250252

251-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
252-
DetectedPlatform mockPlatform = Mockito.mock(DetectedPlatform.class);
253-
Mockito.when(mockPlatform.getSupportedPlatform())
253+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
254+
DetectedPlatform mockPlatform = mock(DetectedPlatform.class);
255+
when(mockPlatform.getSupportedPlatform())
254256
.thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
255-
Mockito.when(mockPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
256-
Mockito.when(mockPlatform.getAttributes()).thenReturn(mockGKEAttributes);
257-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
257+
when(mockPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
258+
when(mockPlatform.getAttributes()).thenReturn(mockGKEAttributes);
259+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
258260

259261
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
260262

261263
verifyGkeMapping(gotResource, mockPlatform);
262-
verify(mockPlatform, Mockito.times(1)).getProjectId();
264+
verify(mockPlatform, times(1)).getProjectId();
263265
assertThat(gotResource.getAttributes())
264266
.hasSize(5)
265267
.containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID)
@@ -269,12 +271,12 @@ void testGkeResourceAttributesMapping_LocationTypeInvalid() {
269271

270272
@Test
271273
void testGkeResourceAttributesMapping_LocationMissing() {
272-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
274+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
273275
DetectedPlatform mockPlatform = generateMockGkePlatform("");
274-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
276+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
275277

276278
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
277-
verify(mockPlatform, Mockito.times(1)).getProjectId();
279+
verify(mockPlatform, times(1)).getProjectId();
278280
verifyGkeMapping(gotResource, mockPlatform);
279281
assertThat(gotResource.getAttributes())
280282
.hasSize(5)
@@ -294,13 +296,13 @@ private static void verifyGkeMapping(Resource gotResource, DetectedPlatform dete
294296

295297
@Test
296298
void testGcrServiceResourceAttributesMapping() {
297-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
299+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
298300
DetectedPlatform mockPlatform =
299301
generateMockServerlessPlatform(GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN);
300-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
302+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
301303

302304
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
303-
verify(mockPlatform, Mockito.times(1)).getProjectId();
305+
verify(mockPlatform, times(1)).getProjectId();
304306

305307
verifyServerlessMapping(gotResource, mockPlatform);
306308
assertThat(gotResource.getAttributes())
@@ -311,14 +313,14 @@ void testGcrServiceResourceAttributesMapping() {
311313

312314
@Test
313315
void testGcfResourceAttributeMapping() {
314-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
316+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
315317
DetectedPlatform mockPlatform =
316318
generateMockServerlessPlatform(
317319
GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS);
318-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
320+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
319321

320322
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
321-
verify(mockPlatform, Mockito.times(1)).getProjectId();
323+
verify(mockPlatform, times(1)).getProjectId();
322324

323325
verifyServerlessMapping(gotResource, mockPlatform);
324326
assertThat(gotResource.getAttributes())
@@ -342,13 +344,13 @@ private static void verifyServerlessMapping(
342344

343345
@Test
344346
void testGcrJobResourceAttributesMapping() {
345-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
347+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
346348
DetectedPlatform mockPlatform = generateMockGcrJobPlatform();
347-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
349+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
348350
Map<String, String> detectedAttributes = mockPlatform.getAttributes();
349351

350352
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
351-
verify(mockPlatform, Mockito.times(1)).getProjectId();
353+
verify(mockPlatform, times(1)).getProjectId();
352354

353355
assertThat(gotResource.getAttributes())
354356
.hasSize(8)
@@ -365,15 +367,29 @@ void testGcrJobResourceAttributesMapping() {
365367
.containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
366368
}
367369

370+
@Test
371+
void testGcrJobResourceAttributesMappingWithMalformedTaskIndex() {
372+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
373+
DetectedPlatform mockPlatform = generateMockGcrJobPlatform();
374+
mockPlatform.getAttributes().put(GCR_JOB_TASK_INDEX, "not-a-number");
375+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
376+
377+
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
378+
379+
assertThat(gotResource.getAttributes())
380+
.doesNotContainKey(GCP_CLOUD_RUN_JOB_TASK_INDEX)
381+
.containsEntry(CLOUD_PROVIDER, GCP);
382+
}
383+
368384
@Test
369385
void testGaeResourceAttributeMapping() {
370-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
386+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
371387
DetectedPlatform mockPlatform = generateMockGaePlatform();
372-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
388+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
373389
Map<String, String> detectedAttributes = mockPlatform.getAttributes();
374390

375391
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
376-
verify(mockPlatform, Mockito.times(1)).getProjectId();
392+
verify(mockPlatform, times(1)).getProjectId();
377393

378394
assertThat(gotResource.getAttributes())
379395
.hasSize(8)
@@ -389,9 +405,9 @@ void testGaeResourceAttributeMapping() {
389405

390406
@Test
391407
void testUnknownPlatformResourceAttributesMapping() {
392-
GcpPlatformDetector mockDetector = Mockito.mock(GcpPlatformDetector.class);
408+
GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class);
393409
DetectedPlatform mockPlatform = generateMockUnknownPlatform();
394-
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
410+
when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
395411

396412
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
397413
assertThat(gotResource.getAttributes()).isEmpty();

0 commit comments

Comments
 (0)