Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
Allow passing a full json file (#164)
Browse files Browse the repository at this point in the history
* Allow passing a full json file

Signed-off-by: Emerson Knapp <eknapp@amazon.com>
  • Loading branch information
emersonknapp authored Aug 10, 2020
1 parent 73198be commit a0c8a62
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 116 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/generate-metrics-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euxo pipefail

now=$(date +%s)

cat << EOF > metrics.json
[
{
"MetricName": "test-bytes-metric",
"Timestamp": ${now},
"Unit": "Bytes",
"Value": 20
},
{
"MetricName": "test-seconds-metric",
"Timestamp": ${now},
"Unit": "Seconds",
"Value": 11
}
]
EOF
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ jobs:
node-version: '12.x'
- run: .github/workflows/build-and-test.sh

test_metrics_file_input:
runs-on: ubuntu-latest
container:
image: ubuntu:bionic
if: ${{ ! github.event.repository.fork && ! github.event.pull_request.head.repo.fork }}
steps:
# Run with a valid metrics JSON file just to make sure that it doesn't crash when doing so
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- run: .github/workflows/generate-metrics-file.sh
- uses: ./
with:
namespace: ActionCloudWatchMetricsTests
metric-data: metrics.json

log_workflow_status_to_cloudwatch:
runs-on: ubuntu-latest
container:
Expand Down
2 changes: 2 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe("unit test suite", () => {
return "1.0";
case "metric-dimensions":
return "[]";
case "metric-data":
return "";
}
throw new Error("unknown input");
});
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ inputs:
{ "Name": "github.ref", "Value": "${{ github.ref }}" },
{ "Name": "github.repository", "Value": "${{ github.repository }}" }
]
metric-data:
description: >-
Path to a JSON file containing CloudWatch-compatible metrics, according to
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudwatch/put-metric-data.html
If this input is specified, the action will ignore the single-value inputs "metric-name", "metric-value", "metric-dimensions" and only publish the data from this file.
required: false

namespace:
description: 'The namespace for the metric data.'
Expand Down
287 changes: 190 additions & 97 deletions dist/index.js

Large diffs are not rendered by default.

48 changes: 29 additions & 19 deletions src/cw-build-status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from "@actions/core";
import * as fs from "fs";
import CloudWatch from "aws-sdk/clients/cloudwatch";

/**
Expand All @@ -9,26 +10,35 @@ export async function run() {
const namespace = core.getInput("namespace", { required: true });
const metricName = core.getInput("metric-name", { required: true });
const metricValue = core.getInput("metric-value", { required: true });
let metricValueAsFloat = 0.0;
switch (metricValue) {
case "true":
metricValueAsFloat = 1.0;
break;
case "false":
metricValueAsFloat = 0.0;
break;
default:
metricValueAsFloat = Number(metricValue);
const metricDataPath = core.getInput("metric-data", { required: false });

let metricData = [];
if (metricDataPath) {
const contents = fs.readFileSync(metricDataPath, 'utf8');
metricData = JSON.parse(contents);
} else {
let metricValueAsFloat = 0.0;
switch (metricValue) {
case "true":
metricValueAsFloat = 1.0;
break;
case "false":
metricValueAsFloat = 0.0;
break;
default:
metricValueAsFloat = Number(metricValue);
}
const metricDimensions = core.getInput("metric-dimensions", {
required: true,
});
const metricDatum = {
MetricName: metricName,
Value: metricValueAsFloat,
Dimensions: JSON.parse(metricDimensions),
};
metricData = [metricDatum];
}
const metricDimensions = core.getInput("metric-dimensions", {
required: true,
});
const metricDatum = {
MetricName: metricName,
Value: metricValueAsFloat,
Dimensions: JSON.parse(metricDimensions),
};
const metricData = [metricDatum];

core.info(
`Publishing metrics ${JSON.stringify(
metricData,
Expand Down

0 comments on commit a0c8a62

Please sign in to comment.