0

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!

Eugene
  • 117,005
  • 15
  • 201
  • 306
ch271828n
  • 15,854
  • 5
  • 53
  • 88

1 Answers1

1

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.

Jonatan Ivanov
  • 4,895
  • 2
  • 15
  • 30