-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): pg-pool plugin implementation (#501)
* feat: pg-pool plugin implementation * feat: pg-pool plugin implementation * fix: linting * fix: add attributes for span & add tests for pool.query() * fix: add span.setStatus * chore: address comments * fix: linting Co-authored-by: Mayur Kale <[email protected]>
- Loading branch information
1 parent
38ce1c3
commit 1a2d926
Showing
8 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/enums.ts
This file contains 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,36 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
export enum AttributeNames { | ||
// required by https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md#databases-client-calls | ||
COMPONENT = 'component', | ||
DB_TYPE = 'db.type', | ||
DB_INSTANCE = 'db.instance', | ||
DB_STATEMENT = 'db.statement', | ||
PEER_ADDRESS = 'peer.address', | ||
PEER_HOSTNAME = 'peer.host', | ||
|
||
// optional | ||
DB_USER = 'db.user', | ||
PEER_PORT = 'peer.port', | ||
PEER_IPV4 = 'peer.ipv4', | ||
PEER_IPV6 = 'peer.ipv6', | ||
PEER_SERVICE = 'peer.service', | ||
|
||
// PG-POOL specific -- not specified by spec | ||
IDLE_TIMEOUT_MILLIS = 'idle.timeout.millis', | ||
MAX_CLIENT = 'max', | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/index.ts
This file contains 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,17 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
export * from './pg-pool'; |
This file contains 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
39 changes: 39 additions & 0 deletions
39
packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/types.ts
This file contains 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,39 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 * as pgTypes from 'pg'; | ||
import * as pgPoolTypes from 'pg-pool'; | ||
|
||
export interface PostgresPoolPluginOptions {} | ||
|
||
export type PgPoolCallback = ( | ||
err: Error, | ||
client: any, | ||
done: (release?: any) => void | ||
) => void; | ||
|
||
export interface PgPoolOptionsParams { | ||
database: string; | ||
host: string; | ||
port: number; | ||
user: string; | ||
idleTimeoutMillis: number; // the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time | ||
maxClient: number; // maximum size of the pool | ||
} | ||
|
||
export interface PgPoolExtended extends pgPoolTypes<pgTypes.Client> { | ||
options: PgPoolOptionsParams; | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/utils.ts
This file contains 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,45 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { Span, CanonicalCode } from '@opentelemetry/types'; | ||
import { PgPoolOptionsParams, PgPoolCallback, PgPoolExtended } from './types'; | ||
|
||
export function getJDBCString(params: PgPoolOptionsParams) { | ||
const host = params.host || 'localhost'; // postgres defaults to localhost | ||
const port = params.port || 5432; // postgres defaults to port 5432 | ||
const database = params.database || ''; | ||
return `jdbc:postgresql://${host}:${port}/${database}`; | ||
} | ||
|
||
export function patchCallback(span: Span, cb: PgPoolCallback): PgPoolCallback { | ||
return function patchedCallback( | ||
this: PgPoolExtended, | ||
err: Error, | ||
res: object, | ||
done: any | ||
) { | ||
if (err) { | ||
span.setStatus({ | ||
code: CanonicalCode.UNKNOWN, | ||
message: err.message, | ||
}); | ||
} else if (res) { | ||
span.setStatus({ code: CanonicalCode.OK }); | ||
} | ||
span.end(); | ||
cb.call(this, err, res, done); | ||
}; | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts
This file contains 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,79 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { | ||
SpanKind, | ||
Attributes, | ||
Event, | ||
Span, | ||
TimedEvent, | ||
} from '@opentelemetry/types'; | ||
import * as assert from 'assert'; | ||
import { ReadableSpan } from '@opentelemetry/tracing'; | ||
import { | ||
hrTimeToMilliseconds, | ||
hrTimeToMicroseconds, | ||
} from '@opentelemetry/core'; | ||
|
||
export const assertSpan = ( | ||
span: ReadableSpan, | ||
kind: SpanKind, | ||
attributes: Attributes, | ||
events: Event[] | ||
) => { | ||
assert.strictEqual(span.spanContext.traceId.length, 32); | ||
assert.strictEqual(span.spanContext.spanId.length, 16); | ||
assert.strictEqual(span.kind, kind); | ||
|
||
// check all the AttributeNames fields | ||
Object.keys(span.attributes).forEach(key => { | ||
assert.deepStrictEqual(span.attributes[key], attributes[key]); | ||
}); | ||
|
||
assert.ok(span.endTime); | ||
assert.strictEqual(span.links.length, 0); | ||
|
||
assert.ok( | ||
hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) | ||
); | ||
assert.ok(hrTimeToMilliseconds(span.endTime) > 0); | ||
|
||
// events | ||
assert.strictEqual( | ||
span.events.length, | ||
events.length, | ||
'Should contain same number of events' | ||
); | ||
span.events.forEach((_: TimedEvent, index: number) => { | ||
assert.deepStrictEqual(span.events[index], events[index]); | ||
}); | ||
}; | ||
|
||
// Check if sourceSpan was propagated to targetSpan | ||
export const assertPropagation = ( | ||
childSpan: ReadableSpan, | ||
parentSpan: Span | ||
) => { | ||
const targetSpanContext = childSpan.spanContext; | ||
const sourceSpanContext = parentSpan.context(); | ||
assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); | ||
assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); | ||
assert.strictEqual( | ||
targetSpanContext.traceFlags, | ||
sourceSpanContext.traceFlags | ||
); | ||
assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); | ||
}; |
Oops, something went wrong.