Skip to content

Commit 7ad60e9

Browse files
committed
Refactor platform detection to enhance testing
1 parent beb4d9a commit 7ad60e9

File tree

16 files changed

+697
-376
lines changed

16 files changed

+697
-376
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.detectors;
17+
18+
public final class AttributeKeys {
19+
// GCE Attributes
20+
public static final String GCE_PROJECT_ID = AttributeKeys.PROJECT_ID;
21+
public static final String GCE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
22+
public static final String GCE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
23+
public static final String GCE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
24+
public static final String GCE_INSTANCE_NAME = AttributeKeys.INSTANCE_NAME;
25+
public static final String GCE_MACHINE_TYPE = AttributeKeys.MACHINE_TYPE;
26+
27+
// GKE Attributes
28+
public static final String GKE_POD_NAME = "gke_pod_name";
29+
public static final String GKE_NAMESPACE = "gke_namespace";
30+
public static final String GKE_CONTAINER_NAME = "gke_container_name";
31+
public static final String GKE_CLUSTER_NAME = "gke_cluster_name";
32+
public static final String GKE_CLUSTER_LOCATION_TYPE = "gke_cluster_location_type";
33+
public static final String GKE_CLUSTER_LOCATION = "gke_cluster_location";
34+
public static final String GKE_HOST_ID = AttributeKeys.INSTANCE_ID;
35+
36+
// GKE Location Constants
37+
public static final String GKE_LOCATION_TYPE_ZONE = "ZONE";
38+
public static final String GKE_LOCATION_TYPE_REGION = "REGION";
39+
40+
// GAE Attributes
41+
public static final String GAE_MODULE_NAME = "gae_module_name";
42+
public static final String GAE_APP_VERSION = "gae_app_version";
43+
public static final String GAE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
44+
public static final String GAE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
45+
public static final String GAE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
46+
47+
// Google Serverless Compute Attributes
48+
public static final String SERVERLESS_COMPUTE_NAME = "serverless_compute_name";
49+
public static final String SERVERLESS_COMPUTE_REVISION = "serverless_compute_revision";
50+
public static final String SERVERLESS_COMPUTE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
51+
public static final String SERVERLESS_COMPUTE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
52+
public static final String SERVERLESS_COMPUTE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
53+
54+
static final String PROJECT_ID = "project_id";
55+
static final String AVAILABILITY_ZONE = "availability_zone";
56+
static final String CLOUD_REGION = "cloud_region";
57+
static final String INSTANCE_ID = "instance_id";
58+
static final String INSTANCE_NAME = "instance_name";
59+
static final String MACHINE_TYPE = "machine_type";
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.detectors;
17+
18+
import java.util.Map;
19+
import java.util.Optional;
20+
21+
public interface DetectedPlatform {
22+
GCPPlatformDetector.SupportedPlatform getSupportedPlatform();
23+
24+
Map<String, Optional<String>> getAttributes();
25+
}

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GAEDetector.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCEDetector.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPPlatformDetector.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.function.Function;
2020
import java.util.stream.Stream;
2121

22-
public final class GCPPlatformDetector {
22+
public class GCPPlatformDetector {
2323
public static final GCPPlatformDetector DEFAULT_INSTANCE = new GCPPlatformDetector();
2424

2525
private final GCPMetadataConfig metadataConfig;
@@ -37,49 +37,74 @@ private GCPPlatformDetector() {
3737
}
3838

3939
// Detects the GCP platform on which the application is running
40-
public GCPPlatform detectPlatform() {
40+
public DetectedPlatform detectPlatform() {
4141
if (!isRunningOnGcp()) {
42-
return GCPPlatform.UNKNOWN_PLATFORM;
42+
return generateDetectedPlatform(SupportedPlatform.UNKNOWN_PLATFORM);
4343
}
44-
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGKE =
44+
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGKE =
4545
environmentVariables ->
4646
environmentVariables.get("KUBERNETES_SERVICE_HOST") != null
47-
? Optional.of(GCPPlatform.GOOGLE_KUBERNETES_ENGINE)
47+
? Optional.of(SupportedPlatform.GOOGLE_KUBERNETES_ENGINE)
4848
: Optional.empty();
49-
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGCR =
49+
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGCR =
5050
environmentVariables ->
5151
environmentVariables.get("K_CONFIGURATION") != null
5252
&& environmentVariables.get("FUNCTION_TARGET") == null
53-
? Optional.of(GCPPlatform.GOOGLE_CLOUD_RUN)
53+
? Optional.of(SupportedPlatform.GOOGLE_CLOUD_RUN)
5454
: Optional.empty();
55-
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGCF =
55+
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGCF =
5656
environmentVariables ->
5757
environmentVariables.get("FUNCTION_TARGET") != null
58-
? Optional.of(GCPPlatform.GOOGLE_CLOUD_FUNCTIONS)
58+
? Optional.of(SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS)
5959
: Optional.empty();
60-
Function<EnvironmentVariables, Optional<GCPPlatform>> detectGAE =
60+
Function<EnvironmentVariables, Optional<SupportedPlatform>> detectGAE =
6161
environmentVariables ->
6262
environmentVariables.get("GAE_SERVICE") != null
63-
? Optional.of(GCPPlatform.GOOGLE_APP_ENGINE)
63+
? Optional.of(SupportedPlatform.GOOGLE_APP_ENGINE)
6464
: Optional.empty();
6565

6666
// Order of detection functions matters here
67-
Stream<Function<EnvironmentVariables, Optional<GCPPlatform>>> platforms =
67+
Stream<Function<EnvironmentVariables, Optional<SupportedPlatform>>> platforms =
6868
Stream.of(detectGKE, detectGCR, detectGCF, detectGAE);
69-
return platforms
70-
.map(detectionFn -> detectionFn.apply(environmentVariables))
71-
.filter(Optional::isPresent)
72-
.map(Optional::get)
73-
.findFirst()
74-
.orElse(GCPPlatform.GOOGLE_COMPUTE_ENGINE); // defaults to GCE
69+
SupportedPlatform platform =
70+
platforms
71+
.map(detectionFn -> detectionFn.apply(environmentVariables))
72+
.filter(Optional::isPresent)
73+
.map(Optional::get)
74+
.findFirst()
75+
.orElse(SupportedPlatform.GOOGLE_COMPUTE_ENGINE); // defaults to GCE
76+
return generateDetectedPlatform(platform);
7577
}
7678

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

81-
/** Enum containing various Google Cloud Platforms that can be detected by the support library. */
82-
public enum GCPPlatform {
83+
private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) {
84+
DetectedPlatform detectedPlatform;
85+
switch (platform) {
86+
case GOOGLE_KUBERNETES_ENGINE:
87+
detectedPlatform = new GoogleKubernetesEngine();
88+
break;
89+
case GOOGLE_CLOUD_RUN:
90+
detectedPlatform = new GoogleCloudRun();
91+
break;
92+
case GOOGLE_CLOUD_FUNCTIONS:
93+
detectedPlatform = new GoogleCloudFunction();
94+
break;
95+
case GOOGLE_APP_ENGINE:
96+
detectedPlatform = new GoogleAppEngine();
97+
break;
98+
case GOOGLE_COMPUTE_ENGINE:
99+
detectedPlatform = new GoogleComputeEngine();
100+
break;
101+
default:
102+
detectedPlatform = new UnknownPlatform();
103+
}
104+
return detectedPlatform;
105+
}
106+
107+
public enum SupportedPlatform {
83108
GOOGLE_COMPUTE_ENGINE,
84109
GOOGLE_KUBERNETES_ENGINE,
85110
GOOGLE_APP_ENGINE,

0 commit comments

Comments
 (0)