OpenTracing instrumentation for Redis Client
- Java 8
pom.xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-redis-jedis</artifactId>
<version>0.0.2</version>
</dependency>
pom.xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-redis-lettuce</artifactId>
<version>0.0.2</version>
</dependency>
// Instantiate tracer
Tracer tracer = ...
// Optionally register tracer with GlobalTracer
GlobalTracer.register(tracer);
// Create Tracing Jedis
Jedis jedis = new TracingJedis(tracer, false);
jedis.set("foo", "bar");
String value = jedis.get("foo");
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
// Create Tracing Jedis Cluster
JedisCluster jc = new TracingJedisCluster(jedisClusterNodes);
jc.set("foo", "bar");
String value = jc.get("foo");
// Configure the pool
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(10);
poolConfig.setTestOnBorrow(false);
// Create Tracing Jedis Pool
JedisPool pool = new TracingJedisPool(poolConfig, "127.0.0.1", 6379, tracer, false);
try (Jedis jedis = pool.getResource()) {
// jedis will be automatically closed and returned to the pool at the end of "try" block
jedis.set("foo", "bar");
String value = jedis.get("foo");
}
// Create Tracing Jedis Sentinel Pool
JedisSentinelPool pool = new TracingJedisSentinelPool(tracer, false, MASTER_NAME, sentinels, poolConfig);
try (Jedis jedis = pool.getResource()) {
// jedis will be automatically closed and returned to the pool at the end of "try" block
jedis.set("foo", "bar"));
String value = jedis.get("foo"));
}
By default, span names are set to the operation performed by the Jedis object. To customize the span name, provide a Function to the Jedis object that alters the span name. If a function is not provided, the span name will remain the default. Refer to the RedisSpanNameProvider class for a function that prefixes the operation name.
//Create Tracing Jedis with custom span name
Jedis jedis = new TracingJedis(tracer, false, RedisSpanNameProvider.PREFIX_OPERATION_NAME("redis.");
jedis.set("foo", "bar");
//Span name is now set to "redis.set"
// Create client
RedisClient client = RedisClient.create("redis://localhost");
// Decorate StatefulRedisConnection with TracingStatefulRedisConnection
StatefulRedisConnection<String, String> connection =
new TracingStatefulRedisConnection(client.connect(), tracer, false);
// Get sync redis commands
RedisCommands<String, String> commands = connection.sync();
// Get async redis commands
RedisAsyncCommands<String, String> commandsAsync = connection.async();