Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Span Name #4

Merged
merged 15 commits into from
Jul 5, 2018
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ MongoClient mongoClient = new TracingAsyncMongoClient(tracer, ...);

```

### Mongo Span Name
By default, span names are set to the operation performed by the Mongo client. To customize the span name, provide a Function to the client that alters the span name. If a function is not provided, the span name will remain the default. Refer to the MongoSpanNameProvider class for a function that prefixes the operation name.

```java
//Create TracingMongoClient with custom span name
TracingMongoClient client = new TracingMongoClient(tracer, replicaSetAddresses, credentials, clientOptions, MongoSpanNameProvider.PREFIX_OPERATION_NAME("mongo."));
Document doc = new Document();
client.getDatabase("db").getCollection("collection).insertOne(doc);
//Span name is now set to "mongo.insert"

```

[ci-img]: https://travis-ci.org/opentracing-contrib/java-mongo-driver.svg?branch=master
[ci]: https://travis-ci.org/opentracing-contrib/java-mongo-driver
[maven-img]: https://img.shields.io/maven-central/v/io.opentracing.contrib/opentracing-mongo-driver.svg
Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</issueManagement>

<properties>
<java.version>1.7</java.version>
<java.version>1.8</java.version>
Copy link

@malafeev malafeev Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MongoDriver support java 1.6+
So I think we cannot upgrade java version until MongoDriver will require it.

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>

Expand All @@ -77,6 +77,13 @@
<version>${mongo.driver.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.19.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.mongo;

import java.util.function.Function;

public class MongoSpanNameProvider {

private static final String NO_OPERATION = "unknown";

public static Function<String, String> OPERATION_NAME = (operationName) -> ((operationName == null) ? NO_OPERATION : operationName);

public static Function<String, String> PREFIX_OPERATION_NAME(final String prefix) {
return (operationName) ->
((prefix == null) ? "" : prefix)
+ ((operationName == null) ? NO_OPERATION : operationName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.opentracing.contrib.mongo;

import java.util.function.Function;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
Expand All @@ -36,13 +37,20 @@ public class TracingCommandListener implements CommandListener {

static final String COMPONENT_NAME = "java-mongo";
private final Tracer tracer;
private Function<String, String> mongoSpanNameProvider;
/**
* Cache for (request id, span) pairs
*/
private final Map<Integer, Span> cache = new ConcurrentHashMap<>();

TracingCommandListener(Tracer tracer) {
this.tracer = tracer;
mongoSpanNameProvider = MongoSpanNameProvider.OPERATION_NAME;
}

TracingCommandListener(Tracer tracer, Function<String, String> customNameProvider) {
this.tracer = tracer;
mongoSpanNameProvider = customNameProvider;
}

@Override
Expand All @@ -68,8 +76,8 @@ public void commandFailed(CommandFailedEvent event) {
}
}

private Span buildSpan(CommandStartedEvent event) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(event.getCommandName())
Span buildSpan(CommandStartedEvent event) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(mongoSpanNameProvider.apply(event.getCommandName()))
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

Span span = spanBuilder.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.opentracing.contrib.mongo;


import java.util.function.Function;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
Expand Down Expand Up @@ -71,7 +72,6 @@ public TracingMongoClient(Tracer tracer, final ServerAddress addr,
tracer)).build());
}


public TracingMongoClient(Tracer tracer, final ServerAddress addr,
final MongoCredential credential, final MongoClientOptions options) {
super(addr, credential,
Expand All @@ -94,6 +94,12 @@ public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
tracer)).build());
}

public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
final MongoClientOptions options, Function<String, String> spanNameProvider) {
super(seeds, MongoClientOptions.builder(options).addCommandListener(new TracingCommandListener(
tracer, spanNameProvider)).build());
}

@Deprecated
public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
final List<MongoCredential> credentialsList, final MongoClientOptions options) {
Expand All @@ -102,6 +108,13 @@ public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
tracer)).build());
}

public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
final List<MongoCredential> credentialsList, final MongoClientOptions options, Function<String, String> spanNameProvider) {
super(seeds, credentialsList,
MongoClientOptions.builder(options).addCommandListener(new TracingCommandListener(
tracer, spanNameProvider)).build());
}

public TracingMongoClient(Tracer tracer, final List<ServerAddress> seeds,
final MongoCredential credential, final MongoClientOptions options) {
super(seeds, credential,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.mongo;

import java.util.function.Function;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MongoSpanNameProviderTest {

Function<String, String> prefixSpanName;
String actual = "";

@Before
public void setUp() {
prefixSpanName = MongoSpanNameProvider.PREFIX_OPERATION_NAME("mongo.");
}

@Test
public void testInsertPrefix() {
actual = prefixSpanName.apply("insert");
assertEquals("mongo.insert", actual);
}

@Test
public void testFindPrefix() {
actual = prefixSpanName.apply("find");
assertEquals("mongo.find", actual);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.mongo;

import java.util.function.Function;

import org.bson.BsonDocument;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterId;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ServerId;
import com.mongodb.event.CommandStartedEvent;

import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;

import static org.junit.Assert.assertEquals;

public class TracingCommandListenerTest {

Tracer tracer = new MockTracer();
private String prefix = "mongo.";
Function<String, String> prefixSpanName;
Function<String, String> operationName;
TracingCommandListener withProvider;
TracingCommandListener withoutProvider;
CommandStartedEvent event;
Span span;

@Before
public void setUp() {
operationName = MongoSpanNameProvider.OPERATION_NAME;
prefixSpanName = MongoSpanNameProvider.PREFIX_OPERATION_NAME(prefix);
withProvider = new TracingCommandListener(tracer, prefixSpanName);
withoutProvider = new TracingCommandListener(tracer);
event = new CommandStartedEvent(
1
, new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()))
, "databaseName"
, "commandName"
, new BsonDocument()
);
}

@Test
public void testDefault() {
span = withoutProvider.buildSpan(event);
MockSpan mockSpan = (MockSpan) span;
assertEquals(mockSpan.operationName(), operationName.apply(event.getCommandName()));
}

@Test
public void testPrefix() {
span = withProvider.buildSpan(event);
MockSpan mockSpan = (MockSpan) span;
assertEquals(mockSpan.operationName(), prefixSpanName.apply(event.getCommandName()));
}
}