Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - How to tag a subtree of spans in Spring Cloud Sleuth? Or, how to sub-divide traces?

When a sender sends a post, I need to notify multiple subscribers. In this case, "sender sends a post" is a HTTP post and create a trace.

My goal: When looking at my logs, I will know "which subscriber is the method creating this log notifying".

My naive thoughts: For each of the "notify subscriber A", "notify subscriber B", etc, they are a sub-tree of spans. If I can give tag onto each of these sub-tree of spans then I am done.

However, I do not know how to do that. Or, is there any other ways to achieve my goal?

Thanks!

question from:https://stackoverflow.com/questions/66065532/how-to-tag-a-subtree-of-spans-in-spring-cloud-sleuth-or-how-to-sub-divide-trac

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand what you are trying to achieve here, that is the default behavior of Sleuth, please take a look at the docs for the trace-span and the parent-child spans relationships.

The only thing you need to do is creating a new Span every time you notify a subscriber. There are multiple ways to do this, please see the the docs (e.g.: tracer.withSpan, @NewSpan).

Here's an imaginary example:

@GetMapping("/something") // Sleuth has already created a span for your endpoint
public void something() {
    subscribers.forEach(subscriber -> notify(subscriber, new Event("foo")));
}

@NewSpan // This does the trick
public void notify(Subscriber subscriber, Event event) {
    subscriber.notify(event);
}

If you want to tag your spans, please check the link to the docs I put above, it will show you how to attach tags.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...