Specify sampler probability using annotations or programmatically using Spring Cloud Sleuth? - spring-boot

We're using Spring Boot 2.7.0, Spring cloud 2021.0.3 in GCP and have added these dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-trace</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>
We have specified the generic sample probability in our properties file like this:
spring.sleuth.sampler.probability=0.1
spring.sleuth.web.skipPattern=(^/admin/.*|.+favicon.*|^/ping$)
But for some methods we'd like to use a different sampler probability (e.g. we always want to generate a trace for some methods).
Questions:
Is it possible to specify the sample rate probability of a method when using annotations from the Spring Sleuth (or related) projects?
If not, is it possible to specify the sample rate probability of a method programmatically (without annotations)?
If not, is it possible to configure different sample rates in the spring property file / yaml file?

You can create a bean of type org.springframework.cloud.sleuth.exporter.SpanFilter. There you have access to FinishedSpan basing on which you can return true if you want to export the span or not.

Related

spring security and oauth dependencies?

I am confused what is the different between these dependencies and which one should I use. Would you explain the differences?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!---------------------------------------------------------->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<!---------------------------------------------------------->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<!---------------------------------------------------------->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
and when to use
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
spring-boot-starter-xxx are with no surprise starters for applications with Spring. It:
pulls most the dependencies you need on a subject
auto-wires many useful beans
enables to define many configuration options from properties (application.properties, application.yaml, command line arguments, environment variables,...)
For OAuth2, there are two starters provided by Spring:
spring-boot-starter-oauth2-resource-server if your app is a resource-server (contains #RestController or #Controller with #ResponseBody)
spring-boot-starter-oauth2-client if your app is a client
#Controller with methods returning a template name
using a REST client like WebClient to consume a web-service secured with OAuth2
I also wrote starters which are thin wrappers arround spring-boot-starter-oauth2-resource-server and enable to replace all of necessary Java configuration with just a few properties. Refer to README and tutorials for more details (and to figure out how much effort and errors it can save)
It is possible for an app to be both a resource-server and a client, in which case you'll need to declare both starters in your dependencies.
You can have a look at the tutorials I already linked if you have doubts on how to configure resource-servers. There are a few whith Spring starters or mine. One is even both a resource-server and a client with Thymeleaf page consuming secured REST resources with WebClient)
You don't need to declare dependencies on spring-boot-starter-security, spring-security-oauth2-client or spring-security-oauth2-jose when using OAuth2 starters as all are transitive dependencies.
spring-security-config is useful for defining Spring #Beans in Java configuration files. You might need it in addition to starters.

Integration of Resilience 4j and Prometheus

How to integrate Resilience-4J and Prometheus with micro-services.
I have integrated Micro-services with resilience 4j and also with Prometheus .
My problem is ,in Prometheus i am not getting any resilience query,only HTTP and other which comes default are populating .
Kindly help
Steps that i have implemented so far are listed below...
1>created micro-services
2>Integrate micro-services with Resilience 4j by following steps
i>dependency in pom.xml file
ii>created separate class of Circuit-breaker and retry module.
iii>After that i have added Prometheus by adding its jar
which is...
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
3>In Prometheus Dashboard resilience queries/matrices are not populating.
i.e # HELP resilience4j_circuit-breaker_calls Circuit Breaker Call Stats
# TYPE resilience4j_circuit-breaker_calls gauge
resilience4j_circuit-breaker_calls{name="backendB",call_result="successful",} 0.0
resilience4j_circuit-breaker_calls{name="backendB",call_result="failed",} 0.0
Kindly help.
Thanks in Advance
i have also added below dependencies for resilience 4j and Prometheus integration...
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-metrics</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-prometheus</artifactId>
<version>0.16.0</version>
</dependency>
want resilience matrices on Prometheus dashboard.
Do you use a Framework like Spring Boot 1 or Spring Boot 2?
If that's the case you can have a look at our demo which also includes a Grafana Dashboard for Prometheus.
https://github.com/resilience4j/resilience4j-spring-boot2-demo
If you want to use Micrometer you should not add resilience4j-prometheus or resilience4j-metrics. resilience4j-prometheus uses the Prometheus Java library and resilience4j-metrics uses Dropwizard Metrics.
You should add resilience4j-micrometer instead. But it's not necessary if you use our Spring Boot 2 starter.

Spring: spring-data-mongodb or spring-boot-starter-data-mongodb

Which's the difference between
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
and,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
I'm developing an spring boot service.
spring-boot-starter-data-mongodb contains configuration classes for Spring Boot. It also includes the spring-data-mongodb library so you would only need to include the start in your boot app:
https://search.maven.org/artifact/org.springframework.boot/spring-boot-starter-data-mongodb/2.0.5.RELEASE/jar
spring-boot-starter-data-mongodb is a spring boot starter pom. For more information on starters:
spring-boot-starters
Dependency management is a critical aspects of any complex project. And doing this manually is less than ideal; the more time you spent on it the less time you have on the other important aspects of the project.
Spring Boot starters were built to address exactly this problem. Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.

Spring Boot Deployment Strategy

Am implementing the swagger 2 using Spring Boot. Using Dependencies-
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
It works good. But want to implement swagger in such a way that in production
swagger does not get deployed. Also will it be possible to host the swagger build differently than the application build on different host machines?
There are two approaches to do this.
Maven profile
By intrudue a maven profile like 'swagger' and add the springfox-swagger-ui related depdencenty to this maven profile. As maybe you need to use some swagger annotations in the java code, so it cannot eliminate the springfox-swagger2 dependency.
Spring profile( should be more better than option 1 )
For a standard spring-boot swagger2 config class, for example you can add the #Profile("swagger") annoation to enable the swagger2 integration only when add the spring.profiles.active=swagger in app running.
For the different host machines, I has no idea about that, but as my understanding, swagger will select all the spring boot endpoints so suppose you cannot leave them alone. But there is a library which can provides a way to publish springfox-swagger2 on spring boot actuator. so you can add management.port=8181 property in application.properties to makes spring-boot-actuator run on another TCP port.

spring-boot-starter versus spring-boot-starter-xxx

I noticed that the Spring Boot Sample Data Redis declares the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
See here for full POM: https://github.com/spring-projects/spring-boot/blob/v1.0.0.RC4/spring-boot-samples/spring-boot-sample-data-redis/pom.xml
I see no mention of the <artifactId>spring-boot-starter-redis</artifactId>
My question is: when do I use spring-boot-starter versus spring-boot-starter-xxx where xxx is the name of the project (here Redis)?
The answer to the specific question: spring-boot-starter is a baseline for the others, and for standalone (non-web) apps that don't use any other Spring components - it has basic support for Spring, Logging, and Testing, but nothing else (no webapp features, no database etc.). Since all the other starters depend on it, once you use another one you can remove the vanilla starter. EDIT: see here https://github.com/spring-projects/spring-boot/commit/77fd127e09963a844f8fb4e574e1f0d9d3424d4e.
Up to you on the redis starter, but I would use the starter if it exists, since it will typically cut down on the number of dependencies you need to declare. The redis one actually doesn't add a lot of value (hence it didn't exist until recently), but it probably ought to be used in the sample.

Resources