Skip to content

Commit d1e600e

Browse files
authored
Generate events for OkHttp Websocket events (#863)
* Generate events for OkHttp Websocket events * move websocket to separate module and add test
1 parent 5b0af2e commit d1e600e

File tree

17 files changed

+426
-2
lines changed

17 files changed

+426
-2
lines changed

instrumentation/okhttp/okhttp-3.0/agent/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010
}
1111

1212
dependencies {
13+
compileOnly(libs.okhttp)
1314
implementation(project(":instrumentation:okhttp:okhttp-3.0:library"))
14-
implementation(libs.okhttp)
1515
implementation(libs.byteBuddy)
1616
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id("otel.android-library-conventions")
3+
id("otel.publish-conventions")
4+
}
5+
6+
description = "OpenTelemetry build-time auto-instrumentation for OkHttp Websocket on Android"
7+
8+
android {
9+
namespace = "io.opentelemetry.android.okhttp.websocket.agent"
10+
}
11+
12+
dependencies {
13+
implementation(project(":instrumentation:okhttp:websocket:okhttp-3.0:library"))
14+
compileOnly(libs.okhttp)
15+
implementation(libs.byteBuddy)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.agent.okhttp.v3_0.websocket;
7+
8+
import io.opentelemetry.instrumentation.library.okhttp.v3_0.websocket.internal.WebsocketListenerWrapper;
9+
import net.bytebuddy.asm.Advice;
10+
import okhttp3.WebSocketListener;
11+
12+
public class OkHttpClientWebsocketAdvice {
13+
14+
@Advice.OnMethodEnter
15+
public static void enter(
16+
@Advice.Argument(value = 1, readOnly = false) WebSocketListener webSocketListener) {
17+
if (webSocketListener instanceof WebsocketListenerWrapper) return;
18+
19+
webSocketListener = new WebsocketListenerWrapper(webSocketListener);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.agent.okhttp.v3_0.websocket;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.named;
9+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
10+
11+
import java.io.IOException;
12+
import net.bytebuddy.asm.Advice;
13+
import net.bytebuddy.build.Plugin;
14+
import net.bytebuddy.description.type.TypeDescription;
15+
import net.bytebuddy.dynamic.ClassFileLocator;
16+
import net.bytebuddy.dynamic.DynamicType;
17+
import okhttp3.WebSocketListener;
18+
19+
public class OkHttpClientWebsocketPlugin implements Plugin {
20+
21+
@Override
22+
public DynamicType.Builder<?> apply(
23+
DynamicType.Builder<?> builder,
24+
TypeDescription typeDescription,
25+
ClassFileLocator classFileLocator) {
26+
return builder.visit(
27+
Advice.to(OkHttpClientWebsocketAdvice.class)
28+
.on(named("newWebSocket").and(takesArgument(1, WebSocketListener.class))));
29+
}
30+
31+
@Override
32+
public void close() throws IOException {
33+
// No operation.
34+
}
35+
36+
@Override
37+
public boolean matches(TypeDescription target) {
38+
return target.getTypeName().equals("okhttp3.OkHttpClient");
39+
}
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.opentelemetry.instrumentation.agent.okhttp.v3_0.websocket.OkHttpClientWebsocketPlugin
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins {
2+
id("otel.android-library-conventions")
3+
id("otel.publish-conventions")
4+
}
5+
6+
description = "OpenTelemetry OkHttp Websocket library instrumentation for Android"
7+
8+
android {
9+
namespace = "io.opentelemetry.android.okhttp.websocket.library"
10+
11+
defaultConfig {
12+
consumerProguardFiles("consumer-rules.pro")
13+
}
14+
}
15+
16+
dependencies {
17+
api(project(":instrumentation:android-instrumentation"))
18+
compileOnly(libs.okhttp)
19+
api(libs.opentelemetry.instrumentation.okhttp)
20+
implementation(libs.opentelemetry.instrumentation.apiSemconv)
21+
implementation(libs.opentelemetry.api.incubator)
22+
}

instrumentation/okhttp/websocket/okhttp-3.0/library/consumer-rules.pro

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 com.google.auto.service.AutoService;
10+
import io.opentelemetry.android.instrumentation.AndroidInstrumentation;
11+
import io.opentelemetry.android.instrumentation.InstallationContext;
12+
13+
@AutoService(AndroidInstrumentation.class)
14+
public class OkHttpWebsocketInstrumentation implements AndroidInstrumentation {
15+
@Override
16+
public void install(@NonNull InstallationContext ctx) {
17+
WebsocketEventGenerator.configure(ctx);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.AttributesBuilder;
10+
import io.opentelemetry.semconv.HttpAttributes;
11+
import io.opentelemetry.semconv.NetworkAttributes;
12+
import io.opentelemetry.semconv.UrlAttributes;
13+
import okhttp3.Request;
14+
import okhttp3.WebSocket;
15+
16+
class WebsocketAttributeExtractor {
17+
private WebsocketAttributeExtractor() {}
18+
19+
static Attributes extractAttributes(WebSocket socket) {
20+
AttributesBuilder builder = Attributes.builder();
21+
Request request = socket.request();
22+
builder.put(NetworkAttributes.NETWORK_PROTOCOL_NAME, "websocket");
23+
24+
builder.put(HttpAttributes.HTTP_REQUEST_METHOD, request.method());
25+
builder.put(UrlAttributes.URL_FULL, request.url().toString());
26+
builder.put(NetworkAttributes.NETWORK_PEER_PORT, request.url().port());
27+
28+
return builder.build();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 io.opentelemetry.api.common.AttributeKey;
9+
10+
public class WebsocketAttributes {
11+
private WebsocketAttributes() {}
12+
13+
public static AttributeKey<Long> MESSAGE_SIZE = AttributeKey.longKey("websocket.message.size");
14+
public static AttributeKey<String> MESSAGE_TYPE =
15+
AttributeKey.stringKey("websocket.message.type");
16+
}

0 commit comments

Comments
 (0)