-
Notifications
You must be signed in to change notification settings - Fork 165
feat(csharp/src/Drivers/Databricks): Implement Telemetry Reporting for Databricks #3191
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 14 commits
fe9b88e
c3626de
b91fd41
ec0f4df
fc75eaf
e5064af
82814f8
4e654fb
8fc65eb
a300733
487ecfc
77a69f0
493e3b8
d56635e
1780b29
42a4cd2
a8fc364
3a9f96c
bea91e3
b1c4ccf
32baf42
8e068d5
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
|
Contributor
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. please add a readme file in this folder, on how is the file generated. |
||
| * 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 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; | ||
|
|
||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry | ||
| { | ||
| public class DatabricksActivityListener : IDisposable | ||
| { | ||
| private readonly ActivityListener _activityListener; | ||
| private TelemetryHelper? _telemetryHelper; | ||
|
|
||
jeremytang-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public DatabricksActivityListener(TelemetryHelper? telemetryHelper, string sourceName) | ||
| { | ||
| this._telemetryHelper = telemetryHelper; | ||
| this._activityListener = new ActivityListener | ||
| { | ||
| ShouldListenTo = (activitySource) => activitySource.Name == sourceName, | ||
| 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")) | ||
| { | ||
| var sqlExecutionEvent = new SqlExecutionEvent(); | ||
| var operationDetail = new OperationDetail(); | ||
| operationDetail.OperationType = Util.StringToOperationType("EXECUTE_STATEMENT_ASYNC"); | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sqlExecutionEvent.OperationDetail = operationDetail; | ||
| _telemetryHelper.AddSqlExecutionEvent(sqlExecutionEvent, Convert.ToInt64(activity.Duration.TotalMilliseconds)); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._activityListener.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| 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; | ||
|
|
||
| public class DatabricksConnectionConfig | ||
| { | ||
| public static readonly int MAX_BATCH_SIZE = 1; | ||
| public static readonly int FLUSH_INTERVAL_MILLIS = 300000; // 5 minutes | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Databricks Telemetry | ||
|
|
||
| Uses `DatabricksActivityListener.cs` to record events. |
| 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; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry | ||
| { | ||
| public 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" : $"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) | ||
| { | ||
| // Log the exception but don't throw to prevent telemetry failures from affecting main functionality | ||
| System.Diagnostics.Debug.WriteLine($"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) | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| 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); | ||
| return response.IsSuccessStatusCode; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Log the exception but don't throw to prevent telemetry failures from affecting main functionality | ||
| System.Diagnostics.Debug.WriteLine($"Failed to send telemetry: {ex.Message}"); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.