Skip to content

fix(tests): Re-enable Metrics integration test by renaming #118

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

Merged
merged 9 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.step.StepMeterRegistry;
import io.micrometer.stackdriver.StackdriverConfig;
import io.micrometer.stackdriver.StackdriverMeterRegistry;

import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport;
import org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver.StackdriverMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver.StackdriverProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand All @@ -47,8 +48,9 @@
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(StackdriverMetricsExportAutoConfiguration.class)
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnClass({StepMeterRegistry.class, StackdriverConfig.class})
@ConditionalOnClass(StackdriverMeterRegistry.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnEnabledMetricsExport("stackdriver")
@EnableConfigurationProperties({GcpMetricsProperties.class, StackdriverProperties.class})
@ConditionalOnProperty(value = "spring.cloud.gcp.metrics.enabled", matchIfMissing = true, havingValue = "true")
public class GcpStackdriverMetricsAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.google.pubsub.v1.Topic;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.jetbrains.annotations.NotNull;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -298,7 +297,7 @@ public PubSubMessageHandler pubSubMessageHandler(PubSubTemplate pubSubTemplate)
// tag::adapter_callback[]
adapter.setPublishCallback(new ListenableFutureCallback<String>() {
@Override
public void onFailure(@NotNull Throwable ex) {
public void onFailure(Throwable ex) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ Run the sample application from Maven:
$ mvn spring-boot:run
----

Then, issue a request to `localhost:8080/` endpoint and verify that a label appears in Stackdriver:
Then, issue a request to `localhost:8080/{custom metric name}` endpoint and verify that the metric appears in Stackdriver:

----
$ curl localhost:8080/
$ curl localhost:8080/my_ping_counter
----

Metrics are reported to Stackdriver every 60 seconds.
So, wait a minute for the custom metric to be reported.
To see the metrics, navigate to the https://console.cloud.google.com/monitoring/metrics-explorer[Monitoring section of the Cloud Console].
Select the `custom/ping` metric to see the tracked metric.
Select the `custom/my_ping_counter` metric to see the tracked metric.

=== Running on Google Cloud Platform

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -31,9 +31,9 @@ public ExampleController(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

@PostMapping("/{id}")
@GetMapping("/{id}")
public String counter(@PathVariable String id) {
this.meterRegistry.counter(id).increment();
return "Ping return successfully";
return "Incremented " + id + " counter successfully";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
management.endpoints.web.exposure.include=*
# (optional) enable access to http://localhost:8080/actuator/metrics
management.endpoints.web.exposure.include=metrics
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
Expand All @@ -47,7 +48,8 @@
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MetricsApplication.class)
public class MetricsSampleApplicationTests {
@AutoConfigureMetrics // needed to enable metrics export in Spring Boot tests
public class MetricsSampleApplicationIntegrationTests {

@Autowired
private GcpProjectIdProvider projectIdProvider;
Expand Down Expand Up @@ -80,7 +82,7 @@ public void testMetricRecordedInStackdriver() {
String id = "integration_test_" + UUID.randomUUID().toString().replace('-', '_');
String url = String.format("http://localhost:%s/%s", this.port, id);

ResponseEntity<String> responseEntity = this.testRestTemplate.postForEntity(url, null, String.class);
ResponseEntity<String> responseEntity = this.testRestTemplate.getForEntity(url, String.class, String.class);
assertThat(responseEntity.getStatusCode().is2xxSuccessful()).isTrue();

String metricType = "custom.googleapis.com/" + id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spring.vision;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -96,7 +97,7 @@ private static List<TextAnnotation> parseJsonBlob(Blob blob)
throws InvalidProtocolBufferException {

AnnotateFileResponse.Builder annotateFileResponseBuilder = AnnotateFileResponse.newBuilder();
String jsonContent = new String(blob.getContent());
String jsonContent = new String(blob.getContent(), StandardCharsets.UTF_8);
JsonFormat.parser().ignoringUnknownFields().merge(jsonContent, annotateFileResponseBuilder);

AnnotateFileResponse annotateFileResponse = annotateFileResponseBuilder.build();
Expand Down