Skip to content

Commit

Permalink
Try to Tracing to Websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
kifj committed Jan 14, 2024
1 parent e9f6f6c commit dfe0d46
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@
<artifactId>microprofile-fault-tolerance-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/x1/stomp/control/QuickQuoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.opentelemetry.instrumentation.annotations.WithSpan;

import java.time.temporal.ChronoUnit;

@Produces({ APPLICATION_JSON, APPLICATION_XML })
Expand All @@ -24,5 +26,6 @@
public interface QuickQuoteService {
@GET
@Path("/quote.htm")
@WithSpan
Response retrieve(@QueryParam("symbols") String symbols, @QueryParam("output") String output);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import io.opentelemetry.api.trace.Tracer;
import x1.service.registry.Service;
import x1.service.registry.Services;
import x1.stomp.control.QuoteRetriever;
Expand Down Expand Up @@ -63,6 +65,9 @@ public class ShareSubscriptionWebSocketServerEndpoint implements MessageListener
@Inject
private SessionHolder sessionHolder;

@Inject
private Tracer tracer;

@OnOpen
public void onConnectionOpen(Session session) {
log.debug("Connection opened for session {}", session.getId());
Expand All @@ -88,15 +93,25 @@ public String onMessage(String message, Session session) throws IOException {

private void unsubscribe(String key) {
log.info("Unsubscribe: {}", key);
shareSubscription.find(key).ifPresent(shareSubscription::unsubscribe);
var span = tracer.spanBuilder("/ws/stocks").setAttribute("command", "unsubscribe").setAttribute("key", key).startSpan();
try {
shareSubscription.find(key).ifPresent(shareSubscription::unsubscribe);
} finally {
span.end();
}
}

private Optional<Quote> subscribe(String key) {
private Optional<Quote> subscribe(String key) {
log.info("Subscribe: {}", key);
var share = new Share(key);
var quote = quoteRetriever.retrieveQuote(share);
quote.ifPresent(q -> shareSubscription.subscribe(q.getShare()));
return quote;
var span = tracer.spanBuilder("/ws/stocks").setAttribute("command", "subscribe").setAttribute("key", key).startSpan();
try {
var share = new Share(key);
var quote = quoteRetriever.retrieveQuote(share);
quote.ifPresent(q -> shareSubscription.subscribe(q.getShare()));
return quote;
} finally {
span.end();
}
}

@OnClose
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/x1/stomp/test/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public void testMetrics() {
});
assertThat(shares).isEmpty();

// if no endpoint is configured, Micrometer uses NoOp metrics
Collection<Counter> counters = registry.get("rest-request-status").counters();
assertThat(counters).isNotEmpty();
assertThat(counters).anyMatch(counter -> counter.count() > 0 && counter.getId().getTag("method").equals("listAllShares"));
assertThat(counters).anyMatch(counter -> counter.getId().getTag("method").equals("listAllShares"));
}
}
1 change: 1 addition & 0 deletions src/test/resources/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
version="4.0" bean-discovery-mode="all">
<interceptors>
<class>x1.stomp.util.MeteredInterceptor</class>
<class>x1.stomp.util.LoggingInterceptor</class>
</interceptors>
</beans>

0 comments on commit dfe0d46

Please sign in to comment.