Skip to content
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

Enable query statement #39990

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open

Enable query statement #39990

wants to merge 49 commits into from

Conversation

JJ-WM
Copy link

@JJ-WM JJ-WM commented May 1, 2024

Description

db.statement are not getting printed in trace according to https://opentelemetry.io/docs/specs/semconv/database/database-spans/ with rational is customer data would be leaked in the query statement. However, users should still have the option to print the query and sanitize it in their own tracing implementation.

This change will add a new options to print query statement in db.statement attribute.
NONE - no query is print
PARAMETERIZED_ONLY - only parameterized query is printed
PARAMETERIZED_AND_NON_PARAMETERIZED - all query is printed.
new CosmosClientTelemetryConfig().showQueryOptions(showQueryOptions);

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

JJ-WM and others added 30 commits February 28, 2023 22:32
@github-actions github-actions bot added Community Contribution Community members are working on the issue Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization. labels May 1, 2024
Copy link

github-actions bot commented May 1, 2024

Thank you for your contribution @JJ-WM! We will review the pull request and get back to you soon.

@@ -1295,6 +1396,13 @@ private void verifyOTelTracerAttributes(

verifyOTelTracerTransport(
cosmosDiagnostics, error, mockTracer, enableRequestLevelTracing);

if(showQueryStatement) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space after if

public String getQueryStatement(CosmosDiagnosticsContext ctx) {
checkNotNull(ctx, "Argument 'ctx' must not be null.");
return ctx.getQueryStatement();
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the toJson() call needs to change? Or should there be some JSON ignore annotations on the method above or the query holding field? I.e. if someone calls toJson() should the query be logged out or not? May be not - it can be ok for traces but now the query will show in logs as well. Or that is ok?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toJson shouldn't print since I didn't add the node there, but it could be an issue if user try to serialize this object directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add the query text there as well (in the toJson)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add the query text, we could log the query with customer data in the query in the diagnostic does it?

@@ -1202,6 +1207,10 @@ private StartSpanOptions startSpanOptions(String spanName, CosmosDiagnosticsCont
!cosmosCtx.getOperationId().equals(ctxAccessor.getSpanName(cosmosCtx))) {
spanOptions.setAttribute("db.cosmosdb.operation_id", cosmosCtx.getOperationId());
}

if(showQueryStatement() && null != cosmosCtx.getQueryStatement() ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space after if

if (logger.isDebugEnabled()) {
logger.debug("Channel({} --> {}) closed", channel, this.remoteAddress());
}
logger.info("Channel({} --> {}) closed due to CPU > 0.90D", channel, this.remoteAddress());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to change this log level? how often the info will be logged out?
Also do check if info level is enabled. If not you will be wasting CPU cycles to do string formatting etc. only for that to be thrown out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess info would be useful here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one also please revert - too noisy in certain environments - in many scenarios in Spark CPU will continuously be >90% - also the threshold isn't always 90% - it is configurable. If this is important to you, then logging as info once (or if you prefer once every x seconds) would be fine - but not info all the time

}
} else {
final long elapsedTimeInNanos = System.nanoTime() - endpoint.lastRequestNanoTime();

if (idleEndpointTimeoutInNanos - elapsedTimeInNanos <= 0) {
if (logger.isDebugEnabled()) {
if(logger.isDebugEnabled()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not: add space after if

if (logger.isDebugEnabled()) {
logger.debug("notifyChannelConnect promise.trySuccess(channel)=false");
}
logger.info("notifyChannelConnect local {}, remote {} promise.trySuccess(channel)=false", channel.localAddress(), channel.remoteAddress());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the log level? Also do check if info level is enabled. If not you will be wasting CPU cycles to do string formatting etc only for that to be thrown out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - this would be too noisy. Please revert the log level change

if (logger.isDebugEnabled()) {
logger.debug("notifyChannelConnect future was not successful");
}
logger.error("notifyChannelConnect future was not successful {}", future.cause());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to log the error?

* It's the user's responsiblity to sanitize the query if necessary.
* @return current CosmosClientTelemetryConfig
*/
public CosmosClientTelemetryConfig showQueryStatement() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the case of turning on/off the showing of queries in traces on the same client (not creating new one) needs to be supported? I.e. dynamic change on/off? Looks like here one can set it on but can't flip it to off.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should extend this a bit - 1) I think we should allow both adding query when parameterized only (what you have now) - but also allow enabling it for simple query text - so, we would need two flags here - showParameterized, showNonParameterized or so. If those flags are boolean it can be used to enable or disabled. and name of the method should probably be something like setIncludeQueryStatement. I would also argue that this flag should determine whether the query statement is even added to CosmosDiagnosticsContext - and if it is we should also log it (like include in toJson) as well as trace it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking of having too flags also, but that would be too much and decided to move the logic to the user custom tracer implementation. We can discuss more on our call.

JJ-WM added 14 commits May 2, 2024 11:41
…ne, parameterized or all queries statement"

This reverts commit 602a5bf.
…ne, parameterized or all queries statement"

This reverts commit 1c1a9f0.
…ne, parameterized or all queries statement"

This reverts commit 602a5bf.
…nting none, parameterized or all queries statement""

This reverts commit 4d51243.
…nting none, parameterized or all queries statement""

This reverts commit de71626.
…nting none, parameterized or all queries statement""

This reverts commit 2633f40.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution Community members are working on the issue Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants