Skip to content
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

feat(node): add paramters to forward OTEL Headers to Clickhouse #395

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.10.2

## Improvements
- Added `opentelemetry_headers` option to the client configuration. ([#394](https://github.com/ClickHouse/clickhouse-js/issues/374))

# 1.10.1 (Common, Node.js, Web)

## Bug fixes
Expand Down
7 changes: 7 additions & 0 deletions packages/client-common/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface BaseQueryParams {
password: string
}
| { access_token: string }
/** OpenTelemetry headers to propagate to ClickHouse.
* @default undefined (no override) */
opentelemetry_headers?: {
traceparent?: string
tracestate?: string
}
}

export interface QueryParams extends BaseQueryParams {
Expand Down Expand Up @@ -308,6 +314,7 @@ export class ClickHouseClient<Stream = unknown> {
session_id: params.session_id ?? this.sessionId,
role: params.role ?? this.role,
auth: params.auth,
opentelemetry_headers: params.opentelemetry_headers,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/client-common/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface ConnBaseQueryParams {
query_id?: string
auth?: { username: string; password: string } | { access_token: string }
role?: string | Array<string>
opentelemetry_headers?: {
traceparent?: string
tracestate?: string
}
}

export interface ConnInsertParams<Stream> extends ConnBaseQueryParams {
Expand Down
33 changes: 33 additions & 0 deletions packages/client-node/__tests__/unit/node_connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,37 @@ describe('[Node.js] Connection', () => {
expect(url.search).toContain(`?query_id=${query_id}`)
})
})

describe('opentelemetry_headers', () => {
it('should set the traceparent and tracestate on request', async () => {
const adapter = buildHttpConnection({
compression: {
decompress_response: false,
compress_request: false,
},
})

const httpRequestStub = spyOn(Http, 'request')

const request1 = stubClientRequest()
httpRequestStub.and.returnValue(request1)

const traceparent =
'00-12345678901234567890123456789012-1234567890123456-01'

const tracestate = 'rojo=00f067aa0ba902b7'

const cmdPromise = adapter.command({
query: 'SELECT * FROM system.numbers LIMIT 5',
opentelemetry_headers: { traceparent, tracestate },
})
await emitResponseBody(request1, 'Ok.')
await cmdPromise

const headers = httpRequestStub.calls.argsFor(0)[1].headers!

expect(headers['traceparent']).toBe(traceparent)
expect(headers['tracestate']).toBe(tracestate)
})
})
})
18 changes: 14 additions & 4 deletions packages/client-node/src/connection/node_base_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,37 @@ export abstract class NodeBaseConnection
protected buildRequestHeaders(
params?: BaseQueryParams,
): Http.OutgoingHttpHeaders {
const headers = { ...this.defaultHeaders }

if (params?.opentelemetry_headers?.traceparent) {
headers['traceparent'] = params.opentelemetry_headers.traceparent
}

if (params?.opentelemetry_headers?.tracestate) {
headers['tracestate'] = params.opentelemetry_headers.tracestate
}

if (isJWTAuth(params?.auth)) {
return {
...this.defaultHeaders,
...headers,
Authorization: `Bearer ${params.auth.access_token}`,
}
}
if (this.params.set_basic_auth_header) {
if (isCredentialsAuth(params?.auth)) {
return {
...this.defaultHeaders,
...headers,
Authorization: `Basic ${Buffer.from(`${params.auth.username}:${params.auth.password}`).toString('base64')}`,
}
} else {
return {
...this.defaultHeaders,
...headers,
Authorization: this.defaultAuthHeader,
}
}
}
return {
...this.defaultHeaders,
...headers,
}
}

Expand Down
Loading