From 4e3958e5aa814b18a9d1524dadfc584b72d2095c Mon Sep 17 00:00:00 2001 From: Mikolaj Stefaniak Date: Fri, 18 Jun 2021 15:25:38 +0200 Subject: [PATCH 1/2] Add resource labels to Stackdriver metrics configuration properties Using resource labels is mandatory for most Stackdriver resources other than 'Global', i.e. k8s_pod. Configuring valid resource type along with related labels makes it possible to use given metric in a wider set of GCP solutions, i.e. custom metric based GKE pod horizontal autoscaler. See gh-26961 --- .../export/stackdriver/StackdriverProperties.java | 15 +++++++++++++++ .../StackdriverPropertiesConfigAdapter.java | 7 +++++++ .../StackdriverPropertiesConfigAdapterTests.java | 14 ++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java index 07c6668244..aacb9998bd 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver; +import java.util.Map; + import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -40,6 +42,11 @@ public class StackdriverProperties extends StepRegistryProperties { */ private String resourceType = "global"; + /** + * Monitored resource's labels. + */ + private Map resourceLabels; + public String getProjectId() { return this.projectId; } @@ -56,4 +63,12 @@ public class StackdriverProperties extends StepRegistryProperties { this.resourceType = resourceType; } + public Map getResourceLabels() { + return this.resourceLabels; + } + + public void setResourceLabels(Map resourceLabels) { + this.resourceLabels = resourceLabels; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java index 98fa207783..e1c95efd1f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver; +import java.util.Map; + import io.micrometer.stackdriver.StackdriverConfig; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter; @@ -48,4 +50,9 @@ public class StackdriverPropertiesConfigAdapter extends StepRegistryPropertiesCo return get(StackdriverProperties::getResourceType, StackdriverConfig.super::resourceType); } + @Override + public Map resourceLabels() { + return get(StackdriverProperties::getResourceLabels, StackdriverConfig.super::resourceLabels); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java index 74b282f645..138dc38e20 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver; +import java.util.HashMap; +import java.util.Map; + import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -41,4 +44,15 @@ class StackdriverPropertiesConfigAdapterTests { assertThat(new StackdriverPropertiesConfigAdapter(properties).resourceType()).isEqualTo("my-resource-type"); } + @Test + void whenPropertiesResourceLabelsAreSetAdapterResourceTypeReturnsThem() { + final Map labels = new HashMap<>(); + labels.put("labelOne", "valueOne"); + labels.put("labelTwo", "valueTwo"); + StackdriverProperties properties = new StackdriverProperties(); + properties.setResourceLabels(labels); + assertThat(new StackdriverPropertiesConfigAdapter(properties).resourceLabels()) + .containsExactlyInAnyOrderEntriesOf(labels); + } + } From 75fdd89be4e2d3e18dbe77265f84842404b92a77 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Jul 2021 15:02:42 +0100 Subject: [PATCH 2/2] Polish "Add resource labels to Stackdriver metrics configuration properties" See gh-26961 --- .../metrics/export/stackdriver/StackdriverProperties.java | 2 +- .../stackdriver/StackdriverPropertiesConfigAdapter.java | 2 +- .../stackdriver/StackdriverPropertiesConfigAdapterTests.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java index aacb9998bd..1ff7f48d77 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java index e1c95efd1f..7011dd1f1f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java index 138dc38e20..7c7980f05c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/stackdriver/StackdriverPropertiesConfigAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ class StackdriverPropertiesConfigAdapterTests { } @Test - void whenPropertiesResourceLabelsAreSetAdapterResourceTypeReturnsThem() { + void whenPropertiesResourceLabelsAreSetAdapterResourceLabelsReturnsThem() { final Map labels = new HashMap<>(); labels.put("labelOne", "valueOne"); labels.put("labelTwo", "valueTwo");