-
Notifications
You must be signed in to change notification settings - Fork 46
[2/7] Telemetry Infrastructure: CircuitBreaker and FeatureFlagCache #325
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
Open
samikshya-db
wants to merge
10
commits into
main
Choose a base branch
from
telemetry-2-infrastructure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db640fa
Add telemetry infrastructure: CircuitBreaker and FeatureFlagCache
samikshya-db 211f91c
Add authentication support for REST API calls
samikshya-db c3779af
Fix feature flag and telemetry export endpoints
samikshya-db a105777
Match JDBC telemetry payload format
samikshya-db 1cdb716
Fix lint errors
samikshya-db 689e561
Add missing getAuthHeaders method to ClientContextStub
samikshya-db 2d41e2d
Fix prettier formatting
samikshya-db e474256
Add DRIVER_NAME constant for nodejs-sql-driver
samikshya-db a3c9042
Add missing telemetry fields to match JDBC
samikshya-db 6110797
Fix TypeScript compilation: add missing fields to system_configuratio…
samikshya-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /** | ||
| * Copyright (c) 2025 Databricks Contributors | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
|
|
||
| import IClientContext from '../contracts/IClientContext'; | ||
| import { LogLevel } from '../contracts/IDBSQLLogger'; | ||
|
|
||
| /** | ||
| * States of the circuit breaker. | ||
| */ | ||
| export enum CircuitBreakerState { | ||
| /** Normal operation, requests pass through */ | ||
| CLOSED = 'CLOSED', | ||
| /** After threshold failures, all requests rejected immediately */ | ||
| OPEN = 'OPEN', | ||
| /** After timeout, allows test requests to check if endpoint recovered */ | ||
| HALF_OPEN = 'HALF_OPEN', | ||
| } | ||
|
|
||
| /** | ||
| * Configuration for circuit breaker behavior. | ||
| */ | ||
| export interface CircuitBreakerConfig { | ||
| /** Number of consecutive failures before opening the circuit */ | ||
| failureThreshold: number; | ||
| /** Time in milliseconds to wait before attempting recovery */ | ||
| timeout: number; | ||
| /** Number of consecutive successes in HALF_OPEN state to close the circuit */ | ||
| successThreshold: number; | ||
| } | ||
|
|
||
| /** | ||
| * Default circuit breaker configuration. | ||
| */ | ||
| export const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig = { | ||
| failureThreshold: 5, | ||
| timeout: 60000, // 1 minute | ||
| successThreshold: 2, | ||
| }; | ||
|
|
||
| /** | ||
| * Circuit breaker for telemetry exporter. | ||
| * Protects against failing telemetry endpoint with automatic recovery. | ||
| * | ||
| * States: | ||
| * - CLOSED: Normal operation, requests pass through | ||
| * - OPEN: After threshold failures, all requests rejected immediately | ||
| * - HALF_OPEN: After timeout, allows test requests to check if endpoint recovered | ||
| */ | ||
| export class CircuitBreaker { | ||
| private state: CircuitBreakerState = CircuitBreakerState.CLOSED; | ||
|
|
||
| private failureCount = 0; | ||
|
|
||
| private successCount = 0; | ||
|
|
||
| private nextAttempt?: Date; | ||
|
|
||
| private readonly config: CircuitBreakerConfig; | ||
|
|
||
| constructor(private context: IClientContext, config?: Partial<CircuitBreakerConfig>) { | ||
| this.config = { | ||
| ...DEFAULT_CIRCUIT_BREAKER_CONFIG, | ||
| ...config, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Executes an operation with circuit breaker protection. | ||
| * | ||
| * @param operation The operation to execute | ||
| * @returns Promise resolving to the operation result | ||
| * @throws Error if circuit is OPEN or operation fails | ||
| */ | ||
| async execute<T>(operation: () => Promise<T>): Promise<T> { | ||
| const logger = this.context.getLogger(); | ||
|
|
||
| // Check if circuit is open | ||
| if (this.state === CircuitBreakerState.OPEN) { | ||
| if (this.nextAttempt && Date.now() < this.nextAttempt.getTime()) { | ||
| throw new Error('Circuit breaker OPEN'); | ||
| } | ||
| // Timeout expired, transition to HALF_OPEN | ||
| this.state = CircuitBreakerState.HALF_OPEN; | ||
| this.successCount = 0; | ||
| logger.log(LogLevel.debug, 'Circuit breaker transitioned to HALF_OPEN'); | ||
| } | ||
|
|
||
| try { | ||
| const result = await operation(); | ||
| this.onSuccess(); | ||
| return result; | ||
| } catch (error) { | ||
| this.onFailure(); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current state of the circuit breaker. | ||
| */ | ||
| getState(): CircuitBreakerState { | ||
| return this.state; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current failure count. | ||
| */ | ||
| getFailureCount(): number { | ||
| return this.failureCount; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current success count (relevant in HALF_OPEN state). | ||
| */ | ||
| getSuccessCount(): number { | ||
| return this.successCount; | ||
| } | ||
|
|
||
| /** | ||
| * Handles successful operation execution. | ||
| */ | ||
| private onSuccess(): void { | ||
| const logger = this.context.getLogger(); | ||
|
|
||
| // Reset failure count on any success | ||
| this.failureCount = 0; | ||
|
|
||
| if (this.state === CircuitBreakerState.HALF_OPEN) { | ||
| this.successCount += 1; | ||
| logger.log( | ||
| LogLevel.debug, | ||
| `Circuit breaker success in HALF_OPEN (${this.successCount}/${this.config.successThreshold})`, | ||
| ); | ||
|
|
||
| if (this.successCount >= this.config.successThreshold) { | ||
| // Transition to CLOSED | ||
| this.state = CircuitBreakerState.CLOSED; | ||
| this.successCount = 0; | ||
| this.nextAttempt = undefined; | ||
| logger.log(LogLevel.debug, 'Circuit breaker transitioned to CLOSED'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles failed operation execution. | ||
| */ | ||
| private onFailure(): void { | ||
| const logger = this.context.getLogger(); | ||
|
|
||
| this.failureCount += 1; | ||
| this.successCount = 0; // Reset success count on failure | ||
|
|
||
| logger.log(LogLevel.debug, `Circuit breaker failure (${this.failureCount}/${this.config.failureThreshold})`); | ||
|
|
||
| if (this.failureCount >= this.config.failureThreshold) { | ||
| // Transition to OPEN | ||
| this.state = CircuitBreakerState.OPEN; | ||
| this.nextAttempt = new Date(Date.now() + this.config.timeout); | ||
| logger.log(LogLevel.debug, `Circuit breaker transitioned to OPEN (will retry after ${this.config.timeout}ms)`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Manages circuit breakers per host. | ||
| * Ensures each host has its own isolated circuit breaker to prevent | ||
| * failures on one host from affecting telemetry to other hosts. | ||
| */ | ||
| export class CircuitBreakerRegistry { | ||
| private breakers: Map<string, CircuitBreaker>; | ||
|
|
||
| constructor(private context: IClientContext) { | ||
| this.breakers = new Map(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets or creates a circuit breaker for the specified host. | ||
| * | ||
| * @param host The host identifier (e.g., "workspace.cloud.databricks.com") | ||
| * @param config Optional configuration overrides | ||
| * @returns Circuit breaker for the host | ||
| */ | ||
| getCircuitBreaker(host: string, config?: Partial<CircuitBreakerConfig>): CircuitBreaker { | ||
| let breaker = this.breakers.get(host); | ||
| if (!breaker) { | ||
| breaker = new CircuitBreaker(this.context, config); | ||
| this.breakers.set(host, breaker); | ||
| const logger = this.context.getLogger(); | ||
| logger.log(LogLevel.debug, `Created circuit breaker for host: ${host}`); | ||
| } | ||
| return breaker; | ||
| } | ||
|
|
||
| /** | ||
| * Gets all registered circuit breakers. | ||
| * Useful for testing and diagnostics. | ||
| */ | ||
| getAllBreakers(): Map<string, CircuitBreaker> { | ||
| return new Map(this.breakers); | ||
| } | ||
|
|
||
| /** | ||
| * Removes a circuit breaker for the specified host. | ||
| * Useful for cleanup when a host is no longer in use. | ||
| * | ||
| * @param host The host identifier | ||
| */ | ||
| removeCircuitBreaker(host: string): void { | ||
| this.breakers.delete(host); | ||
| const logger = this.context.getLogger(); | ||
| logger.log(LogLevel.debug, `Removed circuit breaker for host: ${host}`); | ||
| } | ||
|
|
||
| /** | ||
| * Clears all circuit breakers. | ||
| * Useful for testing. | ||
| */ | ||
| clear(): void { | ||
| this.breakers.clear(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
try use some existing library for circuitbreaker