Skip to content

Commit

Permalink
limit Redis keys length (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
malafeev committed Nov 5, 2018
1 parent e4a4a2b commit ab6e42e
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 452 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ Tracer tracer = ...
// Optionally register tracer with GlobalTracer
GlobalTracer.register(tracer);

// Create TracingConfiguration
TracingConfiguration tracingConfiguration = new TracingConfiguration.Builder(tracer).build();

```

### Jedis
```java

// Create Tracing Jedis
Jedis jedis = new TracingJedis(tracer, false);
Jedis jedis = new TracingJedis(tracingConfiguration);

jedis.set("foo", "bar");
String value = jedis.get("foo");
Expand All @@ -72,7 +75,7 @@ Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));

// Create Tracing Jedis Cluster
JedisCluster jc = new TracingJedisCluster(jedisClusterNodes);
JedisCluster jc = new TracingJedisCluster(jedisClusterNodes, tracingConfiguration);
jc.set("foo", "bar");
String value = jc.get("foo");

Expand All @@ -86,7 +89,7 @@ poolConfig.setMaxIdle(10);
poolConfig.setTestOnBorrow(false);

// Create Tracing Jedis Pool
JedisPool pool = new TracingJedisPool(poolConfig, "127.0.0.1", 6379, tracer, false);
JedisPool pool = new TracingJedisPool(poolConfig, "127.0.0.1", 6379, tracingConfiguration);

