{"id":14983143,"url":"https://github.com/opentracing-contrib/java-spring-web","last_synced_at":"2025-04-04T12:08:44.154Z","repository":{"id":15789462,"uuid":"78768320","full_name":"opentracing-contrib/java-spring-web","owner":"opentracing-contrib","description":"OpenTracing Spring Web instrumentation","archived":false,"fork":false,"pushed_at":"2022-01-17T07:54:28.000Z","size":438,"stargazers_count":107,"open_issues_count":22,"forks_count":58,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-28T11:09:23.675Z","etag":null,"topics":["interceptor","opentracing","resttemplate","spring","spring-boot","spring-cloud","spring-mvc","spring-web","tracing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opentracing-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-12T17:14:33.000Z","updated_at":"2025-02-07T11:43:40.000Z","dependencies_parsed_at":"2022-07-20T22:54:47.064Z","dependency_job_id":null,"html_url":"https://github.com/opentracing-contrib/java-spring-web","commit_stats":null,"previous_names":[],"tags_count":76,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-spring-web","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-spring-web/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-spring-web/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-spring-web/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opentracing-contrib","download_url":"https://codeload.github.com/opentracing-contrib/java-spring-web/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174423,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interceptor","opentracing","resttemplate","spring","spring-boot","spring-cloud","spring-mvc","spring-web","tracing"],"created_at":"2024-09-24T14:06:48.139Z","updated_at":"2025-04-04T12:08:44.132Z","avatar_url":"https://github.com/opentracing-contrib.png","language":"Java","readme":"[![Build Status][ci-img]][ci] [![Released Version][maven-img]][maven]\n\n# OpenTracing Spring Web Instrumentation\n\nThis library provides instrumentation for Spring Web applications (Boot, MVC and WebFlux). It creates tracing data for \nserver requests and also client requests (`RestTemplate`, `AsyncRestTemplate` and `WebClient`).\n\n## Comparison with [opentracing-spring-cloud](https://github.com/opentracing-contrib/java-spring-cloud)\n\nAs it was mentioned above, this library traces only inbound/outbound HTTP requests. If you would like to \nget automatically traced different set of technologies e.g. `spring-cloud-netflix`, JMS or even more then\nuse project [opentracing-spring-cloud](https://github.com/opentracing-contrib/java-spring-cloud) instead.\n\nFor reactive applications, it is especially recommended to use `reactor` tracing from\n[opentracing-spring-cloud](https://github.com/opentracing-contrib/java-spring-cloud), as that will ensure\nthat the `Span` is activated in reactor handler functions. (Without that, one would have to extract the\n`Span` from the subscriber context.)\n\n## How does the server tracing work?\n\n### Servlet\nServer span is started in [Web Servlet Filter](https://github.com/opentracing-contrib/java-web-servlet-filter),\nthen tracing interceptor adds spring related tags and logs. There are use case when spring boot invokes a handler after \na request processing in filter finished, in this case interceptor starts a new span as `followsFrom` \nwhich references the initial span created in the servlet filter.\n\n### Reactive\nServer span is started in [TracingWebFilter](opentracing-spring-web/src/main/java/io/opentracing/contrib/spring/web/webfilter/TracingWebFilter.java)\n(upon subscription), then `onNext()`, `onError()`, etc. handlers add Spring WebFlux related tags and logs.\n\n## Library versions\n\nVersions 1.x.y, 2.x.y, ... of the library are meant to target Spring Framework 5.x and Spring Boot 2.x while versions 0.x.y are meant to be used with Spring Framework 4.3 and Spring Boot 1.5\n\n\n## Configuration\n\n### Spring Boot Auto-configuration\nIf you are using Spring Boot the easiest way how to configure OpenTracing instrumentation is to use auto-configuration:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.opentracing.contrib\u003c/groupId\u003e\n  \u003cartifactId\u003eopentracing-spring-web-starter\u003c/artifactId\u003e\n\u003c/dependency\u003e\n\n```\nJust provide an OpenTracing tracer bean and all required configuration is automatically\ndone for you. It also instruments all `RestTemplate`, `AsyncRestTemplate`, `WebClient` and `WebClient.Builder` beans.\n\n### Manual configuration\n\n#### Servlet and MVC Server\nConfiguration needs to add `TracingFilter` and `TracingHandlerInterceptor`. Both of these classes\nare required!\n\nTracing interceptor can be instantiated manually or injected via CDI, but\nit needs bean of type `Tracer` configured.\n\nJava based configuration:\n```java\n@Configuration\n@Import({TracingHandlerInterceptor.class})\npublic class MVCConfiguration extends WebMvcConfigurerAdapter {\n\n    @Autowired\n    private Tracer tracer;\n\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(new TracingHandlerInterceptor(tracer));\n    }\n\n    @Bean\n    public FilterRegistrationBean tracingFilter() {\n        TracingFilter tracingFilter = new TracingFilter(tracer);\n\n        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(tracingFilter);\n        filterRegistrationBean.addUrlPatterns(\"/*\");\n        filterRegistrationBean.setOrder(Integer.MIN_VALUE);\n        filterRegistrationBean.setAsyncSupported(true);\n\n        return filterRegistrationBean;\n    }\n}\n```\n\nXML based configuration can be used too. Filter can be also directly defined in `web.xml`.\n\n#### Reactive Server\nConfiguration needs to add the `TracingWebFilter` bean.\n\n```java\n@Configuration\nclass TracingConfiguration {\n    @Bean\n    public TracingWebFilter tracingWebFilter(Tracer tracer) {\n        return new TracingWebFilter(\n                tracer,\n                Integer.MIN_VALUE,               // Order\n                Pattern.compile(\"\"),             // Skip pattern\n                Collections.emptyList(),         // URL patterns, empty list means all\n                Arrays.asList(new WebFluxSpanDecorator.StandardTags(), new WebFluxSpanDecorator.WebFluxTags())\n        );\n    }\n}\n```\n\n#### Client\n```java\nRestTemplate restTemplate = new RestTemplate();\nrestTemplate.setInterceptors(Collections.singletonList(new TracingRestTemplateInterceptor(tracer)));\n\n// the same applies for AsyncRestTemplate \n```\n\n#### Reactive Client\n```java\nWebClient webClient = WebClient.builder()\n        .filter(new TracingExchangeFilterFunction(tracer, Collections.singletonList(new WebClientSpanDecorator.StandardTags())))\n        .build();\n```\n\n## Access server span\n```java\n@RequestMapping(\"/hello\")\npublic String hello(HttpServletRequest request) {\n    Span serverSpan = tracer.activeSpan();\n\n    Span span = tracer.buildSpan(\"localSpan\")\n                      .asChildOf(serverSpan.context())\n                      .start();\n    try {\n        // Traced work happens between start() and deactivate();\n        return \"Hello world!\";\n    } finally {\n        span.finish();\n    }\n}\n```\n\n## Development\n```shell\n./mvnw clean install\n```\n\n## Release\nFollow instructions in [RELEASE](RELEASE.md)\n\n\n   [ci-img]: https://travis-ci.org/opentracing-contrib/java-spring-web.svg?branch=master\n   [ci]: https://travis-ci.org/opentracing-contrib/java-spring-web\n   [maven-img]: https://img.shields.io/maven-central/v/io.opentracing.contrib/opentracing-spring-web.svg?maxAge=2592000\n   [maven]: http://search.maven.org/#search%7Cga%7C1%7Copentracing-spring-web\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentracing-contrib%2Fjava-spring-web","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopentracing-contrib%2Fjava-spring-web","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentracing-contrib%2Fjava-spring-web/lists"}