Skip to content

Commit fbf830e

Browse files
authored
feat: towards decoupling from metrics component - make it optional (#131)
* feat: decouple from metrics component making it optional * chore: update api
1 parent 545f1a2 commit fbf830e

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ To create the component you have the option of:
1010

1111
- Supplying any `PoolConfig` options you want. This are related 1:1 with [node-postgres](https://node-postgres.com/api/pool) implementation, which in turn are all variables [Postgres](https://www.postgresql.org/) supports
1212
- Supplying any `RunnerOption`. This are related 1:1 with [node-pg-migrate](https://github.com/salsita/node-pg-migrate) implementation and will be used for [migrations](#migrations)
13+
- Supplying a Metrics component ([any `IMetricsComponent`](https://github.com/well-known-components/metrics)). This component will enable additional functionality for collecting metrics on querys.
1314

1415
```ts
1516
// src/components.ts
1617

17-
await createPgComponent({ config, logs, metrics } /* optional config here */)
18+
await createPgComponent({ config, logs, metrics: metrics | undefined } /* optional config here */)
1819

1920
// A possible optional'd look like:
2021
const { config } = createConfigComponent()
@@ -51,6 +52,7 @@ the API looks like this
5152

5253
```ts
5354
query<T>(sql: string): Promise<IDatabase.IQueryResult<T>>
55+
// If it has been built with the metrics component this contract is also exposed:
5456
query<T>(sql: SQLStatement, durationQueryNameLabel?: string): Promise<IDatabase.IQueryResult<T>>
5557
```
5658

@@ -62,7 +64,7 @@ const id = getIdFromUnreliableSource()
6264
pg.query<TableType>(SQL`SELECT * FROM table WHERE id = ${id}`) // this results in ['SELECT * FROM table WHERE id = $1', id]
6365
```
6466

65-
suppliying a `durationQueryNameLabel` will trigger a metrics increment with that name.
67+
if `metrics` component has been provided, suppliying a `durationQueryNameLabel` will trigger a metrics increment with that name.
6668

6769
**Streaming a query**
6870

etc/pg-component.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export namespace createPgComponent {
2424
export type NeededComponents = {
2525
logs: ILoggerComponent;
2626
config: IConfigComponent;
27-
metrics: IMetricsComponent;
27+
metrics?: IMetricsComponent;
2828
};
2929
}
3030

src/index.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,17 @@ export async function createPgComponent(
6868
}
6969
}
7070

71-
async function query<T extends Record<string, any>>(sql: string): Promise<IDatabase.IQueryResult<T>>
72-
async function query<T extends Record<string, any>>(
73-
sql: SQLStatement,
74-
durationQueryNameLabel?: string
75-
): Promise<IDatabase.IQueryResult<T>>
76-
async function query<T extends Record<string, any>>(
77-
sql: string | SQLStatement,
78-
durationQueryNameLabel?: string
79-
): Promise<IDatabase.IQueryResult<T>> {
80-
const rows = durationQueryNameLabel
81-
? await runReportingQueryDurationMetric(components, durationQueryNameLabel, () => pool.query<T>(sql))
82-
: await pool.query<T>(sql)
83-
84-
return {
85-
rows: rows.rows,
86-
rowCount: rows.rowCount ?? 0
87-
}
71+
async function defaultQuery<T extends Record<string, any>>(sql: string | SQLStatement): Promise<IDatabase.IQueryResult<T>> {
72+
const result = await pool.query<T>(sql)
73+
return { ...result, rowCount: result.rowCount ?? 0 }
74+
}
75+
76+
async function measuredQuery<T extends Record<string, any>>(sql: string | SQLStatement, durationQueryNameLabel?: string): Promise<IDatabase.IQueryResult<T>> {
77+
const result = durationQueryNameLabel
78+
? await runReportingQueryDurationMetric({ metrics: components.metrics! }, durationQueryNameLabel, () => defaultQuery<T>(sql))
79+
: await defaultQuery<T>(sql)
80+
81+
return result
8882
}
8983

9084
async function* streamQuery<T>(sql: SQLStatement, config?: { batchSize?: number }): AsyncGenerator<T> {
@@ -169,7 +163,7 @@ export async function createPgComponent(
169163
}
170164

171165
return {
172-
query,
166+
query: components.metrics ? measuredQuery : defaultQuery,
173167
streamQuery,
174168
getPool,
175169
start,
@@ -184,6 +178,6 @@ export namespace createPgComponent {
184178
export type NeededComponents = {
185179
logs: ILoggerComponent
186180
config: IConfigComponent
187-
metrics: IMetricsComponent
181+
metrics?: IMetricsComponent
188182
}
189183
}

0 commit comments

Comments
 (0)