Skip to content

Commit

Permalink
feat(): reorder spreading of properties to prioritize preserving cust…
Browse files Browse the repository at this point in the history
…om-set properties and custom metadata; add note on this to README
  • Loading branch information
mikaelvesavuori committed Nov 15, 2022
1 parent 6c74649 commit 6bdb8ff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ You can set `MIKROMETRIC_NAMESPACE` and/or `MIKROMETRIC_SERVICE_NAME` respective

**Any values manually passed in will always take precedence**.

## Behavior of output

Output fields will be spread (and potentially deduplicating same-named properties) in the following order:

1. Dynamic metadata (AWS)
2. Custom static metadata
3. Properties

A higher number means that fields in that category will persist if any fields have same names in lower-numbered categories.

## License

MIT. See `LICENSE` file.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mikrometric",
"description": "mikrometric is a Lambda-oriented lightweight wrapper for producing AWS CloudWatch Embedded Metric Format-compatible metric logs.",
"version": "1.0.5",
"version": "1.0.6",
"author": "Mikael Vesavuori",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/domain/entities/MikroMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ export class MikroMetric {
*/
private createMetricObject(): MetricObject {
const completeMetric = this.sortOutput({
...MikroMetric.metric,
...this.createDynamicMetadata(),
...MikroMetric.metadataConfig,
...this.createDynamicMetadata()
...MikroMetric.metric
});

const filtered = this.filterMetadata(completeMetric);
Expand Down
17 changes: 17 additions & 0 deletions tests/MikroMetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ test.serial('It should set AWS metadata with provided "event" and "context" obje
};

const result: any = mikroMetric.flush();
console.log('result', result);
delete result['_aws'];
delete result['id'];
delete result['timestamp'];
Expand Down Expand Up @@ -135,6 +136,22 @@ test.serial('It should set AWS metadata from environment', (t) => {
clearEnv();
});

test.serial(
'It should keep directly set/put properties in output if the property name overlap with dynamic metadata',
(t) => {
setEnv();

const expected = 'Sam Person';
const mikroMetric = MikroMetric.start(config);

mikroMetric.putMetric('user', expected);
const result: any = mikroMetric.flush().user;

t.deepEqual(result, expected);
clearEnv();
}
);

test.serial('It should work without AWS metadata', (t) => {
const mikroMetric = MikroMetric.start(configMinimal);

Expand Down

0 comments on commit 6bdb8ff

Please sign in to comment.