Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix export to log detect resource errors #2197

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -151,25 +151,33 @@ static List<TimeSeries> convertToApplicationResourceTimeSeries(
static MonitoredResource detectResource() {
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
DetectedPlatform detectedPlatform = detector.detectPlatform();
switch (detectedPlatform.getSupportedPlatform()) {
case GOOGLE_COMPUTE_ENGINE:
return createGceMonitoredResource(
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
case GOOGLE_KUBERNETES_ENGINE:
return createGkeMonitoredResource(
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
default:
return null;
MonitoredResource monitoredResource = null;
try {
switch (detectedPlatform.getSupportedPlatform()) {
case GOOGLE_COMPUTE_ENGINE:
monitoredResource =
createGceMonitoredResource(
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
break;
case GOOGLE_KUBERNETES_ENGINE:
monitoredResource =
createGkeMonitoredResource(
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
break;
}
} catch (IllegalStateException e) {
logger.log(Level.WARNING, "Failed to create monitored resource for GCE / GKE.", e);
}
return monitoredResource;
}

private static MonitoredResource createGceMonitoredResource(
String projectId, Map<String, String> attributes) {
return MonitoredResource.newBuilder()
.setType("gce_instance")
.putLabels("project_id", projectId)
.putLabels("instance_id", attributes.get(AttributeKeys.GCE_INSTANCE_ID))
.putLabels("zone", attributes.get(AttributeKeys.GCE_AVAILABILITY_ZONE))
.putLabels("instance_id", getAttribute(attributes, AttributeKeys.GCE_INSTANCE_ID))
.putLabels("zone", getAttribute(attributes, AttributeKeys.GCE_AVAILABILITY_ZONE))
.build();
}

Expand All @@ -178,14 +186,23 @@ private static MonitoredResource createGkeMonitoredResource(
return MonitoredResource.newBuilder()
.setType("k8s_container")
.putLabels("project_id", projectId)
.putLabels("location", attributes.get(AttributeKeys.GKE_CLUSTER_LOCATION))
.putLabels("cluster_name", attributes.get(AttributeKeys.GKE_CLUSTER_NAME))
.putLabels("location", getAttribute(attributes, AttributeKeys.GKE_CLUSTER_LOCATION))
.putLabels("cluster_name", getAttribute(attributes, AttributeKeys.GKE_CLUSTER_NAME))
.putLabels("namespace_name", MoreObjects.firstNonNull(System.getenv("NAMESPACE"), ""))
.putLabels("pod_name", MoreObjects.firstNonNull(System.getenv("HOSTNAME"), ""))
.putLabels("container_name", MoreObjects.firstNonNull(System.getenv("CONTAINER_NAME"), ""))
.build();
}

private static String getAttribute(Map<String, String> attributes, String key) {
String value = attributes.get(key);
if (value == null) {
throw new IllegalStateException(
"Required attribute " + key + " does not exist in the attributes map " + attributes);
}
return value;
}

private static TimeSeries convertPointToBigtableTimeSeries(
MetricData metricData, PointData pointData, String taskId) {
TimeSeries.Builder builder =
Expand Down