Skip to content

Commit

Permalink
Add Retrofit sample (#46)
Browse files Browse the repository at this point in the history
* Created retrofit sample

* Removed target tags

* Improve tests (check logs, metrics and spans)

* retrofit -> micrometer-samples-boot3-retrofit

* format

* Add license

---------

Co-authored-by: Jonatan Ivanov <[email protected]>
  • Loading branch information
nathandeamer and jonatan-ivanov authored Oct 19, 2023
1 parent 88fa932 commit ae9347a
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 0 deletions.
44 changes: 44 additions & 0 deletions micrometer-samples-boot3-retrofit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
buildscript {
repositories {
gradlePluginPortal()
if (springBootVersion.endsWith('SNAPSHOT') || springCloudVersion.endsWith('SNAPSHOT')) {
maven { url 'https://repo.spring.io/snapshot' }
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}

apply plugin: 'org.springframework.boot'

dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'com.squareup.retrofit2:retrofit:latest.release'
implementation 'com.squareup.retrofit2:converter-scalars:latest.release'

// Default is Brave + Zipkin, you can opt in via "-Pwavefront" to Wavefront and "-Potel" for OTel
if (project.hasProperty('wavefront')) {
implementation 'io.micrometer:micrometer-tracing-reporter-wavefront'
}
if (project.hasProperty('otel')) {
implementation 'io.micrometer:micrometer-tracing-bridge-otel'
implementation 'io.opentelemetry:opentelemetry-exporter-zipkin'
}
else {
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
}

testImplementation platform('org.testcontainers:testcontainers-bom:1.+')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone:latest.release'
testImplementation 'io.micrometer:micrometer-test'
testImplementation 'io.rest-assured:rest-assured'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.awaitility:awaitility'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.micrometer;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface GreetingClient {

@GET("/greeting/{name}")
Call<String> getGreeting(@Path("name") String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.micrometer;

import io.micrometer.core.instrument.binder.okhttp3.OkHttpObservationInterceptor;
import io.micrometer.observation.ObservationRegistry;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

@SpringBootApplication
public class RetrofitApplication {

public static void main(String... args) {
SpringApplication.run(RetrofitApplication.class, args);
}

@Bean
GreetingClient greetingClient(ObservationRegistry observationRegistry,
@Value("${greeting.endpoint}") String greetingEndpoint) {
return new Retrofit.Builder().baseUrl(greetingEndpoint)
.client(new OkHttpClient.Builder()
.addInterceptor(
OkHttpObservationInterceptor.builder(observationRegistry, "http.client.requests").build())
.build())
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(GreetingClient.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.micrometer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
public class SampleController {

private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

private final GreetingClient client;

public SampleController(GreetingClient client) {
this.client = client;
}

@GetMapping("/greet/{name}")
Map<String, String> greet(@PathVariable String name) throws IOException {
LOGGER.info("<TEST_MARKER> Greeting {}...", name);
return Map.of("greeting", client.getGreeting(name).execute().body());
}

@GetMapping("/greeting/{name}")
String greeting(@PathVariable String name) {
return "Hi %s!".formatted(name);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright 2023 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring.application.name=boot3-retrofit-sample
server.port=8080

management.endpoints.web.exposure.include=*
management.tracing.sampling.probability=1.0
management.metrics.distribution.percentiles-histogram.http.server.requests=true
management.metrics.distribution.percentiles-histogram.http.client.requests=true

spring.output.ansi.enabled=always
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG

greeting.endpoint=http://localhost:${server.port}
Loading

0 comments on commit ae9347a

Please sign in to comment.