-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Provide fallback for Observation KeyValues #5020
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,11 @@ | |
*/ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.docs.KeyName; | ||
import io.micrometer.observation.docs.ObservationDocumentation; | ||
import org.jspecify.annotations.Nullable; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
/** | ||
* A MongoDB-based {@link io.micrometer.observation.Observation}. | ||
|
@@ -172,6 +175,15 @@ public String asString() { | |
public String asString() { | ||
return "db.operation"; | ||
} | ||
}; | ||
|
||
/** | ||
* Creates a key value for the given key name. | ||
* @param value value for key, if value is null or empty {@link KeyValue.NONE_VALUE} will be used | ||
* @return key value | ||
*/ | ||
public KeyValue withOptionalValue(@Nullable String value) { | ||
return withValue(ObjectUtils.isEmpty(value) ? KeyValue.NONE_VALUE : value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if "UNKNOWN" is a better default value for most of these rather than none. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to have a bit more encapsulated API for a more declarative approach to reduce the likelihood for using the API wrong. Something along the lines of: // TODO: it would be nice to have a getKeyName() method returning KeyName from KeyValue for usage in e.g. KeyName[] getLowCardinalityKeyNames()
static KeyValue DB_SYSTEM = KeyValue.of("db.system", "mongodb");
static SmartKeyName<ConnectionString> DB_CONNECTION_STRING = SmartKeyName.required("db.connection_string",
ConnectionString::getConnectionString); later: @Nullable ConnectionString connectionString = context.getConnectionString();
KeyValues keyValues = KeyValues.of(MongoObservation.DB_SYSTEM,
MongoObservation.DB_CONNECTION_STRING.valueOf(connectionString)); You can find |
||
} | ||
} | ||
|
||
|
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 see your approach and I like it more than concatenating
KeyValues
. With a refined approach regarding value extraction, we might get a chance for improvement.