|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.library.okhttp.v3_0.websocket.internal; |
| 7 | + |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.annotation.Nullable; |
| 10 | +import io.opentelemetry.api.common.Attributes; |
| 11 | +import okhttp3.Response; |
| 12 | +import okhttp3.WebSocket; |
| 13 | +import okhttp3.WebSocketListener; |
| 14 | +import okio.ByteString; |
| 15 | + |
| 16 | +public class WebsocketListenerWrapper extends WebSocketListener { |
| 17 | + private final WebSocketListener delegate; |
| 18 | + |
| 19 | + public WebsocketListenerWrapper(WebSocketListener delegate) { |
| 20 | + this.delegate = delegate; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) { |
| 25 | + Attributes attributes = WebsocketAttributeExtractor.extractAttributes(webSocket); |
| 26 | + WebsocketEventGenerator.generateEvent("websocket.close", attributes); |
| 27 | + delegate.onClosed(webSocket, code, reason); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) { |
| 32 | + Attributes attributes = WebsocketAttributeExtractor.extractAttributes(webSocket); |
| 33 | + WebsocketEventGenerator.generateEvent("websocket.open", attributes); |
| 34 | + delegate.onOpen(webSocket, response); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) { |
| 39 | + Attributes attributes = WebsocketAttributeExtractor.extractAttributes(webSocket); |
| 40 | + WebsocketEventGenerator.generateEvent( |
| 41 | + "websocket.message", |
| 42 | + attributes.toBuilder() |
| 43 | + .put(WebsocketAttributes.MESSAGE_TYPE, "text") |
| 44 | + .put(WebsocketAttributes.MESSAGE_SIZE, text.length()) |
| 45 | + .build()); |
| 46 | + delegate.onMessage(webSocket, text); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onMessage(@NonNull WebSocket webSocket, @NonNull ByteString bytes) { |
| 51 | + Attributes attributes = WebsocketAttributeExtractor.extractAttributes(webSocket); |
| 52 | + WebsocketEventGenerator.generateEvent( |
| 53 | + "websocket.message", |
| 54 | + attributes.toBuilder() |
| 55 | + .put(WebsocketAttributes.MESSAGE_TYPE, "bytes") |
| 56 | + .put(WebsocketAttributes.MESSAGE_SIZE, bytes.size()) |
| 57 | + .build()); |
| 58 | + delegate.onMessage(webSocket, bytes); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onFailure( |
| 63 | + @NonNull WebSocket webSocket, @NonNull Throwable t, @Nullable Response response) { |
| 64 | + Attributes attributes = WebsocketAttributeExtractor.extractAttributes(webSocket); |
| 65 | + WebsocketEventGenerator.generateEvent("websocket.error", attributes); |
| 66 | + delegate.onFailure(webSocket, t, response); |
| 67 | + } |
| 68 | +} |
0 commit comments