Skip to content

Refactor tracing configuration API#5928

Merged
roji merged 5 commits into
npgsql:mainfrom
roji:Tracing
Nov 16, 2024
Merged

Refactor tracing configuration API#5928
roji merged 5 commits into
npgsql:mainfrom
roji:Tracing

Conversation

@roji

@roji roji commented Nov 12, 2024

Copy link
Copy Markdown
Member

Continues #5853

Some general API thoughts I had while doing this:

  • We have a somewhat odd situation, where Npgsql tracing is enabled globally by calling AddNpgsql() (from the Npgsql.OpenTelemetry package), but is configured on a per-datasource basis on NpgsqlDataSourceBuilder.
    • IIUC TracerProviderBuilder.AddSource() needs to be called for any sort of collection to take place (this is a global thing). TracerProviderBuilder is defined in the OpenTelemetry.API package, which Npgsql should not reference (see these docs).
    • In theory, we could define an extension method over NpgsqlDataSourceBuilder inside the Npgsql.OpenTelemetry package - this could call TracerProviderBuilder.AddSource(); but this would require the user to pass in their TracerProviderBuilder, which departs from the way tracing is usually configured etc...
    • As a result, I think we need to keep the above enable/configure split: users enable tracing as before by calling AddNpgsql() globally (and so all data sources trace), but they can tweak tracing config on NpgsqlDataSourceBuilder.
  • In theory, since AddSource() activates tracing for all data sources, it may be desirable to have an API on NpgsqlDataSourceBuilder which disables tracing for that data source. We can always add that on NpgsqlTracingOptionsBuilder later if someone asks for it.

@roji roji requested review from NinoFloris and vonzshik November 12, 2024 16:56
Comment thread src/Npgsql/NpgsqlActivitySource.cs Outdated
Comment thread src/Npgsql/NpgsqlTracingOptionsBuilder.cs Outdated
Comment thread src/Npgsql/NpgsqlTracingOptionsBuilder.cs Outdated
Co-authored-by: Nikita Kazmin <vonzshik@gmail.com>
@roji

roji commented Nov 12, 2024

Copy link
Copy Markdown
Member Author

Thanks for the review @vonzshik. Will wait for any input from @NinoFloris before merging as we've been discussing this a lot recently.

#region Tracing

internal void TraceCommandStart(NpgsqlConnectionStringBuilder settings, NpgsqlTracingOptions? tracingSettings)
internal void TraceCommandStart(NpgsqlConnectionStringBuilder settings, NpgsqlTracingOptions tracingOptions)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is settings actually used?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah - just below? For the filter and span name callbacks?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Or you mean the word "settings" in the name? If so yeah, I'm switching to "options" as that seems to be the direction we're going?

@NinoFloris NinoFloris Nov 13, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Am I looking at different code (did a quick review earlier) I mean the NpgsqlConnectionStringBuilder settings not the tracing options.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah sorry, I was focussed on the tracing options part.

Yeah the connstring builder is there only because we want the database name and we get it out of there. We most probably can get it out of the data source or connector, but let's leave that for another time?

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
ILoggerFactory? _loggerFactory;
bool _sensitiveDataLoggingEnabled;
NpgsqlTracingOptions? _tracingOptions;
Action<NpgsqlTracingOptionsBuilder>? _configureTracingOptionsBuilder;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So the final shape of this is dependent on our conclusions, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You mean in terms of lazy (keep the lambda (or possibly lambdas) and evaluate at the end), or eager (evaluate the lambda immediately and only keep the resulting options), right?

If so yeah... For now I went with the way you did it in the other type loading PR (lazy). I think whatever we do here is likely fine for now, and we can revisit in 10 and possibly change things - but I'm happy to change now if you prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I mostly meant the multi lambda part indeed, whether that's lazy or eager.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, so for now I really just followed what you had in your PR, which was a single lazily-evaluated lambda... Are we good with that for now?

/// Gets or sets a value indicating whether to enable the "time-to-first-read" event.
/// Default is true to preserve existing behavior.
/// </summary>
public NpgsqlTracingOptionsBuilder EnableFirstResponseEvent(bool enable = true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For things that are enabled by default, has this pattern been confusing to EF users? Where they have to use the enable method to disable a default?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I understand the hesitation - I haven't seen anybody actually have trouble with it... People who don't want to disable never see it or care, and people who do figure it out...

I'd be OK with flipping this around (DisableFirstResponseEvent), but then you get double-negation passing false, so... Let me know what you prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I would definitely stick to the non negated naming convention. Was wondering whether you had gotten any EF feedback calling out (somewhat) worse discoverability.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nope, not really...

@roji roji merged commit 6fb947f into npgsql:main Nov 16, 2024
@roji roji deleted the Tracing branch November 16, 2024 08:34
roji added a commit to roji/Npgsql that referenced this pull request Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants