in apm-agent-core/src/main/java/co/elastic/apm/agent/impl/metadata/CloudMetadataProvider.java [314:351]
static CloudProviderInfo deserializeGcpMetadata(@Nullable String rawMetadata) throws IOException {
if (rawMetadata == null) {
return null;
}
Map<String, Object> map = deserialize(rawMetadata);
CloudProviderInfo cloudProviderInfo = new CloudProviderInfo("gcp");
Object instanceData = map.get("instance");
if (instanceData instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> instanceMap = (Map<String, Object>) instanceData;
Long instanceId = (instanceMap.get("id") instanceof Long) ? (Long) instanceMap.get("id") : null;
String instanceName = (instanceMap.get("name") instanceof String) ? (String) instanceMap.get("name") : null;
String zone = instanceMap.get("zone") instanceof String ? (String) instanceMap.get("zone") : null;
cloudProviderInfo.setInstance(new NameAndIdField(instanceName, instanceId));
String machineType = instanceMap.get("machineType") instanceof String ? (String) instanceMap.get("machineType") : null;
if (machineType != null) {
String[] machinePathParts = machineType.split("/");
cloudProviderInfo.setMachine(new CloudProviderInfo.ProviderMachine(machinePathParts[machinePathParts.length - 1]));
}
if (zone != null) {
String[] zoneParts = zone.split("/");
String availabilityZone = zoneParts[zoneParts.length - 1];
cloudProviderInfo.setAvailabilityZone(availabilityZone);
int hyphenLastIndex = availabilityZone.lastIndexOf("-");
cloudProviderInfo.setRegion(hyphenLastIndex != -1 ? availabilityZone.substring(0, hyphenLastIndex) : null);
}
} else {
logger.warn("Error while parsing GCP metadata - expecting the value of the 'instance' entry to be a map but it is not");
}
Object projectData = map.get("project");
if (projectData instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> projectMap = (Map<String, Object>) projectData;
String projectId = projectMap.get("projectId") instanceof String ? (String) projectMap.get("projectId") : null;
cloudProviderInfo.setProject(new NameAndIdField(null, projectId));
} else {
logger.warn("Error while parsing GCP metadata - expecting the value of the 'project' entry to be a map but it is not");
}
return cloudProviderInfo;
}