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: support for Cloud Run monitored resource #78

Merged
merged 3 commits into from Jan 23, 2020
Merged
Changes from all commits
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 @@ -40,10 +40,13 @@ private enum Label {
ContainerName("container_name"),
InstanceId("instance_id"),
InstanceName("instance_name"),
Location("location"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use "location" instead of "region"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at log entries that get ingested from stdout, and they all have the location attribute. So, I tried to match that.

ModuleId("module_id"),
NamespaceId("namespace_id"),
PodId("pod_id"),
ProjectId("project_id"),
RevisionName("revision_name"),
ServiceName("service_name"),
VersionId("version_id"),
Zone("zone");

Expand All @@ -59,6 +62,7 @@ String getKey() {
}

private enum Resource {
CloudRun("cloud_run_revision"),
Container("container"),
GaeAppFlex("gae_app_flex"),
GaeAppStandard("gae_app_standard"),
Expand All @@ -80,8 +84,6 @@ String getKey() {

private static ImmutableMultimap<String, Label> resourceTypeWithLabels =
ImmutableMultimap.<String, Label>builder()
.putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone)
.putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId)
.putAll(
Resource.Container.getKey(),
Label.ClusterName,
Expand All @@ -90,6 +92,9 @@ String getKey() {
Label.NamespaceId,
Label.PodId,
Label.Zone)
.putAll(Resource.CloudRun.getKey(), Label.RevisionName, Label.ServiceName, Label.Location)
.putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone)
.putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId)
.putAll(Resource.GceInstance.getKey(), Label.InstanceId, Label.Zone)
.build();

Expand Down Expand Up @@ -147,6 +152,9 @@ private static String getValue(Label label) {
case InstanceName:
value = getAppEngineInstanceName();
break;
case Location:
value = getCloudRunLocation();
break;
case ModuleId:
value = getAppEngineModuleId();
break;
Expand All @@ -156,6 +164,12 @@ private static String getValue(Label label) {
case PodId:
value = System.getenv("HOSTNAME");
break;
case RevisionName:
value = System.getenv("K_REVISION");
break;
case ServiceName:
value = System.getenv("K_SERVICE");
break;
case VersionId:
value = getAppEngineVersionId();
break;
Expand All @@ -171,6 +185,12 @@ private static String getValue(Label label) {

/* Detect monitored Resource type using environment variables, else return global as default. */
private static Resource getAutoDetectedResourceType() {
if (System.getenv("K_SERVICE") != null
&& System.getenv("K_REVISION") != null
&& System.getenv("K_CONFIGURATION") != null
&& System.getenv("KUBERNETES_SERVICE_HOST") == null) {
return Resource.CloudRun;
}
if (System.getenv("GAE_INSTANCE") != null) {
return Resource.GaeAppFlex;
}
Expand Down Expand Up @@ -199,6 +219,14 @@ private static String getAppEngineInstanceName() {
return System.getenv("GAE_INSTANCE");
}

private static String getCloudRunLocation() {
String zone = MetadataConfig.getZone();
// for Cloud Run managed, the zone is "REGION-1"
// So, we need to strip the "-1" to set location to just the region
if (zone.endsWith("-1")) return zone.substring(0, zone.length() - 2);
else return zone;
}

private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
List<LoggingEnhancer> enhancers = new ArrayList<>(2);
switch (resourceType) {
Expand Down