-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathConnection_Instrumentation.java
90 lines (73 loc) · 2.72 KB
/
Connection_Instrumentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package redis.clients.jedis;
import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.datastore.DatastoreVendor;
import com.newrelic.api.agent.DatastoreParameters;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.NewField;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import redis.clients.jedis.commands.ProtocolCommand;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
@SuppressWarnings({ "ResultOfMethodCallIgnored", "WeakerAccess", "unused" }) // Weaver.callOriginal(), matching signatures
@Weave(type = MatchType.BaseClass, originalName = "redis.clients.jedis.Connection")
public abstract class Connection_Instrumentation {
@NewField
private long db = 0;
abstract HostAndPort getHostAndPort();
public void disconnect() {
db = 0;
Weaver.callOriginal();
}
@Trace
public void sendCommand(final ProtocolCommand cmd, final byte[]... args) {
Weaver.callOriginal();
if (args != null && args.length > 0) {
updateDbIndex(cmd, new String(args[0], StandardCharsets.UTF_8));
}
}
@Trace
public void sendCommand(final ProtocolCommand cmd, final String... args) {
Weaver.callOriginal();
if (args != null && args.length > 0) {
updateDbIndex(cmd, args[0]);
}
}
@Trace(leaf = true)
public void sendCommand(final CommandArguments args) {
Weaver.callOriginal();
ProtocolCommand cmd = args.getCommand();
reportMethodAsExternal(cmd);
}
private void updateDbIndex(ProtocolCommand cmd, String arg0) {
try {
if (cmd == Protocol.Command.SELECT) {
db = Long.parseLong(arg0);
}
} catch (Throwable t) {
AgentBridge.getAgent().getLogger().log(Level.FINER, t, "Unable to set DB index");
}
}
private void reportMethodAsExternal(ProtocolCommand command) {
String operation = "unknown";
try {
operation = new String(command.getRaw(), Protocol.CHARSET).toLowerCase();
} catch (Exception ignored) {
}
NewRelic.getAgent().getTracedMethod().reportAsExternal(DatastoreParameters
.product(DatastoreVendor.Redis.name())
.collection(null)
.operation(operation)
.instance(getHostAndPort().getHost(), getHostAndPort().getPort())
.databaseName(String.valueOf(db))
.build());
}
}