Merge pull request #72 from gpolaert/mongo_norm_v2

improving the normalizer
This commit is contained in:
Emanuele Palazzetti 2017-07-31 11:12:05 +02:00 committed by GitHub
commit 3d83a283dd
1 changed files with 31 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
@ -18,7 +19,8 @@ import org.jboss.byteman.rule.Rule;
@Slf4j
public class MongoHelper extends DDAgentTracingHelper<MongoClientOptions.Builder> {
private static final List<String> WHILDCARD_FIELDS = Arrays.asList("ordered", "insert");
private static final List<String> WHILDCARD_FIELDS =
Arrays.asList("ordered", "insert", "count", "find");
private static final BsonValue HIDDEN_CAR = new BsonString("?");
public MongoHelper(final Rule rule) {
@ -48,27 +50,45 @@ public class MongoHelper extends DDAgentTracingHelper<MongoClientOptions.Builder
public void decorate(final Span span, final CommandStartedEvent event) {
try {
final BsonDocument normalized = new BsonDocument();
norm(event.getCommand(), normalized);
final BsonDocument normalized = norm(event.getCommand());
span.setTag(DDTags.RESOURCE_NAME, normalized.toString());
span.setOperationName("mongo.cmd");
} catch (final Throwable e) {
log.warn("Couldn't decorate the mongo query: " + e.getMessage(), e);
}
}
private void norm(final BsonDocument origin, final BsonDocument normalized) {
private BsonDocument norm(final BsonDocument origin) {
final BsonDocument normalized = new BsonDocument();
for (final Map.Entry<String, BsonValue> entry : origin.entrySet()) {
if (WHILDCARD_FIELDS.contains(entry.getKey())) {
normalized.put(entry.getKey(), entry.getValue());
} else if (entry.getValue().isDocument()) {
final BsonDocument child = new BsonDocument();
normalized.put(entry.getKey(), child);
norm(entry.getValue().asDocument(), child);
} else {
normalized.put(entry.getKey(), HIDDEN_CAR);
final BsonValue child = norm(entry.getValue());
normalized.put(entry.getKey(), child);
}
}
return normalized;
}
private BsonValue norm(final BsonArray origin) {
final BsonArray normalized = new BsonArray();
for (final BsonValue value : origin) {
final BsonValue child = norm(value);
normalized.add(child);
}
return normalized;
}
private BsonValue norm(final BsonValue origin) {
final BsonValue normalized;
if (origin.isDocument()) {
normalized = norm(origin.asDocument());
} else if (origin.isArray()) {
normalized = norm(origin.asArray());
} else {
normalized = HIDDEN_CAR;
}
return normalized;
}
}