-
Notifications
You must be signed in to change notification settings - Fork 825
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(plugin): pg-pool plugin implementation #501
Merged
mayurkale22
merged 14 commits into
open-telemetry:master
from
xiao-lix:xiao/pg-pool-implement
Dec 23, 2019
+613
−0
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dad84ac
feat: pg-pool plugin implementation
xiao-lix 3532248
feat: pg-pool plugin implementation
xiao-lix d27c209
Merge branch 'master' into xiao/pg-pool-implement
xiao-lix 029ed4a
fix: linting
xiao-lix d3dd97b
Merge branch 'xiao/pg-pool-implement' of github.com:xiao-lix/opentele…
xiao-lix a9548ba
fix: add attributes for span & add tests for pool.query()
xiao-lix 3265455
fix: add span.setStatus
xiao-lix b67210e
Merge branch 'master' into xiao/pg-pool-implement
mayurkale22 d29463b
Merge branch 'master' into xiao/pg-pool-implement
mayurkale22 2b7663a
chore: address comments
xiao-lix afdd264
Merge branch 'xiao/pg-pool-implement' of github.com:xiao-lix/opentele…
xiao-lix 87e5375
fix: linting
xiao-lix b1490f3
Merge branch 'master' into xiao/pg-pool-implement
mayurkale22 bbd104e
Merge branch 'master' into xiao/pg-pool-implement
mayurkale22 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
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.
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.
Are any of the attributes from pg applicable here?
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.
yes, will fix this.