-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[JAVA-6159] Micrometer feedback #1940
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
Open
nhachicha
wants to merge
9
commits into
mongodb:main
Choose a base branch
from
nhachicha:nh/micrometer_spring_feedback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4912105
Split Micrometer observation into separate operation and command types
nhachicha 815a250
Move high-cardinality tags from low-cardinality to high-cardinality
nhachicha 27b3a97
Remove QUERY_TEXT_MAX_LENGTH from Observation.Context
nhachicha 52efa25
Use custom MongodbContext instead of generic SenderContext
nhachicha bf91627
Add DefaultMongodbObservationConvention and move tag production out o…
nhachicha da4a3c1
Fixes JAVA-6094
nhachicha cf4660c
Open and close observation scopes for sync driver context propagation
nhachicha db5387c
Fixing tests
nhachicha 32014c2
Stop modifying parent operation span after it is stopped
nhachicha 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 hidden or 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 hidden or 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
166 changes: 166 additions & 0 deletions
166
...in/com/mongodb/internal/observability/micrometer/DefaultMongodbObservationConvention.java
This file contains hidden or 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,166 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * 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 com.mongodb.internal.observability.micrometer; | ||
|
|
||
| import io.micrometer.common.KeyValues; | ||
| import io.micrometer.observation.GlobalObservationConvention; | ||
| import io.micrometer.observation.Observation; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
|
|
||
| /** | ||
| * Default {@link ObservationConvention} for MongoDB observations. | ||
| * <p> | ||
| * Reads domain fields from {@link MongodbContext} and produces the standard MongoDB | ||
| * low-cardinality and high-cardinality key-values. Users can override this by registering | ||
| * a {@code GlobalObservationConvention<MongodbContext>} on their {@code ObservationRegistry}. | ||
| * </p> | ||
| * | ||
| * @since 5.7 | ||
| */ | ||
| public class DefaultMongodbObservationConvention implements GlobalObservationConvention<MongodbContext> { | ||
|
|
||
| @Override | ||
| public boolean supportsContext(final Observation.Context context) { | ||
| return context instanceof MongodbContext; | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValues getLowCardinalityKeyValues(final MongodbContext context) { | ||
| if (context.getObservationType() == MongodbObservation.MONGODB_OPERATION) { | ||
| return getOperationLowCardinalityKeyValues(context); | ||
| } else { | ||
| return getCommandLowCardinalityKeyValues(context); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValues getHighCardinalityKeyValues(final MongodbContext context) { | ||
| if (context.getObservationType() == MongodbObservation.MONGODB_COMMAND) { | ||
| return getCommandHighCardinalityKeyValues(context); | ||
| } | ||
| return KeyValues.empty(); | ||
| } | ||
|
|
||
| private KeyValues getOperationLowCardinalityKeyValues(final MongodbContext context) { | ||
| String commandName = context.getCommandName(); | ||
| String databaseName = context.getDatabaseName(); | ||
| String collectionName = context.getCollectionName(); | ||
|
|
||
| KeyValues kv = KeyValues.of( | ||
| MongodbObservation.OperationLowCardinalityKeyNames.SYSTEM.withValue("mongodb")); | ||
|
|
||
| if (databaseName != null) { | ||
| kv = kv.and(MongodbObservation.OperationLowCardinalityKeyNames.NAMESPACE.withValue(databaseName)); | ||
| } | ||
| if (collectionName != null) { | ||
| kv = kv.and(MongodbObservation.OperationLowCardinalityKeyNames.COLLECTION.withValue(collectionName)); | ||
| } | ||
| if (commandName != null) { | ||
| String dbName = databaseName != null ? databaseName : ""; | ||
| String summary = commandName + " " + dbName | ||
| + (collectionName != null ? "." + collectionName : ""); | ||
| kv = kv.and( | ||
| MongodbObservation.OperationLowCardinalityKeyNames.OPERATION_NAME.withValue(commandName), | ||
| MongodbObservation.OperationLowCardinalityKeyNames.OPERATION_SUMMARY.withValue(summary)); | ||
| } | ||
| return kv; | ||
| } | ||
|
|
||
| private KeyValues getCommandLowCardinalityKeyValues(final MongodbContext context) { | ||
| String commandName = context.getCommandName(); | ||
| String databaseName = context.getDatabaseName(); | ||
| String collectionName = context.getCollectionName(); | ||
| String cmdName = commandName != null ? commandName : ""; | ||
| String dbName = databaseName != null ? databaseName : ""; | ||
| String summary = cmdName + " " + dbName | ||
| + (collectionName != null ? "." + collectionName : ""); | ||
|
|
||
| KeyValues kv = KeyValues.of( | ||
| MongodbObservation.CommandLowCardinalityKeyNames.SYSTEM.withValue("mongodb"), | ||
| MongodbObservation.CommandLowCardinalityKeyNames.NAMESPACE.withValue(dbName), | ||
| MongodbObservation.CommandLowCardinalityKeyNames.QUERY_SUMMARY.withValue(summary), | ||
| MongodbObservation.CommandLowCardinalityKeyNames.COMMAND_NAME.withValue(cmdName)); | ||
| if (collectionName != null) { | ||
| kv = kv.and(MongodbObservation.CommandLowCardinalityKeyNames.COLLECTION.withValue(collectionName)); | ||
| } | ||
| com.mongodb.ServerAddress serverAddress = context.getServerAddress(); | ||
| if (serverAddress != null) { | ||
| kv = kv.and( | ||
| MongodbObservation.CommandLowCardinalityKeyNames.SERVER_ADDRESS.withValue(serverAddress.getHost()), | ||
| MongodbObservation.CommandLowCardinalityKeyNames.SERVER_PORT.withValue( | ||
| String.valueOf(serverAddress.getPort())), | ||
| MongodbObservation.CommandLowCardinalityKeyNames.NETWORK_TRANSPORT.withValue( | ||
| context.isUnixSocket() ? "unix" : "tcp")); | ||
| } | ||
| String responseStatusCode = context.getResponseStatusCode(); | ||
| if (responseStatusCode != null) { | ||
| kv = kv.and(MongodbObservation.CommandLowCardinalityKeyNames.RESPONSE_STATUS_CODE.withValue(responseStatusCode)); | ||
| } | ||
| return kv; | ||
| } | ||
|
|
||
| private KeyValues getCommandHighCardinalityKeyValues(final MongodbContext context) { | ||
| KeyValues kv = KeyValues.empty(); | ||
|
|
||
| String queryText = context.getQueryText(); | ||
| if (queryText != null) { | ||
| kv = kv.and(MongodbObservation.HighCardinalityKeyNames.QUERY_TEXT.withValue(queryText)); | ||
| } | ||
| com.mongodb.connection.ConnectionId connectionId = context.getConnectionId(); | ||
| if (connectionId != null) { | ||
| kv = kv.and( | ||
| MongodbObservation.HighCardinalityKeyNames.CLIENT_CONNECTION_ID.withValue( | ||
| String.valueOf(connectionId.getLocalValue())), | ||
| MongodbObservation.HighCardinalityKeyNames.SERVER_CONNECTION_ID.withValue( | ||
| String.valueOf(connectionId.getServerValue()))); | ||
| } | ||
| Long cursorId = context.getCursorId(); | ||
| if (cursorId != null) { | ||
| kv = kv.and(MongodbObservation.HighCardinalityKeyNames.CURSOR_ID.withValue( | ||
| String.valueOf(cursorId))); | ||
| } | ||
| Long transactionNumber = context.getTransactionNumber(); | ||
| if (transactionNumber != null) { | ||
| kv = kv.and(MongodbObservation.HighCardinalityKeyNames.TRANSACTION_NUMBER.withValue( | ||
| String.valueOf(transactionNumber))); | ||
| } | ||
| String sessionId = context.getSessionId(); | ||
| if (sessionId != null) { | ||
| kv = kv.and(MongodbObservation.HighCardinalityKeyNames.SESSION_ID.withValue(sessionId)); | ||
| } | ||
|
|
||
| // Exception tags from observation error | ||
| Throwable error = context.getError(); | ||
| if (error != null) { | ||
| kv = kv.and( | ||
| MongodbObservation.HighCardinalityKeyNames.EXCEPTION_MESSAGE.withValue(error.getMessage()), | ||
| MongodbObservation.HighCardinalityKeyNames.EXCEPTION_TYPE.withValue(error.getClass().getName()), | ||
| MongodbObservation.HighCardinalityKeyNames.EXCEPTION_STACKTRACE.withValue(getStackTraceAsString(error))); | ||
| } | ||
|
|
||
| return kv; | ||
| } | ||
|
|
||
| private static String getStackTraceAsString(final Throwable throwable) { | ||
| StringWriter sw = new StringWriter(); | ||
| PrintWriter pw = new PrintWriter(sw); | ||
| throwable.printStackTrace(pw); | ||
| return sw.toString(); | ||
| } | ||
| } |
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.
tracingSpan.openScope()happens before command document hydration and before thesendCommandMessagetry/catch. If any exception is thrown betweenopenScope()and the later close sites (e.g.,message.getCommandDocument(bsonOutput), event sender construction), the scope will leak. Enclose the entire post-openScope()section in a try/finally that closes the scope (and ends the span as appropriate) on all exceptional paths.