|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Databricks Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import IClientContext from '../contracts/IClientContext'; |
| 18 | +import { LogLevel } from '../contracts/IDBSQLLogger'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Telemetry client for a specific host. |
| 22 | + * Managed by TelemetryClientProvider with reference counting. |
| 23 | + * One client instance is shared across all connections to the same host. |
| 24 | + */ |
| 25 | +class TelemetryClient { |
| 26 | + private closed: boolean = false; |
| 27 | + |
| 28 | + constructor(private context: IClientContext, private host: string) { |
| 29 | + const logger = context.getLogger(); |
| 30 | + logger.log(LogLevel.debug, `Created TelemetryClient for host: ${host}`); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the host associated with this client. |
| 35 | + */ |
| 36 | + getHost(): string { |
| 37 | + return this.host; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks if the client has been closed. |
| 42 | + */ |
| 43 | + isClosed(): boolean { |
| 44 | + return this.closed; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Closes the telemetry client and releases resources. |
| 49 | + * Should only be called by TelemetryClientProvider when reference count reaches zero. |
| 50 | + */ |
| 51 | + close(): void { |
| 52 | + if (this.closed) { |
| 53 | + return; |
| 54 | + } |
| 55 | + try { |
| 56 | + this.context.getLogger().log(LogLevel.debug, `Closing TelemetryClient for host: ${this.host}`); |
| 57 | + } catch { |
| 58 | + // swallow |
| 59 | + } finally { |
| 60 | + this.closed = true; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export default TelemetryClient; |
0 commit comments