try (Jedis jedis = pool.getResource()) {
// jedis will be automatically closed and returned to the pool at the end of "try" block
Expand All @@ -98,7 +101,7 @@ try (Jedis jedis = pool.getResource()) {
### Jedis Sentinel Pool
```java
// Create Tracing Jedis Sentinel Pool
JedisSentinelPool pool = new TracingJedisSentinelPool(tracer, false, MASTER_NAME, sentinels, poolConfig);
JedisSentinelPool pool = new TracingJedisSentinelPool(tracingConfiguration, 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
Expand All @@ -108,10 +111,16 @@ try (Jedis jedis = pool.getResource()) {
```

### Jedis Span Name
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.
By default, span names are set to the operation performed by the Jedis object.
To customize the span name, provide a Function to the TracingConfiguration 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.
```java
TracingConfiguration tracingConfiguration = new TracingConfiguration.Builder(tracer)
.withSpanNameProvider(RedisSpanNameProvider.PREFIX_OPERATION_NAME("redis."))
.build();
//Create Tracing Jedis with custom span name
Jedis jedis = new TracingJedis(tracer, false, RedisSpanNameProvider.PREFIX_OPERATION_NAME("redis.");
Jedis jedis = new TracingJedis(tracingConfiguration);
jedis.set("foo", "bar");
//Span name is now set to "redis.set"

Expand All @@ -126,7 +135,7 @@ RedisClient client = RedisClient.create("redis://localhost");

// Decorate StatefulRedisConnection with TracingStatefulRedisConnection
StatefulRedisConnection<String, String> connection =
new TracingStatefulRedisConnection(client.connect(), tracer, false);
new TracingStatefulRedisConnection(client.connect(), tracingConfiguration);

// Get sync redis commands
RedisCommands<String, String> commands = connection.sync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2017-2018 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.redis.common;

import io.opentracing.Tracer;
import java.util.function.Function;

public class TracingConfiguration {
static final int DEFAULT_KEYS_MAX_LENGTH = 100;
private final Tracer tracer;
private final boolean traceWithActiveSpanOnly;
private final int keysMaxLength;
private final Function<String, String> spanNameProvider;

private TracingConfiguration(Tracer tracer, boolean traceWithActiveSpanOnly, int keysMaxLength,
Function<String, String> spanNameProvider) {
this.tracer = tracer;
this.traceWithActiveSpanOnly = traceWithActiveSpanOnly;
this.keysMaxLength = keysMaxLength;
this.spanNameProvider = spanNameProvider;
}

public Tracer getTracer() {
return tracer;
}

public boolean isTraceWithActiveSpanOnly() {
return traceWithActiveSpanOnly;
}

public int getKeysMaxLength() {
return keysMaxLength;
}

public Function<String, String> getSpanNameProvider() {
return spanNameProvider;
}

public static class Builder {
private final Tracer tracer;
private boolean traceWithActiveSpanOnly;
private int keysMaxLength = DEFAULT_KEYS_MAX_LENGTH;
private Function<String, String> spanNameProvider = RedisSpanNameProvider.OPERATION_NAME;

public Builder(Tracer tracer) {
this.tracer = tracer;
}

/**
* @param traceWithActiveSpanOnly if <code>true</code> then create new spans only if there is
* an active span
*/
public Builder traceWithActiveSpanOnly(boolean traceWithActiveSpanOnly) {
this.traceWithActiveSpanOnly = traceWithActiveSpanOnly;
return this;
}

/**
* Customize the span name, default is operation name ({@link RedisSpanNameProvider#OPERATION_NAME})
*
* @param spanNameProvider function to customize the span name
*/
public Builder withSpanNameProvider(Function<String, String> spanNameProvider) {
this.spanNameProvider = spanNameProvider;
return this;
}

/**
* Limit number of keys to add to span (default is 100)
*
* @param keysMaxLength max keys length to add to span
*/
public Builder withKeysMaxLength(int keysMaxLength) {
this.keysMaxLength = keysMaxLength;
return this;
}

public TracingConfiguration build() {
if (spanNameProvider == null) {
spanNameProvider = RedisSpanNameProvider.OPERATION_NAME;
}
if (keysMaxLength <= 0) {
keysMaxLength = DEFAULT_KEYS_MAX_LENGTH;
}
return new TracingConfiguration(tracer, traceWithActiveSpanOnly, keysMaxLength,
spanNameProvider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,13 @@ public class TracingHelper {
private final Tracer tracer;
private final boolean traceWithActiveSpanOnly;
private final Function<String, String> spanNameProvider;
private final int maxKeysLength;

public TracingHelper(Tracer tracer, boolean traceWithActiveSpanOnly) {
this.tracer = tracer;
this.traceWithActiveSpanOnly = traceWithActiveSpanOnly;
this.spanNameProvider = RedisSpanNameProvider.OPERATION_NAME;
}

public TracingHelper(Tracer tracer, boolean traceWithActiveSpanOnly,
Function<String, String> spanNameProvider) {
this.tracer = tracer;
this.traceWithActiveSpanOnly = traceWithActiveSpanOnly;
this.spanNameProvider = spanNameProvider;
public TracingHelper(TracingConfiguration tracingConfiguration) {
this.tracer = tracingConfiguration.getTracer();
this.traceWithActiveSpanOnly = tracingConfiguration.isTraceWithActiveSpanOnly();
this.spanNameProvider = tracingConfiguration.getSpanNameProvider();
this.maxKeysLength = tracingConfiguration.getKeysMaxLength();
}

private static SpanBuilder builder(String operationName, Tracer tracer,
Expand Down Expand Up @@ -97,9 +92,16 @@ public Span buildSpan(String operationName, Object[] keys) {
if (traceWithActiveSpanOnly && getNullSafeTracer(tracer).activeSpan() == null) {
return NoopSpan.INSTANCE;
} else {
return builder(operationName, tracer, spanNameProvider).withTag("keys", Arrays.toString(keys))
.start();
return builder(operationName, tracer, spanNameProvider).withTag("keys",
Arrays.toString(limitKeys(keys))).start();
}
}

Object[] limitKeys(Object[] keys) {
if (keys != null && keys.length > maxKeysLength) {
return Arrays.copyOfRange(keys, 0, maxKeysLength);
}
return keys;
}

public static void onError(Throwable throwable, Span span) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2017-2018 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.redis.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import io.opentracing.Tracer;
import io.opentracing.mock.MockTracer;
import java.util.function.Function;
import org.junit.Test;

public class TracingConfigurationTest {

@Test
public void testDefaultValues() {
Tracer tracer = new MockTracer();
TracingConfiguration configuration = new TracingConfiguration.Builder(tracer).build();
assertFalse(configuration.isTraceWithActiveSpanOnly());
assertEquals(TracingConfiguration.DEFAULT_KEYS_MAX_LENGTH, configuration.getKeysMaxLength());
assertSame(tracer, configuration.getTracer());
assertSame(RedisSpanNameProvider.OPERATION_NAME, configuration.getSpanNameProvider());
}

@Test
public void test() {
Tracer tracer = new MockTracer();
Function<String, String> spanNameProvider = RedisSpanNameProvider.PREFIX_OPERATION_NAME("test");

TracingConfiguration configuration = new TracingConfiguration.Builder(tracer)
.withKeysMaxLength(10)
.traceWithActiveSpanOnly(true)
.withSpanNameProvider(spanNameProvider)
.build();

assertTrue(configuration.isTraceWithActiveSpanOnly());
assertEquals(10, configuration.getKeysMaxLength());
assertSame(tracer, configuration.getTracer());
assertSame(spanNameProvider, configuration.getSpanNameProvider());
}

@Test
public void testWrongValues() {
TracingConfiguration configuration = new TracingConfiguration.Builder(null)
.withKeysMaxLength(-20) // will be reverted to TracingConfiguration.DEFAULT_KEYS_MAX_LENGTH
.withSpanNameProvider(null) // will be reverted to RedisSpanNameProvider.OPERATION_NAME
.build();

assertFalse(configuration.isTraceWithActiveSpanOnly());
assertEquals(TracingConfiguration.DEFAULT_KEYS_MAX_LENGTH, configuration.getKeysMaxLength());
assertNull(configuration.getTracer());
assertSame(RedisSpanNameProvider.OPERATION_NAME, configuration.getSpanNameProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public class TracingHelperTest {
@Before
public void setUp() {
prefixSpanName = RedisSpanNameProvider.PREFIX_OPERATION_NAME(prefix);
helperWithProvider = new TracingHelper(mockTracer, false, prefixSpanName);
helperWithoutProvider = new TracingHelper(mockTracer, false);
helperWithProvider = new TracingHelper(
new TracingConfiguration.Builder(mockTracer).withSpanNameProvider(prefixSpanName).build());
helperWithoutProvider = new TracingHelper(new TracingConfiguration.Builder(mockTracer).build());
}

@Test
Expand All @@ -51,4 +52,12 @@ public void testDefault() {
span = (MockSpan) (helperWithoutProvider.buildSpan("get"));
assertEquals("get", span.operationName());
}

@Test
public void limitKeys() {
TracingHelper helper = new TracingHelper(new TracingConfiguration.Builder(mockTracer)
.withKeysMaxLength(2).build());
Object[] keys = {"one", "two", "three"};
assertEquals(2, helper.limitKeys(keys).length);
}
}
Loading

0 comments on commit ab6e42e

Please sign in to comment.