Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 27 additions & 32 deletions docs/modules/ROOT/pages/spring-cloud-bus/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,37 @@ NOTE: If you have enabled https://docs.spring.io/spring-cloud-config/reference/c
[[tracing-bus-events]]
== Tracing Bus Events

Bus events (subclasses of `RemoteApplicationEvent`) can be traced by setting
`spring.cloud.bus.trace.enabled=true`. If you do so, the Spring Boot `TraceRepository`
(if it is present) shows each event sent and all the acks from each service instance. The
following example comes from the `/trace` endpoint:
Bus events (subclasses of `RemoteApplicationEvent`) can be traced by setting `spring.cloud.bus.trace.enabled=true` and `management.endpoints.web.exposure.include=bustrace`.
When enabled, the Spring Cloud Bus `TraceRepository` (by default, an `InMemoryTraceRepository`, or you can provide your own `TraceRepository` bean) records each event sent and all acks from each service instance.
The following example comes from the `/actuator/bustrace` endpoint:

[source,json]
----
{
"timestamp": "2015-11-26T10:24:44.411+0000",
"info": {
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "stores:8081",
"destination": "*:**"
}
},
{
"timestamp": "2015-11-26T10:24:41.864+0000",
"info": {
"signal": "spring.cloud.bus.sent",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "customers:9000",
"destination": "*:**"
}
},
{
"timestamp": "2015-11-26T10:24:41.862+0000",
"info": {
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "customers:9000",
"destination": "*:**"
}
"traces": [
{
"timestamp": "2026-08-23T06:38:29.731486Z",
"origin": "stores:8081",
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent"
},
{
"timestamp": "2026-08-23T06:38:25.059455800Z",
"destination": "**:**",
"id": "2141f38c-7987-46a0-997a-ac95849365e0",
"origin": "customers:9000",
"signal": "spring.cloud.bus.sent",
"type": "RefreshRemoteApplicationEvent"
},
{
"timestamp": "2026-08-23T06:38:25.058947100Z",
"destination": "**",
"id": "2141f38c-7987-46a0-997a-ac95849365e0",
"origin": "customers:9000",
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent"
}
]
}
----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.bus;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.bus.endpoint.TraceBusEndpoint;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test for {@link TraceBusEndpoint}.
*
* @author Ngoc Nhan
*/
@SpringBootTest(
properties = { "management.endpoints.web.exposure.include=bustrace", "spring.cloud.bus.trace.enabled=true" })
public class BusTraceEndpointTests {

@Autowired(required = false)
private TraceBusEndpoint traceBusEndpoint;

@Test
public void contextLoads() {
assertThat(this.traceBusEndpoint).isNotNull();
}

@SpringBootConfiguration
@EnableAutoConfiguration
protected static class TestConfig {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.bus;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.rabbitmq.RabbitMQContainer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
import org.springframework.cloud.bus.trace.Trace;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

/**
* Test for {@link org.springframework.cloud.bus.endpoint.TraceBusEndpoint}.
*
* @author Ngoc Nhan
*/
@Testcontainers
@SpringBootTest(webEnvironment = RANDOM_PORT,
properties = { "management.endpoints.web.exposure.include=busrefresh,bustrace",
"spring.cloud.bus.trace.enabled=true" })
@AutoConfigureWebTestClient
public class TraceListenerIntegrationTests {

@Container
@ServiceConnection
static RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:4.0-management");

@Test
void busTrace(@Autowired WebTestClient client) {

client.post().uri("/actuator/busrefresh").exchange().expectStatus().is2xxSuccessful();
BusTraceResponse response = client.get()
.uri("/actuator/bustrace")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody(BusTraceResponse.class)
.returnResult()
.getResponseBody();
assertThat(response).isNotNull();
assertThat(response.traces()).isNotEmpty().hasSize(2);
}

@SpringBootConfiguration
@EnableAutoConfiguration
static class TestConfig {

}

record BusTraceResponse(List<Trace> traces) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.bus.endpoint.EnvironmentBusEndpoint;
import org.springframework.cloud.bus.endpoint.TraceBusEndpoint;
import org.springframework.cloud.bus.event.Destination;
import org.springframework.cloud.bus.event.EnvironmentChangeListener;
import org.springframework.cloud.bus.event.PathDestinationFactory;
import org.springframework.cloud.bus.event.TraceListener;
import org.springframework.cloud.bus.trace.InMemoryTraceRepository;
import org.springframework.cloud.bus.trace.TraceRepository;
import org.springframework.cloud.context.environment.EnvironmentManager;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -68,14 +70,28 @@ public BusConsumer busConsumer(ApplicationEventPublisher applicationEventPublish

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Endpoint.class })
@ConditionalOnBean(HttpExchangeRepository.class)
@ConditionalOnProperty(BusProperties.PREFIX + ".trace.enabled")
@ConditionalOnAvailableEndpoint(TraceBusEndpoint.class)
protected static class BusAckTraceConfiguration {

@Bean
@ConditionalOnMissingBean
public TraceListener ackTraceListener(HttpExchangeRepository repository) {
return new TraceListener(repository);
public TraceRepository traceRepository() {
return new InMemoryTraceRepository();
}

@Bean
@ConditionalOnBean(TraceRepository.class)
@ConditionalOnMissingBean
public TraceBusEndpoint traceBusEndpoint(TraceRepository traceRepository) {
return new TraceBusEndpoint(traceRepository);
}

@Bean
@ConditionalOnBean(TraceRepository.class)
@ConditionalOnMissingBean
public TraceListener traceListener(TraceRepository traceRepository) {
return new TraceListener(traceRepository);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.bus.endpoint;

import java.util.List;

import org.springframework.boot.actuate.endpoint.OperationResponseBody;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.cloud.bus.trace.Trace;
import org.springframework.cloud.bus.trace.TraceRepository;
import org.springframework.util.Assert;

/**
* {@link Endpoint @Endpoint} to expose {@link Trace} information.
*
* @author Ngoc Nhan
* @since 5.0.4
*/
@Endpoint(id = "bustrace")
public class TraceBusEndpoint {

private final TraceRepository repository;

/**
* Create a new {@link TraceBusEndpoint} instance.
* @param repository the trace repository
*/
public TraceBusEndpoint(TraceRepository repository) {
Assert.notNull(repository, "'repository' must not be null");
this.repository = repository;
}

/**
* Description of an application's {@link Trace} entries.
*/
@ReadOperation
public TraceDescriptor busTrace() {
return new TraceDescriptor(this.repository.findAll());
}

public static final class TraceDescriptor implements OperationResponseBody {

private final List<Trace> traces;

private TraceDescriptor(List<Trace> traces) {
this.traces = traces;
}

public List<Trace> getTraces() {
return this.traces;
}

}

}
Loading