forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureFunctionsResourceProvider.java
More file actions
70 lines (54 loc) · 2.43 KB
/
AzureFunctionsResourceProvider.java
File metadata and controls
70 lines (54 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.azure.resource;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CLOUD_REGION;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CloudPlatformIncubatingValues.AZURE_FUNCTIONS;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.FAAS_INSTANCE;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.FAAS_MAX_MEMORY;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.FAAS_NAME;
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.FAAS_VERSION;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.resources.Resource;
import java.util.HashMap;
import java.util.Map;
public class AzureFunctionsResourceProvider extends CloudResourceProvider {
static final String FUNCTIONS_VERSION = "FUNCTIONS_EXTENSION_VERSION";
private static final String FUNCTIONS_MEM_LIMIT = "WEBSITE_MEMORY_LIMIT_MB";
private static final Map<AttributeKey<String>, String> ENV_VAR_MAPPING = new HashMap<>();
static {
ENV_VAR_MAPPING.put(CLOUD_REGION, AzureAppServiceResourceProvider.REGION_NAME);
ENV_VAR_MAPPING.put(FAAS_NAME, AzureAppServiceResourceProvider.WEBSITE_SITE_NAME);
ENV_VAR_MAPPING.put(FAAS_VERSION, FUNCTIONS_VERSION);
ENV_VAR_MAPPING.put(FAAS_INSTANCE, AzureAppServiceResourceProvider.WEBSITE_INSTANCE_ID);
}
private final Map<String, String> env;
// SPI
public AzureFunctionsResourceProvider() {
this(System.getenv());
}
// Visible for testing
AzureFunctionsResourceProvider(Map<String, String> env) {
this.env = env;
}
@Override
public Resource createResource() {
return Resource.create(getAttributes());
}
public Attributes getAttributes() {
AzureEnvVarPlatform detect = AzureEnvVarPlatform.detect(env);
if (detect != AzureEnvVarPlatform.FUNCTIONS) {
return Attributes.empty();
}
AttributesBuilder builder = AzureVmResourceProvider.azureAttributeBuilder(AZURE_FUNCTIONS);
String limit = env.get(FUNCTIONS_MEM_LIMIT);
if (limit != null) {
builder.put(FAAS_MAX_MEMORY, Long.parseLong(limit));
}
AzureEnvVarPlatform.addAttributesFromEnv(ENV_VAR_MAPPING, env, builder);
return builder.build();
}
}