awslabs / awslabs/aws-embedded-metrics-node

Programmatically Environment override doesn't work

Open
#43 4 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
TypeScript
Stars
263
Forks
39
PR merge metrics
No merged PRs in 30d

Description

## Summary
According to https://github.com/awslabs/aws-embedded-metrics-node/pull/19 you can now override the environment detection by either using

```
const { Configuration } = require("aws-embedded-metrics");
Configuration.environmentOverride = "Local";
```

or

```
export AWS_EMF_ENVIRONMENT=Local
```

When I export the environment variable it works and I can see the agent logging to stdout, when I try the first approach, I still get the following error:

```
(node:783) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 0.0.0.0:25888
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:783) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 13)
(node:783) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```

## Details
I've got the following helper module
```
import { metricScope, Unit, Configuration } from 'aws-embedded-metrics';
import config from 'config';

const environment = config.get('environment');
const namespace = config.get('namespace');

Configuration.logGroupName = namespace;
Configuration.environmentOverride = 'Local';

export const pageRequestLogger = metrics => {
return async pageRequestEvent => {
const {
requestCount,
errorCount,
responseTime,
pageType,
event,
processPid,
request,
response,
log,
logTrace
} = pageRequestEvent;
metrics.setNamespace(namespace);
metrics.putMetric('RequestCount', requestCount, Unit.Count);
metrics.putMetric('ErrorCount', errorCount, Unit.Count);
metrics.putMetric('ResponseTime', responseTime, Unit.Milliseconds);
metrics.setDimensions({ PageType: pageType });

metrics.setProperty('event', event);
metrics.setProperty('processPid', processPid);
metrics.setProperty('request', request);
metrics.setProperty('response', response);
metrics.setProperty('log', log);
metrics.setProperty('logTrace', logTrace);
};
};

export const logPageRequest = metricScope(pageRequestLogger);
```

## Environment
The code runs on a Centos7 Docker container
Node: 12.18.0
NPM: 6.14.4

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the helper-module example in the CentOS 7, Node 12.18.0 environment, comparing Configuration.environmentOverride with AWS_EMF_ENVIRONMENT=Local. Trace the environment-detection and agent-connection path used by metricScope. Done means the programmatic override selects local stdout logging and no longer attempts to connect to 0.0.0.0:25888.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend, observability-sre
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.