-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Enable container and Kubernetes awareness for improved telemetry. #1235
Changes from 3 commits
fd08172
d5e7441
735541f
19923a0
b5221cc
6158afb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,9 @@ | |
import org.bson.codecs.EncoderContext; | ||
import org.bson.io.BasicOutputBuffer; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
@@ -38,6 +40,7 @@ | |
import static com.mongodb.assertions.Assertions.isTrueArgument; | ||
import static java.lang.String.format; | ||
import static java.lang.System.getProperty; | ||
import static java.nio.file.Paths.get; | ||
|
||
/** | ||
* <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
|
@@ -98,17 +101,26 @@ public static BsonDocument createClientMetadataDocument(@Nullable final String a | |
putAtPath(d, "driver.name", listToString(fullDriverInfo.getDriverNames())); | ||
putAtPath(d, "driver.version", listToString(fullDriverInfo.getDriverVersions())); | ||
}); | ||
|
||
// optional fields: | ||
Environment environment = getEnvironment(); | ||
FaasEnvironment faasEnvironment = getFaasEnvironment(); | ||
ContainerRuntime containerRuntime = ContainerRuntime.determineExecutionContainer(); | ||
Orchestrator orchestrator = Orchestrator.determineExecutionOrchestrator(); | ||
|
||
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(baseDriverInfor.getDriverPlatforms()))); | ||
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(fullDriverInfo.getDriverPlatforms()))); | ||
tryWithLimit(client, d -> putAtPath(d, "env.name", environment.getName())); | ||
tryWithLimit(client, d -> putAtPath(d, "os.name", getOperatingSystemName())); | ||
tryWithLimit(client, d -> putAtPath(d, "os.architecture", getProperty("os.arch", "unknown"))); | ||
tryWithLimit(client, d -> putAtPath(d, "os.version", getProperty("os.version", "unknown"))); | ||
tryWithLimit(client, d -> putAtPath(d, "env.timeout_sec", environment.getTimeoutSec())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.memory_mb", environment.getMemoryMb())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.region", environment.getRegion())); | ||
|
||
tryWithLimit(client, d -> putAtPath(d, "env.name", faasEnvironment.getName())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.timeout_sec", faasEnvironment.getTimeoutSec())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.memory_mb", faasEnvironment.getMemoryMb())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.region", faasEnvironment.getRegion())); | ||
|
||
tryWithLimit(client, d -> putAtPath(d, "env.container.runtime", containerRuntime.getName())); | ||
tryWithLimit(client, d -> putAtPath(d, "env.container.orchestrator", orchestrator.getName())); | ||
|
||
return client; | ||
} | ||
|
||
|
@@ -168,8 +180,7 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) { | |
new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build()); | ||
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE; | ||
} | ||
|
||
private enum Environment { | ||
private enum FaasEnvironment { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the introduction of new parameters in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
AWS_LAMBDA("aws.lambda"), | ||
AZURE_FUNC("azure.func"), | ||
GCP_FUNC("gcp.func"), | ||
|
@@ -179,7 +190,7 @@ private enum Environment { | |
@Nullable | ||
private final String name; | ||
|
||
Environment(@Nullable final String name) { | ||
FaasEnvironment(@Nullable final String name) { | ||
this.name = name; | ||
} | ||
|
||
|
@@ -225,6 +236,80 @@ public String getRegion() { | |
} | ||
} | ||
|
||
public enum ContainerRuntime { | ||
DOCKER("docker") { | ||
@Override | ||
boolean isCurrentRuntimeContainer() { | ||
try { | ||
return Files.exists(get(File.separator + ".dockerenv")); | ||
} catch (Exception e) { | ||
return false; | ||
// NOOP. This could be a SecurityException. | ||
} | ||
} | ||
}, | ||
UNKNOWN(null); | ||
@Nullable | ||
private final String name; | ||
|
||
ContainerRuntime(@Nullable final String name) { | ||
this.name = name; | ||
} | ||
|
||
@Nullable | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
boolean isCurrentRuntimeContainer() { | ||
return false; | ||
} | ||
|
||
static ContainerRuntime determineExecutionContainer() { | ||
for (ContainerRuntime allegedContainer : ContainerRuntime.values()) { | ||
if (allegedContainer.isCurrentRuntimeContainer()) { | ||
return allegedContainer; | ||
} | ||
} | ||
return UNKNOWN; | ||
} | ||
} | ||
|
||
private enum Orchestrator { | ||
K8S("kubernetes") { | ||
@Override | ||
boolean isCurrentOrchestrator() { | ||
return System.getenv("KUBERNETES_SERVICE_HOST") != null; | ||
} | ||
}, | ||
UNKNOWN(null); | ||
|
||
@Nullable | ||
private final String name; | ||
|
||
Orchestrator(@Nullable final String name) { | ||
this.name = name; | ||
} | ||
|
||
@Nullable | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
boolean isCurrentOrchestrator() { | ||
return false; | ||
} | ||
|
||
static Orchestrator determineExecutionOrchestrator() { | ||
for (Orchestrator alledgedOrchestrator : Orchestrator.values()) { | ||
if (alledgedOrchestrator.isCurrentOrchestrator()) { | ||
return alledgedOrchestrator; | ||
} | ||
} | ||
return UNKNOWN; | ||
} | ||
} | ||
|
||
@Nullable | ||
private static Integer getEnvInteger(final String name) { | ||
try { | ||
|
@@ -235,29 +320,29 @@ private static Integer getEnvInteger(final String name) { | |
} | ||
} | ||
|
||
static Environment getEnvironment() { | ||
List<Environment> result = new ArrayList<>(); | ||
static FaasEnvironment getFaasEnvironment() { | ||
List<FaasEnvironment> result = new ArrayList<>(); | ||
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV"); | ||
|
||
if (System.getenv("VERCEL") != null) { | ||
result.add(Environment.VERCEL); | ||
result.add(FaasEnvironment.VERCEL); | ||
} | ||
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_")) | ||
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) { | ||
result.add(Environment.AWS_LAMBDA); | ||
result.add(FaasEnvironment.AWS_LAMBDA); | ||
} | ||
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) { | ||
result.add(Environment.AZURE_FUNC); | ||
result.add(FaasEnvironment.AZURE_FUNC); | ||
} | ||
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) { | ||
result.add(Environment.GCP_FUNC); | ||
result.add(FaasEnvironment.GCP_FUNC); | ||
} | ||
// vercel takes precedence over aws.lambda | ||
if (result.equals(Arrays.asList(Environment.VERCEL, Environment.AWS_LAMBDA))) { | ||
return Environment.VERCEL; | ||
if (result.equals(Arrays.asList(FaasEnvironment.VERCEL, FaasEnvironment.AWS_LAMBDA))) { | ||
return FaasEnvironment.VERCEL; | ||
} | ||
if (result.size() != 1) { | ||
return Environment.UNKNOWN; | ||
return FaasEnvironment.UNKNOWN; | ||
} | ||
return result.get(0); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This useful addon to Mockito allows for the mocking of static methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice