Skip to content

[WIP] Propose new API for aggregating metrics #54

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/logger/MetricsLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import { IEnvironment } from '../environment/IEnvironment';
import { MetricsContext } from './MetricsContext';
import { Unit } from './Unit';

type Metrics = { [key: string]: number | number[]; };
Copy link

@simonespa simonespa Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type declaration doesn't allow the definition of the "unit" (e.g. count, milliseconds, etc.).

I was hoping to set something like:

 {
    Name: 'ResponseTime',
    Value: 59,
    Unit: Unit.Milliseconds
}

Also, multiple metrics may apply to the same dimensions, shouldn't we define this as an array of metric items as discussed in #46?

Example:

m.putMetricWithDimensions({
   metrics: [
      {
         Name: 'ResponseTime',
         Value: 322,
         Unit: Unit.Milliseconds
      },
      {
         Name: 'RequestCount',
         Value: 1,
         Unit: Unit.Count
      }
   ],
   namespace: '/example/namespace',
   dimensions: [{ PageType: pageType }],
   stripDefaultDimensions: true
});

Copy link
Member Author

@jaredcnance jaredcnance Sep 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agree on unit. The idea here was that the key would be used as the metric name which naturally supports multiple distinct metrics. I don’t recall exactly why I proposed it this way, probably because it was less verbose and easier to read but once you add units to the mix it breaks down. Happy to go with this.

type MetricWithDimensions = {
metrics: Metrics,
namespace?: string | undefined,
dimensions?: Array<Record<string, string>> | undefined,
};

/**
* An async metrics logger.
* Use this interface to publish logs to CloudWatch Logs
Expand Down Expand Up @@ -114,6 +121,10 @@ export class MetricsLogger {
return this;
}

public putMetricWithDimensions(metricWithDimensions: MetricWithDimensions): MetricsLogger {
return this;
}

/**
* Creates a new logger using the same contextual data as
* the previous logger. This allows you to flush the instances
Expand Down
30 changes: 30 additions & 0 deletions src/logger/__tests__/MetricsLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,36 @@ test('context is preserved across flush() calls', async () => {
}
});

test('putMetricWithDimensions example', async () => {
// this metric will use the default namespace and dimensions
logger.putMetricWithDimensions({
metrics: { "MyMetric": 100 }
});

// this metric will use the default dimensions
logger.putMetricWithDimensions({
metrics: { "MyMetric": 100 },
namespace: "My Namespace"
});

// publish multiple metrics along multiple dimensions
const client = 'client';
const pageType = 'pageType';
logger.putMetricWithDimensions({
metrics: {
Metric1: 100,
Metric2: 200,
},
namespace: "My Namespace",
dimensions: [
{ client },
{ pageType },
{ client, pageType },
]
});
});


const expectDimension = (key: string, value: string) => {
expect(sink.events).toHaveLength(1);
const dimensionSets = sink.events[0].getDimensions();
Expand Down