more robust and recursive

This commit is contained in:
Guillaume Polaert 2017-07-28 13:47:56 +02:00
parent 58cb653d41
commit dad74d0c63
1 changed files with 30 additions and 27 deletions

View File

@ -19,7 +19,8 @@ import org.jboss.byteman.rule.Rule;
@Slf4j @Slf4j
public class MongoHelper extends DDAgentTracingHelper<MongoClientOptions.Builder> { 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("?"); private static final BsonValue HIDDEN_CAR = new BsonString("?");
public MongoHelper(final Rule rule) { public MongoHelper(final Rule rule) {
@ -49,43 +50,45 @@ public class MongoHelper extends DDAgentTracingHelper<MongoClientOptions.Builder
public void decorate(final Span span, final CommandStartedEvent event) { public void decorate(final Span span, final CommandStartedEvent event) {
try { try {
final BsonDocument normalized = new BsonDocument(); final BsonDocument normalized = norm(event.getCommand());
norm(event.getCommand(), normalized);
span.setTag(DDTags.RESOURCE_NAME, normalized.toString()); span.setTag(DDTags.RESOURCE_NAME, normalized.toString());
} catch (final Throwable e) { } catch (final Throwable e) {
log.warn("Couldn't decorate the mongo query: " + e.getMessage(), 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()) { for (final Map.Entry<String, BsonValue> entry : origin.entrySet()) {
if (WHILDCARD_FIELDS.contains(entry.getKey())) { if (WHILDCARD_FIELDS.contains(entry.getKey())) {
// Wildcard fields
normalized.put(entry.getKey(), entry.getValue()); normalized.put(entry.getKey(), entry.getValue());
} else if (entry.getValue().isDocument()) { } else {
// Nested documents final BsonValue child = norm(entry.getValue());
final BsonDocument child = new BsonDocument();
normalized.put(entry.getKey(), child); normalized.put(entry.getKey(), child);
norm(entry.getValue().asDocument(), child); }
} else if (entry.getValue().isArray()) { }
// Nested arrays (works only for 1-depth) TODO:recursive pattern? return normalized;
for (final BsonValue subItemArray : entry.getValue().asArray()) { }
final BsonArray array = new BsonArray();
normalized.put(entry.getKey(), array); private BsonValue norm(final BsonArray origin) {
if (subItemArray.isDocument()) { final BsonArray normalized = new BsonArray();
// Nested array elements are documents for (final BsonValue value : origin) {
final BsonDocument child = new BsonDocument(); final BsonValue child = norm(value);
array.add(child); normalized.add(child);
norm(subItemArray.asDocument(), 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 { } else {
array.add(HIDDEN_CAR); normalized = HIDDEN_CAR;
}
}
} else {
// Hide the value
normalized.put(entry.getKey(), HIDDEN_CAR);
}
} }
return normalized;
} }
} }