Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 64 additions & 3 deletions csharp/src/Drivers/Databricks/DatabricksConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
using Apache.Arrow.Adbc.Drivers.Apache.Hive2.Client;
using Apache.Arrow.Adbc.Drivers.Apache.Spark;
using Apache.Arrow.Adbc.Drivers.Databricks.Auth;
using Apache.Arrow.Adbc.Drivers.Databricks.CloudFetch;

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-13 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)

Check failure on line 31 in csharp/src/Drivers/Databricks/DatabricksConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2022 8.0.x

The type or namespace name 'CloudFetch' does not exist in the namespace 'Apache.Arrow.Adbc.Drivers.Databricks' (are you missing an assembly reference?)
using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry;
using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Enums;
using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Model;
using Apache.Arrow.Adbc.Tracing;
using Apache.Arrow.Adbc.Drivers.Databricks.Reader;
using Apache.Arrow.Ipc;
using Apache.Hive.Service.Rpc.Thrift;
Expand Down Expand Up @@ -67,6 +72,11 @@
private string _traceParentHeaderName = "traceparent";
private bool _traceStateEnabled = false;

// Telemetry
private TelemetryHelper? _telemetryHelper;
private DatabricksActivityListener _databricksActivityListener;
private Guid _guid;

// Identity federation client ID for token exchange
private string? _identityFederationClientId;

Expand All @@ -77,15 +87,17 @@

public DatabricksConnection(IReadOnlyDictionary<string, string> properties) : base(properties)
{
_guid = Guid.NewGuid();
ValidateProperties();
InitializeTelemetry();
_databricksActivityListener = new DatabricksActivityListener(_telemetryHelper, this.AssemblyName, _guid);
}

public override IEnumerable<KeyValuePair<string, object?>>? GetActivitySourceTags(IReadOnlyDictionary<string, string> properties)
{
IEnumerable<KeyValuePair<string, object?>>? tags = base.GetActivitySourceTags(properties);
// TODO: Add any additional tags specific to Databricks connection
//tags ??= [];
//tags.Concat([new("key", "value")]);
tags ??= [];
tags = tags.Concat([new(Util.GetDatabricksTag(),_guid)]);
return tags;
}

Expand Down Expand Up @@ -284,6 +296,45 @@
}
}

private void InitializeTelemetry()
{
var connectionParams = new DriverConnectionParameters();
var hostDetails = new HostDetails();
var clientContext = new ClientContext();
string? token = null;

if (Properties.TryGetValue(SparkParameters.AuthType, out string? authType))
{
connectionParams.AuthMech = Util.StringToAuthMech(authType);
}

if (Properties.TryGetValue(SparkParameters.HostName, out string? host))
{
hostDetails.HostUrl = host;
}
if (Properties.TryGetValue(SparkParameters.Port, out string? port))
{
hostDetails.Port = Int32.Parse(port);
}
connectionParams.HostInfo = hostDetails;

if (Properties.TryGetValue(SparkParameters.UserAgentEntry, out string? userAgent))
{
clientContext.UserAgent = userAgent;
}

if(Properties.TryGetValue(SparkParameters.AccessToken, out string? accessToken))
{
token = accessToken;
}
else if(Properties.TryGetValue(SparkParameters.Token, out string? accesstoken))
{
token = accesstoken;
}
_telemetryHelper = new TelemetryHelper(token);
_telemetryHelper.SetParameters(connectionParams, clientContext);
}

/// <summary>
/// Gets whether server side properties should be applied using queries.
/// </summary>
Expand Down Expand Up @@ -424,6 +475,7 @@
}
}

_telemetryHelper?.InitializeTelemetryClient(_authHttpClient);
return baseHandler;
}

Expand Down Expand Up @@ -724,6 +776,15 @@
{
if (disposing)
{
_databricksActivityListener?.Dispose();
try
{
_telemetryHelper?.ForceFlushAsync().Wait(TimeSpan.FromSeconds(2));
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to flush telemetry on dispose: {ex.Message}");
}
_authHttpClient?.Dispose();
}
base.Dispose(disposing);
Expand Down
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
Copy link
Contributor

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.

* 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;

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 == Util.GetDatabricksTag() && 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)
{
var sqlExecutionEvent = new SqlExecutionEvent();
var operationDetail = new OperationDetail();
operationDetail.OperationType = OperationType.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.
83 changes: 83 additions & 0 deletions csharp/src/Drivers/Databricks/Telemetry/TelemetryClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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
{
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)
{
// 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;
}
}
}
}
Loading
Loading