-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
438 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...-redis-common/src/main/java/io/opentracing/contrib/redis/common/TracingConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...is-common/src/test/java/io/opentracing/contrib/redis/common/TracingConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.