birschick-bq commented on code in PR #3191: URL: https://github.com/apache/arrow-adbc/pull/3191#discussion_r2238456800
########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; + + public DatabricksActivityListener() Review Comment: You could pass in the source name using `ApacheUtility.GetAssemblyName(typeof(DatabricksConnection))`. Or from `connection.AssemblyName` (if you access to the connection object). ########## csharp/src/Drivers/Databricks/Telemetry/TelemetryHelper.cs: ########## @@ -0,0 +1,173 @@ +/* +* 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.Globalization; +using System.Reflection; +using System.Diagnostics; +using System.Collections.Concurrent; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Model; +using Apache.Arrow.Adbc.Drivers.Databricks.Telemetry.Enums; +using Apache.Arrow.Adbc.Drivers.Apache.Spark; +using System.Text.Json; + +namespace Apache.Arrow.Adbc.Drivers.Databricks.Telemetry +{ + public class TelemetryHelper + { + private static List<TelemetryFrontendLog> _eventsBatch = new List<TelemetryFrontendLog>(); + private static readonly object _eventsBatchLock = new object(); + private static long _lastFlushTimeMillis; + private static readonly Timer _flushTimer; + + private static TelemetryClient? _telemetryClient; + private static DatabricksActivityListener? _activityListener; + + private static ClientContext? _clientContext; + private static string? _accessToken; + private static DriverConnectionParameters? _connectionParameters; + private static readonly DriverSystemConfiguration _systemConfiguration = new DriverSystemConfiguration() + { + DriverVersion = Util.GetDriverVersion(), + DriverName = Util.GetDriverName(), + OsName = Environment.OSVersion.Platform.ToString(), + OsVersion = Environment.OSVersion.Version.ToString(), + OsArch = Environment.OSVersion.Platform.ToString(), + RuntimeName = Assembly.GetExecutingAssembly().GetName().Name, + RuntimeVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString(), + RuntimeVendor = RuntimeInformation.FrameworkDescription, + ClientAppName = null, + LocaleName = CultureInfo.CurrentCulture.Name, + ProcessName = Process.GetCurrentProcess().ProcessName + }; + + static TelemetryHelper() Review Comment: I believe this should be an instance class and not static. It should be instanced by the these two value: `_connectionParameters.HostInfo.HostUrl, _accessToken` Why? Because if you had more than one instance of DatabricksConnection (with different HostUrl), the second one would overwrite the HostUrl and accessToken and the original would now be sending telemetry to the second connection's host. ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; Review Comment: ```suggestion private readonly ActivityListener _activityListener; ``` ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; + + public DatabricksActivityListener() + { + this._activityListener = new ActivityListener + { + //ShouldListenTo = (activitySource) => activitySource.Name == sourceName, + ShouldListenTo = _ => true, + Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, + ActivityStarted = OnActivityStarted, + ActivityStopped = OnActivityStopped, + }; + ActivitySource.AddActivityListener(_activityListener); + } + + private void OnActivityStarted(Activity activity) + { + } + + private void OnActivityStopped(Activity activity) + { + if(activity.OperationName == "ExecuteStatementAsync") Review Comment: You might consider `.EndsWith()`. ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; + + public DatabricksActivityListener() + { + this._activityListener = new ActivityListener + { + //ShouldListenTo = (activitySource) => activitySource.Name == sourceName, + ShouldListenTo = _ => true, + Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, + ActivityStarted = OnActivityStarted, + ActivityStopped = OnActivityStopped, + }; + ActivitySource.AddActivityListener(_activityListener); + } + + private void OnActivityStarted(Activity activity) + { + } + + private void OnActivityStopped(Activity activity) + { + if(activity.OperationName == "ExecuteStatementAsync") + { + var sqlExecutionEvent = new SqlExecutionEvent(); + var operationDetail = new OperationDetail(); + operationDetail.OperationType = Util.StringToOperationType("EXECUTE_STATEMENT_ASYNC"); + 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") Review Comment: You should use `SemanticConventions.Db.Query.Text` for this and instrumentation. ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; + + public DatabricksActivityListener() + { + this._activityListener = new ActivityListener + { + //ShouldListenTo = (activitySource) => activitySource.Name == sourceName, + ShouldListenTo = _ => true, + Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, + ActivityStarted = OnActivityStarted, + ActivityStopped = OnActivityStopped, + }; + ActivitySource.AddActivityListener(_activityListener); + } + + private void OnActivityStarted(Activity activity) + { + } + + private void OnActivityStopped(Activity activity) + { + if(activity.OperationName == "ExecuteStatementAsync") + { + var sqlExecutionEvent = new SqlExecutionEvent(); + var operationDetail = new OperationDetail(); + operationDetail.OperationType = Util.StringToOperationType("EXECUTE_STATEMENT_ASYNC"); + 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") + { + 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(); + } + } +} Review Comment: nit: empty line at EOF. ```suggestion } ``` ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + + private ActivityListener _activityListener; + + public DatabricksActivityListener() + { + this._activityListener = new ActivityListener + { + //ShouldListenTo = (activitySource) => activitySource.Name == sourceName, + ShouldListenTo = _ => true, + Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, + ActivityStarted = OnActivityStarted, Review Comment: No need to set this properties if you intend it to be empty. ########## csharp/src/Drivers/Databricks/Telemetry/DatabricksActivityListener.cs: ########## @@ -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 + { + Review Comment: nit ```suggestion ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
