|
16 | 16 | package com.google.cloud.opentelemetry.metric;
|
17 | 17 |
|
18 | 18 | import com.google.api.MonitoredResource;
|
19 |
| -import io.opentelemetry.api.common.AttributeKey; |
20 |
| -import io.opentelemetry.api.common.Attributes; |
| 19 | +import com.google.cloud.opentelemetry.resource.GcpResource; |
21 | 20 | import io.opentelemetry.sdk.resources.Resource;
|
22 |
| -import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; |
23 |
| -import java.util.List; |
24 |
| -import java.util.Optional; |
25 | 21 |
|
26 | 22 | /** Translates from OpenTelemetry Resource into Google Cloud Monitoring's MonitoredResource. */
|
27 | 23 | public class ResourceTranslator {
|
28 | 24 | private ResourceTranslator() {}
|
29 | 25 |
|
30 |
| - @com.google.auto.value.AutoValue |
31 |
| - public abstract static class AttributeMapping { |
32 |
| - /** The label name used in GCP's MonitoredResource. */ |
33 |
| - public abstract String getLabelName(); |
34 |
| - /** The list of OTEL keys that can be used for this resource label, in priority order. */ |
35 |
| - public abstract java.util.List<AttributeKey<?>> getOtelKeys(); |
36 |
| - /** A fallback value to set the resource. */ |
37 |
| - public abstract Optional<String> fallbackLiteral(); |
38 |
| - |
39 |
| - public void fill(Resource resource, MonitoredResource.Builder builder) { |
40 |
| - for (AttributeKey<?> key : getOtelKeys()) { |
41 |
| - Object value = resource.getAttribute(key); |
42 |
| - if (value != null) { |
43 |
| - builder.putLabels(getLabelName(), value.toString()); |
44 |
| - return; |
45 |
| - } |
46 |
| - } |
47 |
| - fallbackLiteral().ifPresent(value -> builder.putLabels(getLabelName(), value)); |
48 |
| - } |
49 |
| - |
50 |
| - public static AttributeMapping create(String labelName, AttributeKey<?> otelKey) { |
51 |
| - return new AutoValue_ResourceTranslator_AttributeMapping( |
52 |
| - labelName, java.util.Collections.singletonList(otelKey), Optional.empty()); |
53 |
| - } |
54 |
| - |
55 |
| - public static AttributeMapping create( |
56 |
| - String labelName, AttributeKey<?> otelKey, String fallbackLiteral) { |
57 |
| - return new AutoValue_ResourceTranslator_AttributeMapping( |
58 |
| - labelName, java.util.Collections.singletonList(otelKey), Optional.of(fallbackLiteral)); |
59 |
| - } |
60 |
| - |
61 |
| - public static AttributeMapping create( |
62 |
| - String labelName, java.util.List<AttributeKey<?>> otelKeys) { |
63 |
| - return new AutoValue_ResourceTranslator_AttributeMapping( |
64 |
| - labelName, otelKeys, Optional.empty()); |
65 |
| - } |
66 |
| - |
67 |
| - public static AttributeMapping create( |
68 |
| - String labelName, java.util.List<AttributeKey<?>> otelKeys, String fallbackLiteral) { |
69 |
| - return new AutoValue_ResourceTranslator_AttributeMapping( |
70 |
| - labelName, otelKeys, Optional.of(fallbackLiteral)); |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - private static List<AttributeMapping> GCE_INSTANCE_LABELS = |
75 |
| - java.util.Arrays.asList( |
76 |
| - AttributeMapping.create("zone", ResourceAttributes.CLOUD_AVAILABILITY_ZONE), |
77 |
| - AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID)); |
78 |
| - private static List<AttributeMapping> K8S_CONTAINER_LABELS = |
79 |
| - java.util.Arrays.asList( |
80 |
| - AttributeMapping.create("location", ResourceAttributes.CLOUD_AVAILABILITY_ZONE), |
81 |
| - AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME), |
82 |
| - AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME), |
83 |
| - AttributeMapping.create("container_name", ResourceAttributes.K8S_CONTAINER_NAME), |
84 |
| - AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME)); |
85 |
| - private static List<AttributeMapping> AWS_EC2_INSTANCE_LABELS = |
86 |
| - java.util.Arrays.asList( |
87 |
| - AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID), |
88 |
| - AttributeMapping.create("region", ResourceAttributes.CLOUD_AVAILABILITY_ZONE), |
89 |
| - AttributeMapping.create("aws_account", ResourceAttributes.CLOUD_ACCOUNT_ID)); |
90 |
| - private static List<AttributeMapping> GENERIC_TASK_LABELS = |
91 |
| - java.util.Arrays.asList( |
92 |
| - AttributeMapping.create( |
93 |
| - "location", |
94 |
| - java.util.Arrays.asList( |
95 |
| - ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION), |
96 |
| - "global"), |
97 |
| - AttributeMapping.create("namespace", ResourceAttributes.SERVICE_NAMESPACE, ""), |
98 |
| - AttributeMapping.create("job", ResourceAttributes.SERVICE_NAME, ""), |
99 |
| - AttributeMapping.create("task_id", ResourceAttributes.SERVICE_INSTANCE_ID, "")); |
100 |
| - |
101 | 26 | /** Converts a Java OpenTelemetyr SDK resoruce into a MonitoredResource from GCP. */
|
102 | 27 | public static MonitoredResource mapResource(Resource resource) {
|
103 |
| - String platform = resource.getAttribute(ResourceAttributes.CLOUD_PLATFORM); |
104 |
| - if (platform == null) { |
105 |
| - return mapBase(resource, "generic_task", GENERIC_TASK_LABELS); |
106 |
| - } |
107 |
| - switch (platform) { |
108 |
| - case ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE: |
109 |
| - return mapBase(resource, "gce_instance", GCE_INSTANCE_LABELS); |
110 |
| - case ResourceAttributes.CloudPlatformValues.GCP_KUBERNETES_ENGINE: |
111 |
| - return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS); |
112 |
| - case ResourceAttributes.CloudPlatformValues.AWS_EC2: |
113 |
| - return mapBase(resource, "aws_ec2_instance", AWS_EC2_INSTANCE_LABELS); |
114 |
| - default: |
115 |
| - return mapBase(resource, "generic_task", GENERIC_TASK_LABELS); |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - private static MonitoredResource mapBase( |
120 |
| - Resource resource, String mrType, List<AttributeMapping> mappings) { |
| 28 | + GcpResource gcpResource = |
| 29 | + com.google.cloud.opentelemetry.resource.ResourceTranslator.mapResource(resource); |
121 | 30 | MonitoredResource.Builder mr = MonitoredResource.newBuilder();
|
122 |
| - mr.setType(mrType); |
123 |
| - io.opentelemetry.api.common.AttributesBuilder unused = Attributes.builder(); |
124 |
| - for (AttributeMapping mapping : mappings) { |
125 |
| - mapping.fill(resource, mr); |
126 |
| - } |
| 31 | + mr.setType(gcpResource.getResourceType()); |
| 32 | + gcpResource.getResourceLabels().getLabels().forEach(mr::putLabels); |
127 | 33 | return mr.build();
|
128 | 34 | }
|
129 | 35 | }
|
0 commit comments