Skip to content

Commit

Permalink
Merge pull request #1186 from dm3-org/metricsDocsAndDisabling
Browse files Browse the repository at this point in the history
Metrics docs and disabling
  • Loading branch information
AlexNi245 authored Sep 24, 2024
2 parents 6a93114 + 83eaa47 commit fa38ea3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/delivery-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ npm
```
npm start
```

## Configuration

### Metrics collection

This delivery service implementation collects these metrics:

- number of messages received
- number of notifications sent
- total size of messages received

These metrics are accumulated over the `metricsCollectionIntervalInSeconds`, which can be defined in the config file and defaults to 1 hour. They are retained for `metricsRetentionDurationInSeconds`, which defaults to 10 days.

The metrics are not sent anywhere, but can be accessed by anyone via the `/metrics` endpoint. This endpoint censors the current collection interval to prevent real-time tracking, which would reduce the privacy of the users.

In order to disable metrics collection, set `metricsRetentionDurationInSeconds` to 0.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ describe('setMetrics', () => {
mockDeliveryServiceProperties.metricsRetentionDurationInSeconds,
);
});

it('should not collect metrics when retention interval is 0', async () => {
const messageSizeBytes = 100;
const countMessageFunction = countMessage(mockRedis);

const propertiesWithZeroRetention = {
...mockDeliveryServiceProperties,
metricsRetentionDurationInSeconds: 0,
};

await countMessageFunction(
messageSizeBytes,
propertiesWithZeroRetention,
);

expect(mockRedis.incrBy).not.toHaveBeenCalled();
expect(mockRedis.expire).not.toHaveBeenCalled();
});
});

describe('countNotification', () => {
Expand All @@ -76,5 +94,19 @@ describe('setMetrics', () => {
mockDeliveryServiceProperties.metricsRetentionDurationInSeconds,
);
});

it('should not collect metrics when retention interval is 0', async () => {
const countNotificationFunction = countNotification(mockRedis);

const propertiesWithZeroRetention = {
...mockDeliveryServiceProperties,
metricsRetentionDurationInSeconds: 0,
};

await countNotificationFunction(propertiesWithZeroRetention);

expect(mockRedis.incrBy).not.toHaveBeenCalled();
expect(mockRedis.expire).not.toHaveBeenCalled();
});
});
});
10 changes: 10 additions & 0 deletions packages/delivery-service/src/persistence/metrics/setMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function countMessage(redis: Redis) {
messageSizeBytes: number,
deliveryServiceProperties: DeliveryServiceProperties,
) => {
if (deliveryServiceProperties.metricsRetentionDurationInSeconds <= 0) {
// Metrics are disabled
return;
}

const timestamp = getCurrentIntervalTimestamp(
deliveryServiceProperties.metricsCollectionIntervalInSeconds,
);
Expand Down Expand Up @@ -43,6 +48,11 @@ export function countMessage(redis: Redis) {

export function countNotification(redis: Redis) {
return async (deliveryServiceProperties: DeliveryServiceProperties) => {
if (deliveryServiceProperties.metricsRetentionDurationInSeconds <= 0) {
// Metrics are disabled
return;
}

const timestamp = getCurrentIntervalTimestamp(
deliveryServiceProperties.metricsCollectionIntervalInSeconds,
);
Expand Down

0 comments on commit fa38ea3

Please sign in to comment.