Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
40 changes: 40 additions & 0 deletions packages/node/src/integrations/tracing/clickhouse/clickhouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Span } from '@opentelemetry/api';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { addOriginToSpan, generateInstrumentOnce } from '@sentry/node-core';
import { ClickHouseInstrumentation } from './instrumentation'

const INTEGRATION_NAME = 'Clickhouse';

export const instrumentClickhouse = generateInstrumentOnce(
INTEGRATION_NAME,
() =>
new ClickHouseInstrumentation({
responseHook(span: Span) {
addOriginToSpan(span, 'auto.db.otel.clickhouse');
},
}),
);

const _clickhouseIntegration = (() => {
return {
name: INTEGRATION_NAME,
setupOnce() {
instrumentClickhouse();
},
};
}) satisfies IntegrationFn;

/**
* Adds Sentry tracing instrumentation for the [ClickHouse](https://www.npmjs.com/package/@clickhouse/client) library.
*
* @example
* ```javascript
* const Sentry = require('@sentry/node');
*
* Sentry.init({
* integrations: [Sentry.clickhouseIntegration()],
* });
* ```
*/
export const clickhouseIntegration = defineIntegration(_clickhouseIntegration);
3 changes: 3 additions & 0 deletions packages/node/src/integrations/tracing/clickhouse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { clickhouseIntegration, instrumentClickhouse } from './clickhouse';
export { ClickHouseInstrumentation } from './instrumentation';
export type { ClickHouseInstrumentationConfig } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { InstrumentationModuleDefinition } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
import { SDK_VERSION } from '@sentry/core';
import { type ClickHouseModuleExports,patchClickHouseClient } from './patch';
import type { ClickHouseInstrumentationConfig } from './types';

const PACKAGE_NAME = '@sentry/instrumentation-clickhouse';
const supportedVersions = ['>=0.0.1'];

/**
*
*/
export class ClickHouseInstrumentation extends InstrumentationBase<ClickHouseInstrumentationConfig> {
public constructor(config: ClickHouseInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

/**
*
*/
public override init(): InstrumentationModuleDefinition {
return new InstrumentationNodeModuleDefinition(
'@clickhouse/client',
supportedVersions,
moduleExports =>
patchClickHouseClient(moduleExports as ClickHouseModuleExports, {
wrap: this._wrap.bind(this),
unwrap: this._unwrap.bind(this),
tracer: this.tracer,
getConfig: this.getConfig.bind(this),
isEnabled: this.isEnabled.bind(this),
}),
moduleExports => {
const moduleExportsTyped = moduleExports as ClickHouseModuleExports;
const ClickHouseClient = moduleExportsTyped.ClickHouseClient;
if (ClickHouseClient && typeof ClickHouseClient === 'function' && 'prototype' in ClickHouseClient) {
const ClickHouseClientCtor = ClickHouseClient as new () => {
query: unknown;
insert: unknown;
exec: unknown;
command: unknown;
};
this._unwrap(ClickHouseClientCtor.prototype, 'query');
this._unwrap(ClickHouseClientCtor.prototype, 'insert');
this._unwrap(ClickHouseClientCtor.prototype, 'exec');
this._unwrap(ClickHouseClientCtor.prototype, 'command');
}
},
);
}
}
Loading