Java Code Examples for com.google.api.MonitoredResource#Builder
The following examples show how to use
com.google.api.MonitoredResource#Builder .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 6 votes |
static MonitoredResource getDefaultResource() { MonitoredResource.Builder builder = MonitoredResource.newBuilder(); // Populate internal resource label for defaulting project_id label. // This allows stats from other projects (e.g from GAE running in another project) to be // collected. if (MetadataConfig.getProjectId() != null) { builder.putLabels(STACKDRIVER_PROJECT_ID_KEY, MetadataConfig.getProjectId()); } Resource autoDetectedResource = ResourceUtils.detectResource(); if (autoDetectedResource == null || autoDetectedResource.getType() == null) { builder.setType(GLOBAL); return builder.build(); } setResourceForBuilder(builder, autoDetectedResource); return builder.build(); }
Example 2
Source File: StackdriverExportUtilsTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void setResourceForBuilder_GcpInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(CloudResource.ACCOUNT_ID_KEY, "proj1"); resourceLabels.put(CloudResource.PROVIDER_KEY, CloudResource.PROVIDER_GCP); resourceLabels.put(HostResource.ID_KEY, "inst1"); resourceLabels.put(CloudResource.ZONE_KEY, "zone1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("instance_id", "inst1"); expectedResourceLabels.put("zone", "zone1"); Resource resource = Resource.create(HostResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("gce_instance"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(3); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
Example 3
Source File: StackdriverExportUtilsTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void setResourceForBuilder_K8sInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(CloudResource.ZONE_KEY, "zone1"); resourceLabels.put(HostResource.ID_KEY, "instance1"); resourceLabels.put(K8sResource.CLUSTER_NAME_KEY, "cluster1"); resourceLabels.put(ContainerResource.NAME_KEY, "container1"); resourceLabels.put(K8sResource.NAMESPACE_NAME_KEY, "namespace1"); resourceLabels.put(K8sResource.POD_NAME_KEY, "pod1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("location", "zone1"); expectedResourceLabels.put("cluster_name", "cluster1"); expectedResourceLabels.put("namespace_name", "namespace1"); expectedResourceLabels.put("pod_name", "pod1"); expectedResourceLabels.put("container_name", "container1"); Resource resource = Resource.create(ContainerResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("k8s_container"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(6); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
Example 4
Source File: StackdriverExportUtilsTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void setResourceForBuilder_AwsInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(CloudResource.REGION_KEY, "region1"); resourceLabels.put(CloudResource.PROVIDER_KEY, CloudResource.PROVIDER_AWS); resourceLabels.put(CloudResource.ACCOUNT_ID_KEY, "account1"); resourceLabels.put(HostResource.ID_KEY, "instance1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("instance_id", "instance1"); expectedResourceLabels.put( "region", StackdriverExportUtils.AWS_REGION_VALUE_PREFIX + "region1"); expectedResourceLabels.put("aws_account", "account1"); Resource resource = Resource.create(HostResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("aws_ec2_instance"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(4); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
Example 5
Source File: MonitoringService.java From healthcare-dicom-dicomweb-adapter with Apache License 2.0 | 4 votes |
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); }
Example 6
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 4 votes |
@VisibleForTesting static void setResourceForBuilder( MonitoredResource.Builder builder, Resource autoDetectedResource) { String type = autoDetectedResource.getType(); if (type == null) { return; } String sdType = GLOBAL; Map<String, String> mappings = null; if (HostResource.TYPE.equals(type)) { String provider = autoDetectedResource.getLabels().get(CloudResource.PROVIDER_KEY); if (CloudResource.PROVIDER_GCP.equals(provider)) { sdType = GCP_GCE_INSTANCE; mappings = GCP_RESOURCE_MAPPING; } else if (CloudResource.PROVIDER_AWS.equals(provider)) { sdType = AWS_EC2_INSTANCE; mappings = AWS_RESOURCE_MAPPING; } } else if (ContainerResource.TYPE.equals(type)) { sdType = K8S_CONTAINER; mappings = K8S_RESOURCE_MAPPING; } builder.setType(sdType); if (GLOBAL.equals(sdType) || mappings == null) { return; } Map<String, String> resLabels = autoDetectedResource.getLabels(); for (Map.Entry<String, String> entry : mappings.entrySet()) { if (entry.getValue() != null && resLabels.containsKey(entry.getValue())) { String resourceLabelKey = entry.getKey(); String resourceLabelValue = resLabels.get(entry.getValue()); if (AWS_EC2_INSTANCE.equals(sdType) && "region".equals(resourceLabelKey)) { // Add "aws:" prefix to AWS EC2 region label. This is Stackdriver specific requirement. resourceLabelValue = AWS_REGION_VALUE_PREFIX + resourceLabelValue; } builder.putLabels(resourceLabelKey, resourceLabelValue); } } }