Skip to content

Commit 2185372

Browse files
committed
Add accidentally dropped proxy support to Wavefront
See spring-projectsgh-30156
1 parent 98d4024 commit 2185372

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontMetricsPropertiesConfigAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public String get(String k) {
5151

5252
@Override
5353
public String uri() {
54-
return this.properties.getUri().toString();
54+
return this.properties.getEffectiveUri().toString();
5555
}
5656

5757
@Override

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class WavefrontAutoConfiguration {
4747
@Bean
4848
@ConditionalOnMissingBean
4949
WavefrontSender wavefrontSender(WavefrontProperties properties) {
50-
Builder builder = new Builder(properties.getUri().toString(), properties.getApiTokenOrThrow());
50+
Builder builder = new Builder(properties.getEffectiveUri().toString(), properties.getApiTokenOrThrow());
5151
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
5252
WavefrontProperties.Sender sender = properties.getSender();
5353
mapper.from(sender.getMaxQueueSize()).to(builder::maxQueueSize);

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ public class WavefrontProperties {
5151

5252
private final Sender sender = new Sender();
5353

54-
public URI getUri() {
54+
/**
55+
* Returns the effective URI of the wavefront instance. This will not be the same URI
56+
* given through {@link #setUri(URI)} when a proxy is used.
57+
* @return the effective URI of the wavefront instance
58+
*/
59+
public URI getEffectiveUri() {
60+
if (usesProxy()) {
61+
// See io.micrometer.wavefront.WavefrontMeterRegistry.getWavefrontReportingUri
62+
return URI.create(this.uri.toString().replace("proxy://", "http://"));
63+
}
5564
return this.uri;
5665
}
5766

@@ -67,8 +76,13 @@ public void setApiToken(String apiToken) {
6776
this.apiToken = apiToken;
6877
}
6978

79+
/**
80+
* Returns the API token or throws an exception if the API token is mandatory. If a
81+
* proxy is used, the API token is optional.
82+
* @return the API token
83+
*/
7084
public String getApiTokenOrThrow() {
71-
if (this.apiToken == null) {
85+
if (this.apiToken == null && !usesProxy()) {
7286
throw new InvalidConfigurationPropertyValueException("management.wavefront.api-token", null,
7387
"This property is mandatory whenever publishing directly to the Wavefront API");
7488
}
@@ -95,6 +109,10 @@ private String getSourceDefault() {
95109
}
96110
}
97111

112+
private boolean usesProxy() {
113+
return "proxy".equals(this.uri.getScheme());
114+
}
115+
98116
public static class Sender {
99117

100118
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
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+
* https://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+
17+
package org.springframework.boot.actuate.autoconfigure.wavefront;
18+
19+
import java.net.URI;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
27+
28+
/**
29+
* Tests for {@link WavefrontProperties}.
30+
*
31+
* @author Moritz Halbritter
32+
*/
33+
class WavefrontPropertiesTests {
34+
35+
@Test
36+
void apiTokenIsOptionalWhenUsingProxy() {
37+
WavefrontProperties sut = new WavefrontProperties();
38+
sut.setUri(URI.create("proxy://localhost:2878"));
39+
sut.setApiToken(null);
40+
assertThat(sut.getApiTokenOrThrow()).isNull();
41+
assertThat(sut.getEffectiveUri()).isEqualTo(URI.create("http://localhost:2878"));
42+
}
43+
44+
@Test
45+
void apiTokenIsMandatoryWhenNotUsingProxy() {
46+
WavefrontProperties sut = new WavefrontProperties();
47+
sut.setUri(URI.create("http://localhost:2878"));
48+
sut.setApiToken(null);
49+
assertThatThrownBy(sut::getApiTokenOrThrow).isInstanceOf(InvalidConfigurationPropertyValueException.class)
50+
.hasMessageContaining("management.wavefront.api-token");
51+
}
52+
53+
}

0 commit comments

Comments
 (0)