Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Add more logging around websocket subscriptions
Browse files Browse the repository at this point in the history
If the subscription is for an agent (as opposed to the UI), log
more details about connects, disconnects, and errors.
  • Loading branch information
Craig Jellick committed May 25, 2018
1 parent db4e637 commit 8416b5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package io.cattle.platform.api.pubsub.subscribe.jetty;

import io.cattle.platform.api.auth.Policy;
import io.cattle.platform.api.pubsub.subscribe.MessageWriter;
import io.cattle.platform.api.pubsub.subscribe.NonBlockingSubscriptionHandler;
import io.cattle.platform.api.pubsub.util.SubscriptionUtils;
import io.cattle.platform.api.pubsub.util.SubscriptionUtils.SubscriptionStyle;
import io.cattle.platform.api.utils.ApiUtils;
import io.cattle.platform.iaas.event.IaasEvents;
import io.github.ibuildthecloud.gdapi.request.ApiRequest;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
Expand All @@ -20,7 +26,17 @@ public class JettyWebSocketSubcriptionHandler extends NonBlockingSubscriptionHan
protected MessageWriter getMessageWriter(ApiRequest apiRequest) throws IOException {
HttpServletRequest req = apiRequest.getServletContext().getRequest();
HttpServletResponse resp = apiRequest.getServletContext().getResponse();
final WebSocketMessageWriter messageWriter = new WebSocketMessageWriter();
Policy policy = ApiUtils.getPolicy();
String identifier = null;
SubscriptionStyle style = SubscriptionUtils.getSubscriptionStyle(policy);
if (SubscriptionStyle.QUALIFIED.equals(style)) {
String key = SubscriptionUtils.getSubscriptionQualifier(policy);
String value = SubscriptionUtils.getSubscriptionQualifierValue(policy);
if (IaasEvents.AGENT_QUALIFIER.equals(key) && StringUtils.isNotEmpty(value)) {
identifier = String.format("%s [%s]", key, value);
}
}
final WebSocketMessageWriter messageWriter = new WebSocketMessageWriter(identifier);
WebSocketServerFactory factory = new WebSocketServerFactory();
factory.getPolicy().setAsyncWriteTimeout(1000);
factory.setCreator(new WebSocketCreator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public class WebSocketMessageWriter extends WebSocketAdapter implements MessageW
private boolean connectionClosed = false;
private AtomicInteger queuedMessageCount = new AtomicInteger();

private String identifier;

public WebSocketMessageWriter(String identifier) {
this.identifier = identifier;
if (identifier != null) {
log.info("Creating websocket message writer for {}", identifier);
}
}

@Override
public void onWebSocketConnect(Session session) {
this.session = session;
Expand All @@ -33,7 +42,18 @@ public void onWebSocketConnect(Session session) {
@Override
public void onWebSocketClose(int closeCode, String message) {
connectionClosed = true;
log.debug("Websocket connection closed. Code: [{}], message: [{}].", closeCode, message);
if (identifier != null) {
log.info("Websocket connection closed for {}. Code: [{}], message: [{}].", identifier, closeCode, message);
} else {
log.debug("Websocket connection closed. Code: [{}], message: [{}].", closeCode, message);
}
}

@Override
public void onWebSocketError(Throwable cause) {
if (identifier != null) {
log.warn("Unexpected websocket error for {}", identifier);
}
}

@Override
Expand Down

0 comments on commit 8416b5c

Please sign in to comment.