Skip to content

Commit

Permalink
Merge pull request #239 from dmarmugi/feature/addScheme
Browse files Browse the repository at this point in the history
Issue #231: Add __scheme__ configuration
  • Loading branch information
eaglerainbow committed Apr 16, 2023
2 parents e5b068c + c1ea893 commit 4fcf7e3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ Specifies the port number of the host (or the IP address) which shall be used fo

Setting this option is optional. If not specified, Promregator tries to auto-detect this value based on the configuration of the underlying operating system and the request, which triggered the service discovery.

#### Option "promregator.discovery.scheme" (optional)
Specifies the scheme (`http|https`) of the host (or the IP address) which shall be used for specifying the target during discovery. As a rule of thumb, this name should always be the scheme under which Prometheus is capable of reaching Promregator.

Setting this option is optional. If not specified, and `promregator.discovery.port` is set to `443`, Promregator sets this to `https`. This is output as `__scheme__` in the target labels.

#### Option "promregator.discovery.auth" (optional)
Specifies the way how authentication shall be verified, if a request reaches the endpoint. Valid values are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class DiscoveryEndpoint {
@Value("${promregator.discovery.hostname:#{null}}")
private String myHostname;

@Value("${promregator.discovery.scheme:#{null}}")
private String myScheme;

@Value("${promregator.discovery.port:0}")
private int myPort;

Expand All @@ -49,14 +52,16 @@ public static class DiscoveryLabel {
private String applicationId;
private String instanceNumber;
private String instanceId;
private String scheme;

public DiscoveryLabel(String path) {
public DiscoveryLabel(String path, String scheme) {
super();
this.targetPath = path;
this.scheme = scheme;
}

public DiscoveryLabel(String path, Instance instance) {
this(path);
public DiscoveryLabel(String path, String scheme, Instance instance) {
this(path, scheme);

this.orgName = instance.getTarget().getOrgName();
this.spaceName = instance.getTarget().getSpaceName();
Expand Down Expand Up @@ -105,6 +110,11 @@ public String getInstanceId() {
public String getMetricsPath() {
return this.targetPath;
}

@JsonGetter("__scheme__")
public String getScheme() {
return this.scheme;
}
}

public static class DiscoveryResponse {
Expand Down Expand Up @@ -138,6 +148,9 @@ public ResponseEntity<DiscoveryResponse[]> getDiscovery(HttpServletRequest reque

String localHostname = this.myHostname != null ? this.myHostname : request.getLocalName();
int localPort = this.myPort != 0 ? this.myPort : request.getLocalPort();

//Default __scheme__ to https if promregator.discovery.port:443
String localScheme = (this.myScheme == null && this.myPort == 443) ? "https" : this.myScheme;
final String[] targets = { String.format("%s:%d", localHostname, localPort) };

log.info("Using scraping target {} in discovery response", targets[0]);
Expand All @@ -146,15 +159,15 @@ public ResponseEntity<DiscoveryResponse[]> getDiscovery(HttpServletRequest reque
for (Instance instance : instances) {

String path = String.format(EndpointConstants.ENDPOINT_PATH_SINGLE_TARGET_SCRAPING+"/%s/%s", instance.getApplicationId(), instance.getInstanceNumber());
DiscoveryLabel dl = new DiscoveryLabel(path, instance);
DiscoveryLabel dl = new DiscoveryLabel(path, localScheme, instance);

DiscoveryResponse dr = new DiscoveryResponse(targets, dl);
result.add(dr);
}

if (this.promregatorMetricsEndpoint) {
// finally, also add our own metrics endpoint
DiscoveryLabel dl = new DiscoveryLabel(EndpointConstants.ENDPOINT_PATH_PROMREGATOR_METRICS);
DiscoveryLabel dl = new DiscoveryLabel(EndpointConstants.ENDPOINT_PATH_PROMREGATOR_METRICS, localScheme);
result.add(new DiscoveryResponse(targets, dl));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.ReflectionTestUtils;

import java.sql.Ref;
import java.util.Arrays;


@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -91,4 +95,52 @@ void testStraightForward() {

}

@Test
void testSchemePropertySetsSchemeInOutput(){
//given promregator.discovery.port:443 and promregator.discovery.scheme unset
ReflectionTestUtils.setField(subject, "myScheme", "https");

//when getDiscovery
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
ResponseEntity<DiscoveryResponse[]> responseE = this.subject.getDiscovery(requestMock);
DiscoveryResponse[] responses = responseE.getBody();

//then response labels contain __scheme__: https
Assertions.assertTrue(Arrays.stream(responses).allMatch(
response -> "https".equals(response.getLabels().getScheme())
));
}

@Test
void testPort443SetsHttpsIfSchemeNotSet(){
//given promregator.discovery.port:443 and promregator.discovery.scheme unset
ReflectionTestUtils.setField(subject, "myPort", 443);

//when getDiscovery
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
ResponseEntity<DiscoveryResponse[]> responseE = this.subject.getDiscovery(requestMock);
DiscoveryResponse[] responses = responseE.getBody();

//then response labels contain __scheme__: https
Assertions.assertTrue(Arrays.stream(responses).allMatch(
response -> "https".equals(response.getLabels().getScheme())
));
}

@Test
void testPort443DoesntSetHttpsIfSchemeIsExplicitlySetToHttp(){
//given promregator.discovery.port:443 and promregator.discovery.scheme unset
ReflectionTestUtils.setField(subject, "myPort", 443);
ReflectionTestUtils.setField(subject, "myScheme", "http");

//when getDiscovery
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
ResponseEntity<DiscoveryResponse[]> responseE = this.subject.getDiscovery(requestMock);
DiscoveryResponse[] responses = responseE.getBody();

//then response labels contain __scheme__: https
Assertions.assertTrue(Arrays.stream(responses).allMatch(
response -> "http".equals(response.getLabels().getScheme())
));
}
}

0 comments on commit 4fcf7e3

Please sign in to comment.