in util/src/main/java/com/google/cloud/healthcare/imaging/dicomadapter/monitoring/MonitoringService.java [61:105]
private MonitoringService(String projectId, IMonitoringEvent[] monitoredEvents,
HttpRequestFactory requestFactory) throws IOException {
client = MetricServiceClient.create();
aggregateEvents = new HashMap<>();
this.projectId = projectId;
this.monitoredEvents = monitoredEvents;
// configure Resource
MonitoredResource.Builder resourceBuilder = MonitoredResource.newBuilder();
Map<String, String> resourceLabels = new HashMap<>();
resourceLabels.put("project_id", this.projectId);
Map<String, String> env = System.getenv();
String podName = env.get("ENV_POD_NAME");
String namespaceName = env.get("ENV_POD_NAMESPACE");
String containerName = env.get("ENV_CONTAINER_NAME");
String clusterName = GcpMetadataUtil.get(requestFactory, META_CLUSTER_NAME);
String location = GcpMetadataUtil.get(requestFactory, META_LOCATION);
if (location != null) {
// GCPMetadata returns locations as "projects/[NUMERIC_PROJECT_ID]/zones/[ZONE]"
// Only last part is necessary here.
location = location.substring(location.lastIndexOf('/') + 1);
}
if (podName != null && namespaceName != null && containerName != null &&
clusterName != null && location != null) {
resourceLabels.put("pod_name", podName);
resourceLabels.put("namespace_name", namespaceName);
resourceLabels.put("container_name", containerName);
resourceLabels.put("cluster_name", clusterName);
resourceLabels.put("location", location);
resourceBuilder.setType("k8s_container");
} else {
resourceBuilder.setType("global");
}
this.monitoredResource = resourceBuilder.putAllLabels(resourceLabels).build();
log.info("monitoredResource = {}", monitoredResource);
service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(MonitoringService.this::flush,
DELAY, DELAY, TimeUnit.SECONDS);
}