Skip to content

Commit

Permalink
Create a SpanNameProvider that includes both operation and collection…
Browse files Browse the repository at this point in the history
… name (#13)
  • Loading branch information
jamal authored and malafeev committed Aug 23, 2019
1 parent 4473666 commit 549774f
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Span buildSpan(CommandStartedEvent event) {
}

Tracer.SpanBuilder spanBuilder = tracer
.buildSpan(mongoSpanNameProvider.generateName(event.getCommandName()))
.buildSpan(mongoSpanNameProvider.generateName(event))
.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 @@ -13,9 +13,11 @@
*/
package io.opentracing.contrib.mongo.common.providers;

import com.mongodb.event.CommandStartedEvent;

public interface MongoSpanNameProvider {

String NO_OPERATION = "unknown";

String generateName(final String operationName);
String generateName(CommandStartedEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*/
package io.opentracing.contrib.mongo.common.providers;

import com.mongodb.event.CommandEvent;
import com.mongodb.event.CommandStartedEvent;

public class NoopSpanNameProvider implements MongoSpanNameProvider {

@Override
public String generateName(String operationName) {
return ((operationName == null) ? NO_OPERATION : operationName);
public String generateName(CommandStartedEvent event) {
return event != null && event.getCommandName() != null ?
event.getCommandName() : NO_OPERATION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2017-2019 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.common.providers;

import com.mongodb.event.CommandStartedEvent;
import org.bson.BsonDocument;

public class OperationCollectionSpanNameProvider extends NoopSpanNameProvider {

@Override
public String generateName(CommandStartedEvent event) {
if (event == null || event.getCommand() == null) {
return NO_OPERATION;
}
final BsonDocument cmd = event.getCommand();
String col = cmd.getString(cmd.getFirstKey()).getValue();
return super.generateName(event) + " " + col;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
package io.opentracing.contrib.mongo.common.providers;

public class PrefixSpanNameProvider implements MongoSpanNameProvider {
import com.mongodb.event.CommandStartedEvent;

private static final String NO_OPERATION = "unknown";
public class PrefixSpanNameProvider implements MongoSpanNameProvider {

private final String prefix;

Expand All @@ -24,8 +24,9 @@ public PrefixSpanNameProvider(String prefix) {
}

@Override
public String generateName(String operationName) {
return ((prefix == null) ? "" : prefix)
+ ((operationName == null) ? NO_OPERATION : operationName);
public String generateName(CommandStartedEvent event) {
final String operationName = event != null && event.getCommandName() != null ?
event.getCommandName() : NO_OPERATION;
return ((prefix == null) ? "" : prefix) + operationName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public void commandFailed(CommandFailedEvent event, Span span) { }
public void testDefault() {
span = withoutProvider.buildSpan(event);
MockSpan mockSpan = (MockSpan) span;
assertEquals(mockSpan.operationName(), operationName.generateName(event.getCommandName()));
assertEquals(mockSpan.operationName(), operationName.generateName(event));
}

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@

import static org.junit.Assert.assertEquals;

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 org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.Test;

public class NoopSpanNameProviderTest {

private final MongoSpanNameProvider provider = new NoopSpanNameProvider();

private final CommandStartedEvent TEST_EVENT = new CommandStartedEvent(1,
new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())),
"database-name", "insert",
new BsonDocument().append("insert", new BsonString("collection-name")));

@Test
public void testOperationNameExists() {
assertEquals("insert", provider.generateName("insert"));
assertEquals("insert", provider.generateName(TEST_EVENT));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2017-2019 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.common.providers;

import static org.junit.Assert.assertEquals;

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 org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.Test;

public class OperationCollectionSpanNameProviderTest {

private final MongoSpanNameProvider provider = new OperationCollectionSpanNameProvider();

private final CommandStartedEvent TEST_EVENT = new CommandStartedEvent(1,
new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())),
"database-name", "insert",
new BsonDocument().append("insert", new BsonString("collection-name")));

@Test
public void testOperationNameExists() {
assertEquals("insert collection-name", provider.generateName(TEST_EVENT));
}

@Test
public void testNullOperationName() {
assertEquals("unknown", provider.generateName(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@

import static org.junit.Assert.assertEquals;

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 org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.Test;

public class PrefixSpanNameProviderTest {

private final MongoSpanNameProvider provider = new PrefixSpanNameProvider("mongo.");

private final CommandStartedEvent TEST_EVENT = new CommandStartedEvent(1,
new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())),
"database-name", "insert",
new BsonDocument().append("insert", new BsonString("collection-name")));

@Test
public void testOperationNameExists() {
assertEquals("mongo.insert", provider.generateName("insert"));
assertEquals("mongo.insert", provider.generateName(TEST_EVENT));
}

@Test
Expand All @@ -33,6 +45,6 @@ public void testNullOperationName() {

@Test
public void testNullPrefixName() {
assertEquals("insert", new PrefixSpanNameProvider(null).generateName("insert"));
assertEquals("insert", new PrefixSpanNameProvider(null).generateName(TEST_EVENT));
}
}

0 comments on commit 549774f

Please sign in to comment.