Skip to content

Commit

Permalink
fix(): remove lifecycleStage from metadata; rename 'route' to 'resour…
Browse files Browse the repository at this point in the history
…ce'; make dataSensitivity a string and therefore more flexible
  • Loading branch information
mikaelvesavuori committed Nov 7, 2022
1 parent c841fc2 commit 8758113
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 59 deletions.
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ Under the hood, MikroLog is built and tested around practically the same metadat
```typescript
const metadataConfig = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand All @@ -267,23 +266,23 @@ The dynamic metadata fields are picked up automatically if you pass them in duri

If these values are not available, they will be dropped at the time of log output. In effect, this means you won't have to deal with them (being empty or otherwise) if you use MikroLog in another type of context.

| Field | Type | Description |
| -------------------- | ------ | ------------------------------------------------------------------------------ |
| `accountId` | string | The AWS account ID that the system is running in. |
| `correlationId` | string | Correlation ID for this function call. |
| `functionMemorySize` | string | Memory size of the current function. |
| `functionName` | string | The name of the function. |
| `functionVersion` | string | The version of the function. |
| `id` | string | ID of the log. |
| `region` | string | The region of the responding function/system. |
| `route` | string | The route that is responding. In EventBridge, this will be your `detail-type`. |
| `runtime` | string | What runtime is used? |
| `stage` | string | What AWS stage are we in? |
| `timestamp` | string | Timestamp of this message in ISO 8601 (RFC 3339) format. |
| `timestampEpoch` | string | Timestamp of this message in Unix epoch. |
| `timestampRequest` | string | Request time in Unix epoch of the incoming request. |
| `user` | string | The user in this log context. |
| `viewerCountry` | string | Which country did AWS CloudFront infer the user to be in? |
| Field | Type | Description |
| -------------------- | ------ | --------------------------------------------------------- |
| `accountId` | string | The AWS account ID that the system is running in. |
| `correlationId` | string | Correlation ID for this function call. |
| `functionMemorySize` | string | Memory size of the current function. |
| `functionName` | string | The name of the function. |
| `functionVersion` | string | The version of the function. |
| `id` | string | ID of the log. |
| `region` | string | The region of the responding function/system. |
| `resource` | string | The resource (channel, URL path...) that is responding. |
| `runtime` | string | What runtime is used? |
| `stage` | string | What AWS stage are we in? |
| `timestamp` | string | Timestamp of this message in ISO 8601 (RFC 3339) format. |
| `timestampEpoch` | string | Timestamp of this message in Unix epoch. |
| `timestampRequest` | string | Request time in Unix epoch of the incoming request. |
| `user` | string | The user in this log context. |
| `viewerCountry` | string | Which country did AWS CloudFront infer the user to be in? |

## Redacting keys or masking values

Expand Down
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": "mikrolog",
"description": "The JSON logger you always wanted for Lambda.",
"version": "2.1.3",
"version": "2.1.4",
"author": "Mikael Vesavuori",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
produceFunctionMemorySize,
produceFunctionVersion,
produceCorrelationId,
produceRoute,
produceResource,
produceUser,
produceStage,
produceViewerCountry,
Expand Down Expand Up @@ -226,7 +226,7 @@ export class MikroLog {
functionVersion: produceFunctionVersion(MikroLog.context),
correlationId:
MikroLog.correlationId || produceCorrelationId(MikroLog.event, MikroLog.context),
route: produceRoute(MikroLog.event),
resource: produceResource(MikroLog.event),
user: produceUser(MikroLog.event),
stage: produceStage(MikroLog.event),
viewerCountry: produceViewerCountry(MikroLog.event)
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/metadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export function produceFunctionVersion(context: any): string {
}

/**
* @description Set the route (Lambda) or `detail-type` (EventBridge).
* @description Set the resource from `path` (Lambda) or `detail-type` (EventBridge).
*/
export function produceRoute(event: any): string {
export function produceResource(event: any): string {
if (event && event['detail-type']) return event['detail-type'];
else if (event && event['path']) return event['path'];
return '';
Expand Down
29 changes: 8 additions & 21 deletions src/interfaces/Metadata.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* @description Valid lifecycles stages.
*/
type LifecycleStage = 'production' | 'qa' | 'test' | 'development' | 'staging' | 'demo';

/**
* @description Valid data sensitivity levels.
*/
type DataSensitivity = 'public' | 'sensitive' | 'proprietary' | 'secret';

/**
* @description User-provided static metadata input.
*/
Expand All @@ -16,10 +6,6 @@ export interface StaticMetadataConfigInput {
* @description The version of the logged service.
*/
version: number;
/**
* @description Which lifecycle stage the logged service pertains to.
*/
lifecycleStage: LifecycleStage;
/**
* @description The organization that owns this system.
*/
Expand Down Expand Up @@ -50,8 +36,9 @@ export interface StaticMetadataConfigInput {
tags?: string[];
/**
* @description Data sensitivity classification for the contents of this log.
* @example `public`, `proprietary`, `confidential`, `secret`
*/
dataSensitivity?: DataSensitivity;
dataSensitivity?: string;
/**
* @description What legal jurisdiction does this system fall into?
* @example `EU`, `US`, `CN`
Expand All @@ -66,6 +53,10 @@ export interface DynamicMetadataOutput {
* @description ID of the log.
*/
id: string;
/**
* @description Correlation ID for this function call.
*/
correlationId: string;
/**
* @description Timestamp of this message in ISO 8601 (RFC 3339) format.
*/
Expand All @@ -78,19 +69,15 @@ export interface DynamicMetadataOutput {
* @description Request time in Unix epoch of the incoming request.
*/
timestampRequest: string;
/**
* @description Correlation ID for this function call.
*/
correlationId: string;
/**
* @description The user in this log context.
*/
user: string;
/**
* @description The route that is responding. In EventBridge, this will be your detail type.
* @description The resource (channel, URL path...) that is responding.
* @example `/doSomething`
*/
route: string;
resource: string;
/**
* @description The region of the responding function/system.
*/
Expand Down
1 change: 0 additions & 1 deletion testdata/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { StaticMetadataConfigInput } from '../src/interfaces/Metadata';

export const metadataConfig: StaticMetadataConfigInput = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down
7 changes: 0 additions & 7 deletions tests/MikroLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ test.serial('It should return (print out) a structured log when given a string m

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -115,7 +114,6 @@ test.serial(

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -166,7 +164,6 @@ test.serial(

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -217,7 +214,6 @@ test.serial(

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -268,7 +264,6 @@ test.serial(

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -451,7 +446,6 @@ test.serial('It should redact keys when given a "redactedKeys" list', (t) => {

const expected: any = {
version: 1,
lifecycleStage: 'production',
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
Expand Down Expand Up @@ -506,7 +500,6 @@ test.serial('It should mask values when given a "maskedValues" list', (t) => {
hostPlatform: 'aws',
httpStatusCode: 400,
level: 'ERROR',
lifecycleStage: 'production',
message: 'Hello World',
owner: 'MyCompany',
service: 'UserSignUp',
Expand Down
10 changes: 5 additions & 5 deletions tests/metadataUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,25 @@ test.serial('It should set function version', (t) => {
process.env.AWS_LAMBDA_FUNCTION_VERSION = '';
});

test.serial('It should set route (or detail type) if given event path', (t) => {
test.serial('It should set resource (or detail type) if given event path', (t) => {
MikroLog.reset();
const logger = MikroLog.start();
MikroLog.enrich({ event, context });
const expected = '/functionName';
const response = logger.log('something').route;
const response = logger.log('something').resource;

t.is(response, expected);
});

test.serial('It should set route (or detail type) if given event detail-type', (t) => {
test.serial('It should set resource (or detail type) if given event detail-type', (t) => {
MikroLog.reset();
const logger = MikroLog.start();
const _event: any = JSON.parse(JSON.stringify(event));
_event['path'] = '';
_event['detail-type'] = 'SomeDetail';
MikroLog.enrich({ event: _event, context });
const expected = 'SomeDetail';
const response = logger.log('something').route;
const response = logger.log('something').resource;

t.is(response, expected);
});
Expand Down Expand Up @@ -246,7 +246,7 @@ test.serial('It should emit a full log and filter out any empty fields', (t) =>
region: 'eu-north-1',
id: 'a6b1caa3-8a8d-4dc0-8828-e10f63876f9f',
timestampRequest: '1657389598171',
route: '/functionName',
resource: '/functionName',
stage: 'shared',
timestamp: '1657393943792',
timestampEpoch: '2022-07-09T19:12:23.792Z',
Expand Down

0 comments on commit 8758113

Please sign in to comment.