Skip to content
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

Fixes with getting config from secrets manager and types #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AWS from "aws-sdk";
import { AwsReporter } from "./aws-reporter";
import { DataDogReporter } from "./datadog-reporter";
import { Metric, MetricReporter, ReporterConfigs } from "./types";
Expand All @@ -18,8 +19,18 @@ export class DynamicMetricReporter implements MetricReporter {

if (reporters != null && reporters instanceof Promise) {
this.setupPromise = reporters as unknown as Promise<MetricReporter[] | ReporterConfigs>;
} else {
} else if (reporters != null) {
this.setupPromise = Promise.resolve(reporters);
} else {
this.setupPromise = (async () => {
const secret = await new AWS.SecretsManager({ region: process.env.AWS_REGION })
.getSecretValue({ SecretId: 'GlobalRSFMetricConfigs' })
.promise();
return JSON.parse(secret.SecretString);
})().catch(e => {
console.log("Error getting default secret 'GlobalRSFMetricConfigs'", e);
return null;
});
}

this.setupPromise.then((reporters: MetricReporter[] | ReporterConfigs) => {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rstreams-metrics",
"version": "1.0.5",
"version": "1.0.9-alpha",
"description": "",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -12,7 +12,9 @@
"watch": "tsc -w",
"webpack": "webpack",
"compile": "tsc",
"lint": "eslint ."
"lint": "eslint .",
"prepublish": "cp package.json dist/package.json && cd dist",
"postpublish": "cd .."
},
"author": "",
"license": "ISC",
Expand Down
32 changes: 32 additions & 0 deletions test/index.utest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chai, { assert, expect } from "chai";
import sinon from "sinon";
import sinonchai from "sinon-chai";
import { Metric } from "types";
import AWS from "aws-sdk";
chai.use(sinonchai);


Expand Down Expand Up @@ -35,6 +36,37 @@ describe("index", () => {


it("default", async () => {
const getSecretValue = sandbox.stub()
.onFirstCall().returns(AWSRequest(new Error("Invalid Region")));

sandbox.stub(AWS, "SecretsManager").returns({ getSecretValue });
const reporter = new DynamicMetricReporter();
await reporter.start();
reporter.log({
id: "metric-id",
value: 1
});
await reporter.end();
});

function AWSRequest(response: AWS.SecretsManager.GetSecretValueResponse | Error) {
return {
promise: async () => {
if (response instanceof Error) {
throw response;
}
return response;
}
};
}

it("default with secret", async () => {
process.env.AWS_REGION = "us-east-1";

const getSecretValue = sandbox.stub()
.onFirstCall().returns(AWSRequest({ SecretString: JSON.stringify({}) }));

sandbox.stub(AWS, "SecretsManager").returns({ getSecretValue });
const reporter = new DynamicMetricReporter();
await reporter.start();
reporter.log({
Expand Down
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AWSConfig } from "aws-reporter";
import { AWSConfig } from "./aws-reporter";
import { DataDogConfig } from "./datadog-reporter";

export interface Metric {
Expand Down