Add spring webmvc documentation (#915)

* add readme and rename WebMvcFilter

* Update README.md

* format

* clean up readme

* clean up readme

* clean up readme

* Update README.md

* fix web mvc naming

* format, clean up comments

* format, clean up comments
This commit is contained in:
Munir Abdinur 2020-08-11 02:53:18 +00:00 committed by GitHub
parent b5fbf90977
commit fe45515536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 18 deletions

View File

@ -16,7 +16,7 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc; package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc;
import io.opentelemetry.instrumentation.springwebmvc.WebMVCTracingFilter; import io.opentelemetry.instrumentation.spring.webmvc.WebMvcTracingFilter;
import io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.Tracer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -27,13 +27,13 @@ import org.springframework.web.filter.OncePerRequestFilter;
/** Configures {@link WebMVCFilter} for tracing. */ /** Configures {@link WebMVCFilter} for tracing. */
@Configuration @Configuration
@EnableConfigurationProperties(WebMVCProperties.class) @EnableConfigurationProperties(WebMvcProperties.class)
@ConditionalOnProperty(prefix = "opentelemetry.trace.web", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "opentelemetry.trace.web", name = "enabled", matchIfMissing = true)
@ConditionalOnClass(OncePerRequestFilter.class) @ConditionalOnClass(OncePerRequestFilter.class)
public class WebMVCFilterAutoConfiguration { public class WebMvcFilterAutoConfiguration {
@Bean @Bean
public WebMVCTracingFilter otelWebMVCTracingFilter(final Tracer tracer) { public WebMvcTracingFilter otelWebMvcTracingFilter(final Tracer tracer) {
return new WebMVCTracingFilter(tracer); return new WebMvcTracingFilter(tracer);
} }
} }

View File

@ -25,7 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* exist in application context * exist in application context
*/ */
@ConfigurationProperties(prefix = "opentelemetry.trace.web") @ConfigurationProperties(prefix = "opentelemetry.trace.web")
public final class WebMVCProperties { public final class WebMvcProperties {
private boolean enabled = true; private boolean enabled = true;
public boolean isEnabled() { public boolean isEnabled() {

View File

@ -6,5 +6,5 @@ io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging.LoggingS
io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.webmvc.WebMVCFilterAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.webmvc.WebMvcFilterAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.aspects.TraceAspectAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.aspects.TraceAspectAutoConfiguration

View File

@ -19,19 +19,19 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration;
import io.opentelemetry.instrumentation.springwebmvc.WebMVCTracingFilter; import io.opentelemetry.instrumentation.spring.webmvc.WebMvcTracingFilter;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/** Spring Boot auto configuration test for {@link WebMVCFilterAutoConfiguration} */ /** Spring Boot auto configuration test for {@link WebMvcFilterAutoConfiguration} */
class WebMVCFilterAutoConfigurationTest { class WebMvcFilterAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner() new ApplicationContextRunner()
.withConfiguration( .withConfiguration(
AutoConfigurations.of( AutoConfigurations.of(
TracerAutoConfiguration.class, WebMVCFilterAutoConfiguration.class)); TracerAutoConfiguration.class, WebMvcFilterAutoConfiguration.class));
@Test @Test
@DisplayName("when web is ENABLED should initialize WebMvcTracingFilter bean") @DisplayName("when web is ENABLED should initialize WebMvcTracingFilter bean")
@ -40,7 +40,7 @@ class WebMVCFilterAutoConfigurationTest {
.withPropertyValues("opentelemetry.trace.web.enabled=true") .withPropertyValues("opentelemetry.trace.web.enabled=true")
.run( .run(
(context) -> { (context) -> {
assertThat(context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class)) assertThat(context.getBean("otelWebMvcTracingFilter", WebMvcTracingFilter.class))
.isNotNull(); .isNotNull();
}); });
} }
@ -52,7 +52,7 @@ class WebMVCFilterAutoConfigurationTest {
.withPropertyValues("opentelemetry.trace.web.enabled=false") .withPropertyValues("opentelemetry.trace.web.enabled=false")
.run( .run(
(context) -> { (context) -> {
assertThat(context.containsBean("otelWebMVCTracingFilter")).isFalse(); assertThat(context.containsBean("otelWebMvcTracingFilter")).isFalse();
}); });
} }
@ -61,7 +61,7 @@ class WebMVCFilterAutoConfigurationTest {
void noProperty() { void noProperty() {
this.contextRunner.run( this.contextRunner.run(
(context) -> { (context) -> {
assertThat(context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class)) assertThat(context.getBean("otelWebMvcTracingFilter", WebMvcTracingFilter.class))
.isNotNull(); .isNotNull();
}); });
} }

View File

@ -0,0 +1,90 @@
# Manual Instrumentation for Spring Web MVC
Provides OpenTelemetry tracing for spring-webmvc RestControllers by leveraging spring-webmvc [filters](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter).
## Quickstart
### Add these dependencies to your project.
Replace `SPRING_VERSION` with the version of spring you're using.
- `Minimum version: 3.1`
Replace `OPENTELEMETRY_VERSION` with the latest stable [release](https://mvnrepository.com/artifact/io.opentelemetry).
- `Minimum version: 0.8.0`
For Maven add to your `pom.xml`:
```xml
<dependencies>
<!-- opentelemetry instrumentation -->
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-webmvc-3.1</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
<!-- opentelemetry exporter -->
<!-- replace this default exporter with your opentelemetry exporter (ex. otlp/zipkin/jaeger/..) -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporters-logging</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
<!-- required to instrument spring-webmvc -->
<!-- this artifact should already be present in your application -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>SPRING_VERSION</version>
</dependency>
</dependencies>
```
For Gradle add to your dependencies:
```groovy
// opentelemetry instrumentation
implementation 'io.opentelemetry.instrumentation:opentelemetry-spring-webmvc-3.1:OPENTELEMETRY_VERSION'
// opentelemetry exporter
// replace this default exporter with your opentelemetry exporter (ex. otlp/zipkin/jaeger/..)
implementation 'io.opentelemetry:opentelemetry-exporters-logging:OPENTELEMETRY_VERSION'
// required to instrument spring-webmvc
// this artifact should already be present in your application
implementation 'org.springframework:spring-webmvc:SPRING_VERSION'
```
### Features
#### WebMvcTracingFilter
WebMvcTracingFilter adds OpenTelemetry server spans to requests processed by request dispatch, on any spring servlet container. An example is shown below:
##### Usage
```java
import io.opentelemetry.instrumentation.spring.webmvc.WebMvcTracingFilter
import io.opentelemetry.trace.Tracer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class WebMvcTracingFilterConfig {
@Bean
public WebMvcTracingFilter webMvcTracingFilter(Tracer tracer) {
return new WebMvcTracingFilter(tracer);
}
}
```
### Starter Guide
Check out the opentelemetry [quick start](https://github.com/open-telemetry/opentelemetry-java/blob/master/QUICKSTART.md) to learn more about OpenTelemetry instrumentation.

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package io.opentelemetry.instrumentation.springwebmvc; package io.opentelemetry.instrumentation.spring.webmvc;
import io.grpc.Context; import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Getter;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package io.opentelemetry.instrumentation.springwebmvc; package io.opentelemetry.instrumentation.spring.webmvc;
import io.opentelemetry.context.Scope; import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span;
@ -27,13 +27,13 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
public class WebMVCTracingFilter extends OncePerRequestFilter implements Ordered { public class WebMvcTracingFilter extends OncePerRequestFilter implements Ordered {
private static final String FILTER_CLASS = "WebMVCTracingFilter"; private static final String FILTER_CLASS = "WebMVCTracingFilter";
private static final String FILTER_METHOD = "doFilterInteral"; private static final String FILTER_METHOD = "doFilterInteral";
private final SpringWebMvcServerTracer tracer; private final SpringWebMvcServerTracer tracer;
public WebMVCTracingFilter(Tracer tracer) { public WebMvcTracingFilter(Tracer tracer) {
this.tracer = new SpringWebMvcServerTracer(tracer); this.tracer = new SpringWebMvcServerTracer(tracer);
} }