Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fe9b88e
test
jeremytang-db Jul 23, 2025
c3626de
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc
jeremytang-db Jul 23, 2025
b91fd41
fix merge
jeremytang-db Jul 23, 2025
ec0f4df
more fix
jeremytang-db Jul 23, 2025
fc75eaf
in progress
jeremytang-db Jul 23, 2025
e5064af
review
jeremytang-db Jul 24, 2025
82814f8
fix field name
jeremytang-db Jul 25, 2025
4e654fb
move things
jeremytang-db Jul 28, 2025
8fc65eb
fix small errors
jeremytang-db Jul 28, 2025
a300733
add
jeremytang-db Jul 28, 2025
af908ab
git push
jeremytang-db Jul 29, 2025
487ecfc
update
jeremytang-db Jul 29, 2025
77a69f0
Merge branch 'apache:main' into main
jeremytang-db Jul 29, 2025
493e3b8
update
jeremytang-db Jul 29, 2025
d56635e
add guid for update
jeremytang-db Jul 31, 2025
3acb9e0
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db Jul 31, 2025
98700c4
update
jeremytang-db Jul 31, 2025
1780b29
update
jeremytang-db Aug 1, 2025
42a4cd2
Merge branch 'main' of https://github.com/apache/arrow-adbc
jeremytang-db Aug 1, 2025
a8fc364
fix
jeremytang-db Aug 1, 2025
97c9cf4
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db Aug 1, 2025
509c43b
update
jeremytang-db Aug 1, 2025
3a9f96c
update
jeremytang-db Aug 1, 2025
e853852
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db Aug 1, 2025
7a1c997
logging
jeremytang-db Aug 1, 2025
9d324ec
update
jeremytang-db Aug 4, 2025
bea91e3
update
jeremytang-db Aug 5, 2025
27019ba
Merge branch 'main' of github.com:jeremytang-db/arrow-adbc into imple…
jeremytang-db Aug 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions csharp/src/Drivers/Databricks/DatabricksConnection.cs

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions csharp/src/Drivers/Databricks/Log/DatabricksLogger.cs
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)
{
}
}
}
}
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)
Copy link
Contributor

@birschick-bq birschick-bq Aug 7, 2025

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)

{
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();
}
}
}
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
}
7 changes: 7 additions & 0 deletions csharp/src/Drivers/Databricks/Telemetry/README.md
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 csharp/src/Drivers/Databricks/Telemetry/TelemetryClient.cs
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;
}
}
}
}
Loading