-
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 10 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,79 @@ | ||
| /* | ||
| * 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.Diagnostics; | ||
| using Apache.Arrow.Adbc.Tracing; | ||
| using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Model; | ||
| using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Enums; | ||
| using System; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry | ||
| { | ||
| public class DatabricksActivityListener : IDisposable | ||
| { | ||
|
|
||
jeremytang-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private ActivityListener _activityListener; | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public DatabricksActivityListener() | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| this._activityListener = new ActivityListener | ||
| { | ||
| //ShouldListenTo = (activitySource) => activitySource.Name == sourceName, | ||
| ShouldListenTo = _ => true, | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, | ||
| ActivityStarted = OnActivityStarted, | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ActivityStopped = OnActivityStopped, | ||
| }; | ||
| ActivitySource.AddActivityListener(_activityListener); | ||
| } | ||
|
|
||
| private void OnActivityStarted(Activity activity) | ||
| { | ||
| } | ||
|
|
||
| private void OnActivityStopped(Activity activity) | ||
| { | ||
| if(activity.OperationName == "ExecuteStatementAsync") | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| 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); | ||
| } | ||
| // iterate over the tags and create a telemetry event | ||
| foreach (var tag in activity.Tags) | ||
| { | ||
| // example tag and handling | ||
| if(tag.Key == "sql.query") | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| var sqlExecutionEvent = new SqlExecutionEvent(); | ||
| var operationDetail = new OperationDetail(); | ||
| operationDetail.OperationType = Util.StringToOperationType(tag.Value); | ||
| sqlExecutionEvent.StatementType = StatementType.QUERY; | ||
| sqlExecutionEvent.OperationDetail = operationDetail; | ||
| TelemetryHelper.AddSqlExecutionEvent(sqlExecutionEvent); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._activityListener.Dispose(); | ||
| } | ||
| } | ||
| } | ||
jeremytang-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 = 200; | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
please add a readme file in this folder, on how is the file generated.