-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add mongo commands to otel span attributes #40191
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
92 changes: 92 additions & 0 deletions
92
...-client/runtime/src/main/java/io/quarkus/mongodb/tracing/MongoTracingCommandListener.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,92 @@ | ||
package io.quarkus.mongodb.tracing; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import com.mongodb.event.*; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
|
||
public class MongoTracingCommandListener implements CommandListener { | ||
private static final org.jboss.logging.Logger LOGGER = Logger.getLogger(MongoTracingCommandListener.class); | ||
private static final String KEY = "mongodb.command"; | ||
private final Map<Integer, ContextEvent> requestMap; | ||
private final Instrumenter<CommandStartedEvent, Void> instrumenter; | ||
|
||
private record ContextEvent(Context context, CommandStartedEvent commandEvent) { | ||
} | ||
|
||
@Inject | ||
public MongoTracingCommandListener(OpenTelemetry openTelemetry) { | ||
requestMap = new ConcurrentHashMap<>(); | ||
SpanNameExtractor<CommandStartedEvent> spanNameExtractor = CommandEvent::getCommandName; | ||
instrumenter = Instrumenter.<CommandStartedEvent, Void> builder( | ||
openTelemetry, "quarkus-mongodb-client", spanNameExtractor) | ||
.addAttributesExtractor(new CommandEventAttrExtractor()) | ||
.buildInstrumenter(SpanKindExtractor.alwaysClient()); | ||
LOGGER.debugf("MongoTracingCommandListener created"); | ||
} | ||
|
||
@Override | ||
public void commandStarted(CommandStartedEvent event) { | ||
LOGGER.tracef("commandStarted event %s", event.getCommandName()); | ||
|
||
Context parentContext = Context.current(); | ||
if (instrumenter.shouldStart(parentContext, event)) { | ||
Context context = instrumenter.start(parentContext, event); | ||
requestMap.put(event.getRequestId(), new ContextEvent(context, event)); | ||
} | ||
} | ||
|
||
@Override | ||
public void commandSucceeded(CommandSucceededEvent event) { | ||
LOGGER.tracef("commandSucceeded event %s", event.getCommandName()); | ||
ContextEvent contextEvent = requestMap.remove(event.getRequestId()); | ||
if (contextEvent != null) { | ||
instrumenter.end(contextEvent.context(), contextEvent.commandEvent(), null, null); | ||
} | ||
} | ||
|
||
@Override | ||
public void commandFailed(CommandFailedEvent event) { | ||
LOGGER.tracef("commandFailed event %s", event.getCommandName()); | ||
ContextEvent contextEvent = requestMap.remove(event.getRequestId()); | ||
if (contextEvent != null) { | ||
instrumenter.end( | ||
contextEvent.context(), | ||
contextEvent.commandEvent(), | ||
null, | ||
event.getThrowable()); | ||
} | ||
} | ||
|
||
private static class CommandEventAttrExtractor implements AttributesExtractor<CommandStartedEvent, Void> { | ||
@Override | ||
public void onStart(AttributesBuilder attributesBuilder, | ||
Context context, | ||
CommandStartedEvent commandStartedEvent) { | ||
attributesBuilder.put(KEY, commandStartedEvent.getCommand().toJson()); | ||
} | ||
|
||
@Override | ||
public void onEnd(AttributesBuilder attributesBuilder, | ||
Context context, | ||
CommandStartedEvent commandStartedEvent, | ||
@Nullable Void unused, | ||
@Nullable Throwable throwable) { | ||
|
||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...ent/runtime/src/test/java/io/quarkus/mongodb/tracing/MongoTracingCommandListenerTest.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,103 @@ | ||
package io.quarkus.mongodb.tracing; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
|
||
import org.bson.BsonDocument; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.mongodb.ServerAddress; | ||
import com.mongodb.connection.ClusterId; | ||
import com.mongodb.connection.ConnectionDescription; | ||
import com.mongodb.connection.ServerId; | ||
import com.mongodb.event.CommandFailedEvent; | ||
import com.mongodb.event.CommandStartedEvent; | ||
import com.mongodb.event.CommandSucceededEvent; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
|
||
class MongoTracingCommandListenerTest { | ||
private ConnectionDescription connDescr; | ||
private MongoTracingCommandListener listener; | ||
private BsonDocument command; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
connDescr = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())); | ||
listener = new MongoTracingCommandListener(OpenTelemetry.noop()); | ||
command = new BsonDocument(); | ||
} | ||
|
||
@Test | ||
void commandStarted() { | ||
var startEvent = new CommandStartedEvent( | ||
null, | ||
1L, | ||
10, | ||
connDescr, | ||
"db", | ||
"find", | ||
command); | ||
assertThatNoException().isThrownBy(() -> listener.commandStarted(startEvent)); | ||
|
||
CommandSucceededEvent successEvent = new CommandSucceededEvent(null, | ||
startEvent.getOperationId(), | ||
startEvent.getRequestId(), | ||
connDescr, | ||
startEvent.getDatabaseName(), | ||
startEvent.getCommandName(), | ||
startEvent.getCommand(), | ||
10L); | ||
assertThatNoException().isThrownBy(() -> listener.commandSucceeded(successEvent)); | ||
} | ||
|
||
@Test | ||
void commandSucceeded() { | ||
CommandSucceededEvent cmd = new CommandSucceededEvent(null, | ||
1L, | ||
10, | ||
connDescr, | ||
"db", | ||
"find", | ||
command, | ||
10L); | ||
assertThatNoException().isThrownBy(() -> listener.commandSucceeded(cmd)); | ||
} | ||
|
||
@Test | ||
void commandFailed() { | ||
var startedEvent = new CommandStartedEvent( | ||
null, | ||
1L, | ||
10, | ||
connDescr, | ||
"db", | ||
"find", | ||
command); | ||
assertThatNoException().isThrownBy(() -> listener.commandStarted(startedEvent)); | ||
|
||
CommandFailedEvent failedEvent = new CommandFailedEvent(null, | ||
1L, | ||
10, | ||
connDescr, | ||
"db", | ||
"find", | ||
10L, | ||
new IllegalStateException("command failed")); | ||
assertThatNoException().isThrownBy(() -> listener.commandFailed(failedEvent)); | ||
} | ||
|
||
@Test | ||
void commandFailedNoEvent() { | ||
CommandFailedEvent cmd = new CommandFailedEvent(null, | ||
1L, | ||
10, | ||
connDescr, | ||
"db", | ||
"find", | ||
10L, | ||
new IllegalStateException("command failed")); | ||
assertThatNoException().isThrownBy(() -> listener.commandFailed(cmd)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned with the amount of data you are storing here... can you give an example of one of this events?
Can you just store what you need later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brunobat it is not big, just description of mongo connection, db name and command. You can inspect it by using
BookResourceTest
. But I could create another record to just save command name and the command itself, if you preferThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brunobat I've moved the implementation to opentelemetry extension. Is this enough for a new PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is but there are also other improvements needed:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brunobat Opened #40472
I'm not sure about additional IT tests. There is already a test for for reactive endpoint with assertion for attributes: