Skip to content

Commit

Permalink
FIX: emit and query options.log
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Sep 13, 2023
1 parent 9b5371c commit f4be9d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CustomMetrics stores metrics to a DynamoDB table of your choosing that can coexi
- Written in TypeScript with full TypeScript support.
- Clean, readable, small, TypeScript code base (~1.3K lines).
- [SenseDeep](https://www.sensedeep.com) support for visualizing and graphing metrics.
- [DynamoDB Onetable](https://www.npmjs.com/package/dynamodb-onetable) support CustomMetrics for detailed single table metrics.

## Database

Expand Down Expand Up @@ -402,7 +403,8 @@ async emit(namespace: string,
sum: number,
count: number,
elapsed: number,
}
},
log: boolean,
}): Promise<Metric>
```

Expand All @@ -424,6 +426,8 @@ This will create three metrics:

The `buffer` option can be provided to optimize metric load by aggregating calls to emit(). See [Buffering](#buffering) for details.

The `log` option if set to true, will emit debug trace to the console.

### query

Query a metric value.
Expand All @@ -450,6 +454,8 @@ If `options.owner` is provided, it overrides the default owner or the `owner` gi
If `options.id` is provided, the ID will be returned in the corresponding result items. This can help to correlate parallel queries with results.
If `options.log` is set to true, this will emit debug trace to the console.
### getMetricList
Return a list of supported namespaces, metrics and dimensions.
Expand All @@ -476,6 +482,7 @@ If a namespace argument is provided, the list of metrics in that namespace will
### References
- [DynamoDB OneTable](https://www.npmjs.com/package/dynamodb-onetable)
- [SenseDeep Blog](https://www.sensedeep.com/blog/)
- [SenseDeep Web Site](https://www.sensedeep.com/)
- [SenseDeep Developer Studio](https://app.sensedeep.com/)
Expand Down
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class CustomMetrics {
let chan = options.log == true ? 'info' : 'trace'
do {
let owner = options.owner || this.owner
metric = await this.getMetric(owner, namespace, metricName, dimensions)
metric = await this.getMetric(owner, namespace, metricName, dimensions, options.log)
if (!metric) {
metric = this.initMetric(owner, namespace, metricName, dimensions)
}
Expand Down Expand Up @@ -487,7 +487,7 @@ export class CustomMetrics {
*/
let dimString = this.makeDimensionString(dimensions)

let metric = await this.getMetric(owner, namespace, metricName, dimString)
let metric = await this.getMetric(owner, namespace, metricName, dimString, options.log)
if (!metric) {
return {dimensions, id: options.id, metric: metricName, namespace, period, points: [], owner, samples: 0}
}
Expand Down Expand Up @@ -892,7 +892,7 @@ export class CustomMetrics {
return metric
}

async getMetric(owner: string, namespace: string, metric: string, dimensions: string): Promise<Metric> {
async getMetric(owner: string, namespace: string, metric: string, dimensions: string, log: boolean): Promise<Metric> {
let command = new GetItemCommand({
TableName: this.table,
Key: {
Expand All @@ -902,11 +902,16 @@ export class CustomMetrics {
ConsistentRead: this.consistent,
})
let data = await this.client.send(command)
let result = null
if (data && data.Item) {
let item = unmarshall(data.Item)
return this.mapItemFromDB(item)
result = this.mapItemFromDB(item)
}
return null
if (log == true) {
let chan = log == true ? 'info' : 'trace'
this.log[chan](`GetMetric ${namespace}, ${metric} ${dimensions}`, {cmd: command, result})
}
return result
}

async findMetrics(
Expand Down Expand Up @@ -989,7 +994,7 @@ export class CustomMetrics {

/* istanbul ignore next */
let chan = options.log == true ? 'info' : 'trace'
this.log[chan](`Put metric ${item.namespace}, ${item.metric}`, {dimensions: item.dimensions, command})
this.log[chan](`Put metric ${item.namespace}, ${item.metric}`, {dimensions: item.dimensions, command, params, item})

try {
await this.client.send(command)
Expand Down

0 comments on commit f4be9d5

Please sign in to comment.