@comprehend/telemetry-browser
Version:
Integration of comprehend.dev with OpenTelemetry in browser environments.
103 lines (102 loc) • 3.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketConnection = void 0;
const INGESTION_ENDPOINT = 'wss://ingestion.comprehend.dev';
class WebSocketConnection {
ws;
organization;
token;
debug;
reconnectAttempts = 0;
maxReconnectAttempts = 5;
reconnectDelay = 1000;
messageQueue = [];
isConnected = false;
constructor(organization, token, debug) {
this.organization = organization;
this.token = token;
this.debug = debug;
this.connect();
}
connect() {
try {
const url = `${INGESTION_ENDPOINT}/${this.organization}/observations`;
this.ws = new WebSocket(url);
this.ws.onopen = () => {
this.debug?.('WebSocket connected');
this.isConnected = true;
this.reconnectAttempts = 0;
// Send init message
this.sendMessage({
event: "init",
protocolVersion: 1,
token: this.token
});
// Send queued messages
while (this.messageQueue.length > 0) {
const message = this.messageQueue.shift();
if (message) {
this.sendMessage(message);
}
}
};
this.ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
this.debug?.(`Received: ${JSON.stringify(message)}`);
}
catch (error) {
this.debug?.(`Failed to parse message: ${error}`);
}
};
this.ws.onclose = (event) => {
this.debug?.(`WebSocket closed: ${event.code} ${event.reason}`);
this.isConnected = false;
this.scheduleReconnect();
};
this.ws.onerror = (event) => {
this.debug?.(`WebSocket error: ${event}`);
this.isConnected = false;
};
}
catch (error) {
this.debug?.(`Failed to connect: ${error}`);
this.scheduleReconnect();
}
}
scheduleReconnect() {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
this.debug?.(`Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})`);
setTimeout(() => {
this.connect();
}, delay);
}
else {
this.debug?.('Max reconnection attempts reached');
}
}
sendMessage(message) {
if (!this.isConnected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
this.debug?.('WebSocket not ready, queuing message');
this.messageQueue.push(message);
return;
}
try {
const messageStr = JSON.stringify(message);
this.ws.send(messageStr);
this.debug?.(`Sent: ${messageStr}`);
}
catch (error) {
this.debug?.(`Failed to send message: ${error}`);
this.messageQueue.push(message);
}
}
close() {
if (this.ws) {
this.ws.close();
}
}
}
exports.WebSocketConnection = WebSocketConnection;