Skip to content

Commit

Permalink
Refactor platform detection to enhance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Jan 3, 2024
1 parent beb4d9a commit eefad12
Show file tree
Hide file tree
Showing 19 changed files with 577 additions and 436 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.google.cloud.opentelemetry.detectors;

public final class AttributeKeys {
// GCE Attributes
public static final String GCE_PROJECT_ID = AttributeKeys.PROJECT_ID;
public static final String GCE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
public static final String GCE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
public static final String GCE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
public static final String GCE_INSTANCE_NAME = AttributeKeys.INSTANCE_NAME;
public static final String GCE_MACHINE_TYPE = AttributeKeys.MACHINE_TYPE;

// GKE Attributes
public static final String GKE_POD_NAME = "gke_pod_name";
public static final String GKE_NAMESPACE = "gke_namespace";
public static final String GKE_CONTAINER_NAME = "gke_container_name";
public static final String GKE_CLUSTER_NAME = "gke_cluster_name";
public static final String GKE_CLUSTER_LOCATION_TYPE = "gke_cluster_location_type";
public static final String GKE_CLUSTER_LOCATION = "gke_cluster_location";
public static final String GKE_HOST_ID = AttributeKeys.INSTANCE_ID;

// GKE Location Constants
public static final String GKE_LOCATION_TYPE_ZONE = "ZONE";
public static final String GKE_LOCATION_TYPE_REGION = "REGION";

// GAE Attributes
public static final String GAE_MODULE_NAME = "gae_module_name";
public static final String GAE_APP_VERSION = "gae_app_version";
public static final String GAE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
public static final String GAE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
public static final String GAE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;

// Google Serverless Compute Attributes
public static final String SERVERLESS_COMPUTE_NAME = "serverless_compute_name";
public static final String SERVERLESS_COMPUTE_REVISION = "serverless_compute_revision";
public static final String SERVERLESS_COMPUTE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
public static final String SERVERLESS_COMPUTE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
public static final String SERVERLESS_COMPUTE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;

static final String PROJECT_ID = "project_id";
static final String AVAILABILITY_ZONE = "availability_zone";
static final String CLOUD_REGION = "cloud_region";
static final String INSTANCE_ID = "instance_id";
static final String INSTANCE_NAME = "instance_name";
static final String MACHINE_TYPE = "machine_type";
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 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.detectors;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.google.cloud.opentelemetry.detectors;

import java.util.Map;
import java.util.Optional;

public interface DetectedPlatform {
GCPPlatformDetector.SupportedPlatform getSupportedPlatform();

Map<String, Optional<String>> getAttributes();
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 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.detectors;

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 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.detectors;

import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
/*
* Copyright 2023 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.detectors;

import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

public final class GCPPlatformDetector {
public class GCPPlatformDetector {
public static final GCPPlatformDetector DEFAULT_INSTANCE = new GCPPlatformDetector();

private final GCPMetadataConfig metadataConfig;
Expand All @@ -37,49 +22,74 @@ private GCPPlatformDetector() {
}

// Detects the GCP platform on which the application is running
public GCPPlatform detectPlatform() {
public DetectedPlatform detectPlatform() {
if (!isRunningOnGcp()) {
return GCPPlatform.UNKNOWN_PLATFORM;
return generateDetectedPlatform(SupportedPlatform.UNKNOWN_PLATFORM);
}
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGKE =
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGKE =
environmentVariables ->
environmentVariables.get("KUBERNETES_SERVICE_HOST") != null
? Optional.of(GCPPlatform.GOOGLE_KUBERNETES_ENGINE)
? Optional.of(SupportedPlatform.GOOGLE_KUBERNETES_ENGINE)
: Optional.empty();
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGCR =
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGCR =
environmentVariables ->
environmentVariables.get("K_CONFIGURATION") != null
&& environmentVariables.get("FUNCTION_TARGET") == null
? Optional.of(GCPPlatform.GOOGLE_CLOUD_RUN)
? Optional.of(SupportedPlatform.GOOGLE_CLOUD_RUN)
: Optional.empty();
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGCF =
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGCF =
environmentVariables ->
environmentVariables.get("FUNCTION_TARGET") != null
? Optional.of(GCPPlatform.GOOGLE_CLOUD_FUNCTIONS)
? Optional.of(SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS)
: Optional.empty();
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGAE =
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGAE =
environmentVariables ->
environmentVariables.get("GAE_SERVICE") != null
? Optional.of(GCPPlatform.GOOGLE_APP_ENGINE)
? Optional.of(SupportedPlatform.GOOGLE_APP_ENGINE)
: Optional.empty();

// Order of detection functions matters here
Stream<Function<EnvironmentVariables, Optional<GCPPlatform>>> platforms =
Stream<Function<EnvironmentVariables, Optional<SupportedPlatform>>> platforms =
Stream.of(detectGKE, detectGCR, detectGCF, detectGAE);
return platforms
.map(detectionFn -> detectionFn.apply(environmentVariables))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElse(GCPPlatform.GOOGLE_COMPUTE_ENGINE); // defaults to GCE
SupportedPlatform platform =
platforms
.map(detectionFn -> detectionFn.apply(environmentVariables))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElse(SupportedPlatform.GOOGLE_COMPUTE_ENGINE); // defaults to GCE
return generateDetectedPlatform(platform);
}

private boolean isRunningOnGcp() {
return metadataConfig.getProjectId() != null && !metadataConfig.getProjectId().isEmpty();
}

/** Enum containing various Google Cloud Platforms that can be detected by the support library. */
public enum GCPPlatform {
private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) {
DetectedPlatform detectedPlatform;
switch (platform) {
case GOOGLE_KUBERNETES_ENGINE:
detectedPlatform = new GoogleKubernetesEngine();
break;
case GOOGLE_CLOUD_RUN:
detectedPlatform = new GoogleCloudRun();
break;
case GOOGLE_CLOUD_FUNCTIONS:
detectedPlatform = new GoogleCloudFunction();
break;
case GOOGLE_APP_ENGINE:
detectedPlatform = new GoogleAppEngine();
break;
case GOOGLE_COMPUTE_ENGINE:
detectedPlatform = new GoogleComputeEngine();
break;
default:
detectedPlatform = new UnknownPlatform();
}
return detectedPlatform;
}

public enum SupportedPlatform {
GOOGLE_COMPUTE_ENGINE,
GOOGLE_KUBERNETES_ENGINE,
GOOGLE_APP_ENGINE,
Expand Down
Loading

0 comments on commit eefad12

Please sign in to comment.