-
Notifications
You must be signed in to change notification settings - Fork 6
Tag ClickHouse User-Agent with lib identity #47
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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
26 changes: 26 additions & 0 deletions
26
src/EFCore.ClickHouse/Storage/Internal/ClickHouseClientIdentity.cs
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,26 @@ | ||
| using ClickHouse.Driver.ADO; | ||
|
|
||
| namespace ClickHouse.EntityFrameworkCore.Storage.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Tags the ClickHouse client's <c>User-Agent</c> with this provider's identity so that | ||
| /// queries it issues are attributable server-side via | ||
| /// <see cref="ClickHouseClientSettings.ApplicationInfo"/> (ClickHouse.Driver 1.3.0+). | ||
| /// </summary> | ||
| internal static class ClickHouseClientIdentity | ||
| { | ||
| /// <summary> | ||
| /// Value of the <c>lib</c> User-Agent tag identifying this library. | ||
| /// </summary> | ||
| internal const string LibraryName = "ClickHouse.EntityFrameworkCore"; | ||
|
|
||
| /// <summary> | ||
| /// Builds <see cref="ClickHouseClientSettings"/> from a connection string with the | ||
| /// <c>lib</c> User-Agent tag set to <see cref="LibraryName"/>. | ||
| /// </summary> | ||
| internal static ClickHouseClientSettings CreateSettings(string connectionString) | ||
| => new(connectionString) | ||
| { | ||
| ApplicationInfo = new Dictionary<string, string> { ["lib"] = LibraryName }, | ||
| }; | ||
| } |
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
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
57 changes: 57 additions & 0 deletions
57
test/EFCore.ClickHouse.FunctionalTests/UserAgentClickHouseTest.cs
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,57 @@ | ||
| using ClickHouse.Driver.ADO; | ||
| using Microsoft.EntityFrameworkCore.TestUtilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Verifies end-to-end that the provider tags its ClickHouse HTTP requests with the | ||
| /// <c>lib</c> User-Agent token, by reading it back from <c>system.query_log</c>. | ||
| /// </summary> | ||
| public class UserAgentClickHouseTest | ||
| { | ||
| private sealed class ProbeContext(string connectionString) : DbContext | ||
| { | ||
| private readonly string _connectionString = connectionString; | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| => optionsBuilder.UseClickHouse(_connectionString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Queries_are_tagged_with_lib_in_user_agent() | ||
| { | ||
| var connectionString = TestEnvironment.DefaultConnection; | ||
| var marker = $"ua_probe_{Guid.NewGuid():N}"; | ||
|
|
||
| // Issue a query through EF Core's (tagged) connection. The marker (a GUID) is | ||
| // concatenated rather than interpolated to keep clear of EF1002. | ||
| var probeSql = "SELECT 1 /* " + marker + " */"; | ||
| await using (var context = new ProbeContext(connectionString)) | ||
| { | ||
| await context.Database.ExecuteSqlRawAsync(probeSql); | ||
| } | ||
|
|
||
| // Use a separate, untagged connection for verification so the lib tag we assert | ||
| // on can only have come from the provider's own connection. | ||
| await using var connection = new ClickHouseConnection(connectionString); | ||
| await connection.OpenAsync(); | ||
|
|
||
| using (var flush = connection.CreateCommand()) | ||
| { | ||
| flush.CommandText = "SYSTEM FLUSH LOGS"; | ||
| await flush.ExecuteNonQueryAsync(); | ||
| } | ||
|
|
||
| using var query = connection.CreateCommand(); | ||
| query.CommandText = | ||
| "SELECT count() FROM system.query_log " + | ||
| $"WHERE query LIKE '%{marker}%' " + | ||
| "AND http_user_agent LIKE '%lib:ClickHouse.EntityFrameworkCore%' " + | ||
| "AND event_time > now() - INTERVAL 5 MINUTE"; | ||
|
|
||
| var count = Convert.ToInt64(await query.ExecuteScalarAsync()); | ||
| Assert.True(count > 0, | ||
| "Expected EF Core's query to carry lib:ClickHouse.EntityFrameworkCore in the HTTP User-Agent."); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.