Skip to content

Commit

Permalink
fix: support for Cloud Run monitored resource (#78)
Browse files Browse the repository at this point in the history
* fix: support for Cloud Run monitored resource

Fixes: #71.

* add check for not k8s

* format
  • Loading branch information
meltsufin authored and chingor13 committed Jan 23, 2020
1 parent 67ce459 commit b3c1b68
Showing 1 changed file with 30 additions and 2 deletions.
Expand Up @@ -40,10 +40,13 @@ private enum Label {
ContainerName("container_name"),
InstanceId("instance_id"),
InstanceName("instance_name"),
Location("location"),
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

0 comments on commit b3c1b68

Please sign in to comment.