spring sleuth baggage propagation not working - spring

I am using Sleuth first time need some help. I am currently using sleuth 2.2.3.RELEASE.
My requirement is I want to propagate 2 fields product id and product type so that I can read these 2 values from other microservices for that I am using baggage propagation.
1. In one of the microservice in rest API my code is something like this,
BaggageField PRODUCT_ID = BaggageField.create("x-df-product-id");
PRODUCT_ID.updateValue(productId); // I have value of productId that value I want to propogate
BaggageField PRODUCT_TYPE = BaggageField.create("x-df-product-type");
PRODUCT_TYPE.updateValue(productType); // I have value of productType that value I want to propogate
Tracing.newBuilder().propagationFactory(
BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
.add(SingleBaggageField.remote(PRODUCT_ID))
.add(SingleBaggageField.remote(PRODUCT_TYPE))
.build());
In another microservice, I am reading the values like this:
String baggageProductId=BaggageField.getAllValues().entrySet().stream.filter(e ->"PRODUCT_ID".equalIgnoreCase(e.getKey())).map(Map.Entry::getValue).findFirst().orElse(null);
String baggageProducType=BaggageField.getAllValues().entrySet().stream.filter(e ->"PRODUCT_TYPE".equalIgnoreCase(e.getKey())).map(Map.Entry::getValue).findFirst().orElse(null);
in application.yml these are the entries:
sleuth:
baggage:
remoteFields:
-x-df-product-id
-x-df-product-type
but I am getting both values baggageProductId and baggageProducType as null.
I am not using span anywhere is that a problem? not sure how can I read these values. can you please help me?

updates:
I updated my code like this and it's working fine, BUT...need some help.
application.yml changed to
sleuth:
baggage:
baggage-keys:
-x-df-product-id
-x-df-product-type
log.slf4.whitelisted-mdc-keys:
-x-df-product-id
-x-df-product-type
2.In Microservice-1 I have the following code
BaggageField baggageFieldProductId = BaggageField.getByName("x-df-product-id");
baggageProductId=baggageFieldProductId.getValue();
BaggageField baggageFieldProductType = BaggageField.getByName("x-df-product-type");
baggageProductType=baggageFieldProductType.getValue();
I inserted the same entries in each application.yml file for each microservice.
after that, I am able to retrieve these values in microservice 1, microservice 2, microservice 3
but getting null value in microservice 4. Don't know why? the logic of code retrieval is the same everywhere.
the only difference is my microservices 1,2,3 spring boot version is 2.2.8 and the microservice 4 spring boot version is 2.3.6.
Need some help. really don't know why I am getting null value. Thank You!!!

You must be config in you applicaiton.properties
spring.sleuth.propagation-keys[0]= x-df-product-id
spring.sleuth.propagation.tag.whitelisted-keys[0]=x-df-product-id
spring.sleuth.log.slf4j.whitelisted-mdc-keys[0]= x-df-product-id
this source code
org.springframework.cloud.sleuth.autoconfig.TraceBaggageConfiguration

Related

spring cloud stream multi sink with dynamic destinations

I need to use dynamic group names for RabbitMQ binder based Multisink (multiple sinks) app.
Seems like the
spring.cloud.stream.bindings..group property can't be set at runtime.
Any thoughts on how to go about it ?
SPB: 2.2.8.RELEASE ,
spring-cloud-dependencies: Hoxton.SR6 ,
spring-cloud-stream-dependencies: Horsham.SR6
Thanx,
You can use a property placeholder there...
spring.cloud.stream.bindings.input-in-0.group=${group.prop:foo}
...and then set the property on the command line:
-Dgroup.prop=bar

How can I have custom identifiers/primary keys for my resources?

My table has a primary key other than id and react-admin enforces id to be returned in the response by the DataProvider. So can I configure different primary keys/identifiers for my resources?
I am using this library - https://github.com/Steams/ra-data-hasura-graphql
Right now I have made few changes in my library code to make this work, but I need an idea to implement it, so anyone using this library doesn't need to go thru whole code to make it work.
const config = {
'primaryKey': {
'tableName': 'primaryKey1', 'tableName2': 'primaryKey2'
}
};
I was thinking of something like passing configuration like this.
Thanks.
I don't believe it is currently possible with the 0.1.0 release. However in hasura you can expose the column with a different name
hasura column exposed name

Spring Boot Config File - Can JSON object go in?

In my Spring boot application, I need to store the list of State and some configuration related to State. For example - State of GA and FL allows lottery, but Hawaai do not.. so my JSON object that store this info looks like
states:[
{"code":"GA", "lottery":"yes"},
{"code":"HI", "lottery":"no"}
..
}
Bear with me..if this sounds like naive question. I am trying to find out if application.properties is a nice place to store this kind of information. I mean, I can either store in the properties and load at startup..or move it to a Database and do the same. But since application.properties is handy and does not require connecting to a database, sounds like an easy approach. Need your views. Thanks in advance.
The configuration is too complicated. New statelottery.json file
{"GA": "yes", "HI": "no"}
Read the json file. Then json2map. I don't think there is any difficulty. It has nothing to do with springboot.

spring reactive: check if user exists or not

I am using spring reactive and need to check weather user with particular
data exists or not and currently I am not able to solve that problem.
Considering the scenerio
In my document I need to check if username or email already exists or not
In RDBS I can do it as
select count(id)>0 where username='abc' or email='abc#idx.com'
while using spring reactive which returns either mono or flux the simple most query becomes
{$or:[{"username":"abc"},{"email":"abc#idx.com"}]} which will return flux but I need boolean to verify from db
On solution is that I can get Flux<User> and the iterate it using form loop but then using ' result.block()' whick will block some other threads and therefore not a clean solution.
Is there any clean solution or any Idea how to solve this.
Thanks
Edit One possible solution can be creating unique indexing in monogdb, that I am using right now. But if there is any other solution please let me know
Using Spring Data MongoDB, you can use something like below:
public interface ReactiveUserRepository extends ReactiveSortingRepository<User, Long> {
Mono<User> findByUsernameAndEmail(String username, String email);
}
Find a single entity for the given criteria. It completes with IncorrectResultSizeDataAccessException on non-unique results.
You can refer to the complete syntax here:
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

Spring Data: can it find by two values of the same field without writing the implementation?

I am doing a Spring web app and I use Spring Data.
I am able to use Spring Data to find objects by a single value of a field. For example:
some_object_repository.findByFirstName("John")
Is there any way I can provide two first names (e.g., "John", "David") similar to the following in concept:
some_object_repository.findByFirstName({"John", "David"})
without me writing a custom implementation?
Regards and thanks!
You can do this with In at the end
findByAgeIn(Collection ages) … where x.age in ?1
http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods
Section 2.3.2 Query creation
In your case it will be
findByFirstNameIn(Collection names)

Resources