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

Support cloud run jobs in resource detection #371

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public final class AttributeKeys {
public static final String SERVERLESS_COMPUTE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
public static final String SERVERLESS_COMPUTE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;

// Cloud Run Job Specific Attributes
public static final String GCR_JOB_EXECUTION_KEY = "gcr_job_execution_key";
public static final String GCR_JOB_TASK_INDEX = "gcr_job_task_index";

static final String AVAILABILITY_ZONE = "availability_zone";
static final String CLOUD_REGION = "cloud_region";
static final String INSTANCE_ID = "instance_id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private SupportedPlatform detectSupportedPlatform() {
return SupportedPlatform.GOOGLE_CLOUD_RUN;
} else if (environmentVariables.get("FUNCTION_TARGET") != null) {
return SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS;
} else if (environmentVariables.get("CLOUD_RUN_JOB") != null) {
return SupportedPlatform.GOOGLE_CLOUD_RUN_JOB;
} else if (environmentVariables.get("GAE_SERVICE") != null) {
return SupportedPlatform.GOOGLE_APP_ENGINE;
}
Expand All @@ -75,6 +77,9 @@ private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) {
case GOOGLE_CLOUD_FUNCTIONS:
detectedPlatform = new GoogleCloudFunction(environmentVariables, metadataConfig);
break;
case GOOGLE_CLOUD_RUN_JOB:
detectedPlatform = new GoogleCloudRunJob(environmentVariables, metadataConfig);
break;
case GOOGLE_APP_ENGINE:
detectedPlatform = new GoogleAppEngine(environmentVariables, metadataConfig);
break;
Expand All @@ -98,8 +103,10 @@ public enum SupportedPlatform {
GOOGLE_KUBERNETES_ENGINE,
/** Represents the Google App Engine platform. Could either be flex or standard. */
GOOGLE_APP_ENGINE,
/** Represents the Google Cloud Run platform. */
/** Represents the Google Cloud Run platform (Service). */
GOOGLE_CLOUD_RUN,
/** Represents the Google Cloud Run platform (Jobs). */
GOOGLE_CLOUD_RUN_JOB,
/** Represents the Google Cloud Functions platform. */
GOOGLE_CLOUD_FUNCTIONS,
/** Represents the case when the application is not running on GCP. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.opentelemetry.detection;

import java.util.HashMap;
import java.util.Map;

final class GoogleCloudRunJob implements DetectedPlatform {
private final GCPMetadataConfig metadataConfig;
private final EnvironmentVariables environmentVariables;
private final Map<String, String> availableAttributes;

GoogleCloudRunJob(EnvironmentVariables environmentVariables, GCPMetadataConfig metadataConfig) {
this.metadataConfig = metadataConfig;
this.environmentVariables = environmentVariables;
this.availableAttributes = prepareAttributes();
}

private Map<String, String> prepareAttributes() {
Map<String, String> map = new HashMap<>();
map.put(AttributeKeys.SERVERLESS_COMPUTE_NAME, this.environmentVariables.get("CLOUD_RUN_JOB"));
map.put(
AttributeKeys.GCR_JOB_EXECUTION_KEY, this.environmentVariables.get("CLOUD_RUN_EXECUTION"));
map.put(
AttributeKeys.GCR_JOB_TASK_INDEX, this.environmentVariables.get("CLOUD_RUN_TASK_INDEX"));
map.put(AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID, this.metadataConfig.getInstanceId());
map.put(AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION, this.metadataConfig.getRegionFromZone());
return map;
}

@Override
public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
return GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB;
}

@Override
public String getProjectId() {
return metadataConfig.getProjectId();
}

@Override
public Map<String, String> getAttributes() {
return this.availableAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ public void testGCFDetectionWhenGCRAttributesPresent() {
detector.detectPlatform().getAttributes());
}

/** Google Cloud Run Tests * */
/** Google Cloud Run Tests (Service) * */
@Test
public void testGCFResourceWithCloudRunAttributesSucceeds() {
// Setup GCR required env vars
// Setup GCR service required env vars
envVars.put("K_SERVICE", "cloud-run-hello");
envVars.put("K_REVISION", "cloud-run-hello.1");
envVars.put("K_CONFIGURATION", "cloud-run-hello");
Expand Down Expand Up @@ -306,6 +306,37 @@ public void testGCFResourceWithCloudRunAttributesSucceeds() {
assertEquals("GCR-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
}

/** Google Cloud Run Tests (Jobs) * */
@Test
public void testCloudRunJobResourceWithAttributesSucceeds() {
// Setup GCR Job required env vars
envVars.put("CLOUD_RUN_JOB", "cloud-run-hello-job");
envVars.put("CLOUD_RUN_EXECUTION", "cloud-run-hello-job-1a2b3c");
envVars.put("CLOUD_RUN_TASK_INDEX", "0");

TestUtils.stubEndpoint("/project/project-id", "GCR-pid");
TestUtils.stubEndpoint("/instance/zone", "country-region-zone");
TestUtils.stubEndpoint("/instance/id", "GCR-job-instance-id");

EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleCloudRunJob(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GCR-pid", detector.detectPlatform().getProjectId());
assertEquals(5, detectedAttributes.size());

assertEquals("cloud-run-hello-job-1a2b3c", detectedAttributes.get(GCR_JOB_EXECUTION_KEY));
assertEquals("0", detectedAttributes.get(GCR_JOB_TASK_INDEX));
assertEquals("cloud-run-hello-job", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
assertEquals("country-region", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
assertEquals("GCR-job-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
}

/** Google App Engine Tests * */
@ParameterizedTest
@MethodSource("provideGAEVariantEnvironmentVariable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.time.Duration;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MetricsExporterExample {
private static SdkMeterProvider METER_PROVIDER;
Expand Down Expand Up @@ -146,9 +148,16 @@ public static void main(String[] args) throws InterruptedException, IOException
} finally {
System.out.println("Shutting down the metrics-example application");

METER_PROVIDER.shutdown();
CompletableResultCode resultCode = METER_PROVIDER.shutdown();
// Wait upto 60 seconds for job to complete
resultCode.join(60, TimeUnit.SECONDS);
if (resultCode.isSuccess()) {
System.out.println("Shutdown completed successfully!");
} else {
System.out.println("Unable to shutdown gracefully!");
}

System.out.println("Shutdown complete");
System.out.println("Exiting job");
}
}
}
Loading