-
Notifications
You must be signed in to change notification settings - Fork 162
feat(csharp/src/Drivers/Databricks): Add Logging Capabilities to Databricks driver #3231
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
Draft
jeremytang-db
wants to merge
28
commits into
apache:main
Choose a base branch
from
jeremytang-db:implement-loggin
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.
Draft
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
fe9b88e
test
jeremytang-db c3626de
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc
jeremytang-db b91fd41
fix merge
jeremytang-db ec0f4df
more fix
jeremytang-db fc75eaf
in progress
jeremytang-db e5064af
review
jeremytang-db 82814f8
fix field name
jeremytang-db 4e654fb
move things
jeremytang-db 8fc65eb
fix small errors
jeremytang-db a300733
add
jeremytang-db af908ab
git push
jeremytang-db 487ecfc
update
jeremytang-db 77a69f0
Merge branch 'apache:main' into main
jeremytang-db 493e3b8
update
jeremytang-db d56635e
add guid for update
jeremytang-db 3acb9e0
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db 98700c4
update
jeremytang-db 1780b29
update
jeremytang-db 42a4cd2
Merge branch 'main' of https://github.com/apache/arrow-adbc
jeremytang-db a8fc364
fix
jeremytang-db 97c9cf4
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db 509c43b
update
jeremytang-db 3a9f96c
update
jeremytang-db e853852
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db 7a1c997
logging
jeremytang-db 9d324ec
update
jeremytang-db bea91e3
update
jeremytang-db 27019ba
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,76 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Log; | ||
|
|
||
| public class DatabricksLogger | ||
| { | ||
| public static readonly string DATABRICKS_LOG_FILE = "databricks_adbc.log"; | ||
| public static readonly DatabricksLogger LOGGER = new DatabricksLogger(); | ||
|
|
||
| private static readonly object _logLock = new object(); | ||
| private static readonly string _logFilePath = Path.Combine(Path.GetTempPath(), DATABRICKS_LOG_FILE); | ||
|
|
||
| public void trace(string message) | ||
| { | ||
| WriteLog("TRACE", message); | ||
| } | ||
|
|
||
| public void debug(string message) | ||
| { | ||
| WriteLog("DEBUG", message); | ||
| } | ||
|
|
||
| public void info(string message) | ||
| { | ||
| WriteLog("INFO", message); | ||
| } | ||
|
|
||
| public void warn(string message) | ||
| { | ||
| WriteLog("WARN", message); | ||
| } | ||
|
|
||
| public void error(string message) | ||
| { | ||
| WriteLog("ERROR", message); | ||
| } | ||
|
|
||
| public void fatal(string message) | ||
| { | ||
| WriteLog("FATAL", message); | ||
| } | ||
|
|
||
| private static void WriteLog(string level, string message) | ||
| { | ||
| var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level}] {message}"; | ||
|
|
||
| lock (_logLock) | ||
| { | ||
| try | ||
| { | ||
| File.AppendAllText(_logFilePath, logMessage + Environment.NewLine); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| } | ||
| } | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.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,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Apache.Arrow.Adbc.Tracing; | ||
| using Apache.Arrow.Adbc.Drivers.Apache; | ||
| using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Model; | ||
| using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Enums; | ||
| using static Apache.Arrow.Adbc.Drivers.Databricks.Log.DatabricksLogger; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry | ||
| { | ||
| internal class DatabricksActivityListener : IDisposable | ||
| { | ||
| private readonly ActivityListener _activityListener; | ||
| private TelemetryHelper? _telemetryHelper; | ||
|
|
||
| public DatabricksActivityListener(TelemetryHelper? telemetryHelper, string sourceName, Guid guid) | ||
| { | ||
| this._telemetryHelper = telemetryHelper; | ||
| this._activityListener = new ActivityListener | ||
| { | ||
| ShouldListenTo = (activitySource) => activitySource.Tags?.Any(kvp => kvp.Key == "guid" && kvp.Value?.Equals(guid) == true) == true, | ||
| Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, | ||
| ActivityStopped = OnActivityStopped, | ||
| }; | ||
| ActivitySource.AddActivityListener(_activityListener); | ||
| } | ||
|
|
||
| private void OnActivityStopped(Activity activity) | ||
| { | ||
| if(_telemetryHelper == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if(activity.OperationName?.EndsWith("ExecuteStatementAsync") == true) | ||
| { | ||
| LOGGER.info("OnActivityStopped: " + activity.OperationName); | ||
| var sqlExecutionEvent = new SqlExecutionEvent(); | ||
| var operationDetail = new OperationDetail(); | ||
| operationDetail.OperationType = Util.StringToOperationType("EXECUTE_STATEMENT_ASYNC"); | ||
| sqlExecutionEvent.OperationDetail = operationDetail; | ||
| _telemetryHelper.AddSqlExecutionEvent(sqlExecutionEvent, Convert.ToInt64(activity.Duration.TotalMilliseconds)); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._activityListener.Dispose(); | ||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
csharp/src/Drivers/Databricks/Telemetry/DatabricksConnectionConfig.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,24 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry; | ||
|
|
||
| internal class DatabricksConnectionConfig | ||
| { | ||
| public static readonly int MAX_BATCH_SIZE = 200; | ||
| public static readonly int FLUSH_INTERVAL_MILLIS = 300000; // 5 minutes | ||
| } |
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,7 @@ | ||
| # Databricks Telemetry | ||
|
|
||
| Uses `DatabricksActivityListener.cs` to listen and record events to `TelemetryHelper.cs` using ActivityListener. | ||
|
|
||
| `TelemetryHelper.cs` stores telemetry logs and telemetry parameters. | ||
|
|
||
| `TelemtryClient.cs` sends logs to `/telemetry` or `/telemetry-unauth` depending on authentication level. |
124 changes: 124 additions & 0 deletions
124
csharp/src/Drivers/Databricks/Telemetry/TelemetryClient.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,124 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Model; | ||
| using static Apache.Arrow.Adbc.Drivers.Databricks.Log.DatabricksLogger; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry | ||
| { | ||
| internal class TelemetryClient | ||
| { | ||
| private readonly HttpClient _httpClient; | ||
| private readonly string? _telemetryUrl; | ||
| private readonly string? _accessToken; | ||
|
|
||
| public TelemetryClient(HttpClient httpClient, string? hostUrl, string? accessToken) | ||
| { | ||
| _httpClient = httpClient; | ||
| _accessToken = accessToken; | ||
| _telemetryUrl = !string.IsNullOrEmpty(hostUrl) ? accessToken != null ? $"https://{hostUrl}/telemetry-ext" : $"https://{hostUrl}/telemetry-unauth" : null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sends a batch of telemetry events asynchronously | ||
| /// </summary> | ||
| /// <param name="telemetryBatch">List of telemetry events to send</param> | ||
| /// <returns>Task representing the async operation</returns> | ||
| public async Task<bool> SendTelemetryBatchAsync(List<TelemetryFrontendLog> telemetryBatch) | ||
| { | ||
| if (string.IsNullOrEmpty(_telemetryUrl) || telemetryBatch.Count == 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var request = new HttpRequestMessage(HttpMethod.Post, _telemetryUrl); | ||
|
|
||
| // Serialize the batch to JSON | ||
| var telemetryRequest = new TelemetryRequest(); | ||
| telemetryRequest.UploadTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| telemetryRequest.ProtoLogs = telemetryBatch.Select(x => JsonSerializer.Serialize(x)).ToList(); | ||
| request.Content = new StringContent(JsonSerializer.Serialize(telemetryRequest)); | ||
|
|
||
| // Set headers | ||
| request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
| if(_accessToken != null) | ||
| { | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); | ||
| } | ||
|
|
||
| var response = await _httpClient.SendAsync(request); | ||
| return response.IsSuccessStatusCode; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| LOGGER.error($"Failed to send telemetry: {ex.Message}"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sends a single telemetry event asynchronously | ||
| /// </summary> | ||
| /// <param name="telemetryEvent">Single telemetry event to send</param> | ||
| /// <returns>Task representing the async operation</returns> | ||
| public async Task<bool> SendTelemetryAsync(TelemetryFrontendLog telemetryEvent) | ||
| { | ||
| if (string.IsNullOrEmpty(_telemetryUrl)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var request = new HttpRequestMessage(HttpMethod.Post, _telemetryUrl); | ||
|
|
||
| // Serialize the event to JSON | ||
| var telemetryRequest = new TelemetryRequest(); | ||
| telemetryRequest.UploadTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| telemetryRequest.ProtoLogs = new List<string> { JsonSerializer.Serialize(telemetryEvent) }; | ||
| request.Content = new StringContent(JsonSerializer.Serialize(telemetryRequest)); | ||
|
|
||
| // Set headers | ||
| request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
| if(_accessToken != null) | ||
| { | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); | ||
| } | ||
|
|
||
| var response = await _httpClient.SendAsync(request); | ||
| LOGGER.info($"Telemetry response: {response.StatusCode}"); | ||
| return response.IsSuccessStatusCode; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| LOGGER.error($"Failed to send telemetry: {ex.Message}"); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } |
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.
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.
As the name of the function may get renamed, you should use
nameof(ExecuteStatementAsync)