Skip to content

Commit

Permalink
feat: add a new metric "s3_latest_file_size_test" (#9)
Browse files Browse the repository at this point in the history
* chore: upgrade deps and prepare 0.2.0

* feat: add new metric latest file size

* feat: fix existing code

* test: add about metric name

* chore: purge yarn lock to clean build

* chore: add the changelog
  • Loading branch information
Tchoupinax authored Aug 19, 2023
1 parent 553b85a commit 6e8f28a
Show file tree
Hide file tree
Showing 8 changed files with 914 additions and 799 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 0.2.0 | 2023-08-19

#### Features

- Add a new metric `metric-latest-file-size`
- Fix some metric labels

#### Chores

- Upgrade dependencies

## 0.1.3 | 2023-07-29

#### Features
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s3-prometheus-exporter",
"version": "0.1.3",
"version": "0.2.0",
"description": "S3 data exporter for prometheus",
"author": "Tchoupinax <[email protected]> (https://corentinfiloche.xyz)",
"license": "MIT",
Expand All @@ -14,14 +14,14 @@
},
"scripts": {
"build": "tsc",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint": "eslint src --fix",
"s3": "docker compose up -d",
"security": "trivy fs --security-checks vuln,config .",
"test": "vitest",
"watch": "nodemon --exec vite-node --options.deps.inline=\"@aws-sdk\" src/index.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "3.386.0",
"@aws-sdk/client-s3": "3.395.0",
"config": "3.3.9",
"express": "4.18.2",
"fastify": "4.21.0",
Expand All @@ -30,13 +30,13 @@
"pino-pretty": "10.2.0",
"pretty-bytes": "6.1.1",
"prom-client": "14.2.0",
"vite-node": "0.34.1",
"vitest": "0.34.1"
"vite-node": "0.34.2",
"vitest": "0.34.2"
},
"devDependencies": {
"@types/config": "3.3.0",
"@types/express": "4.17.17",
"@types/node": "20.4.8",
"@types/node": "20.5.1",
"esbuild-plugin-tsc": "0.4.0",
"eslint-config-tchoupinax": "1.0.3",
"nodemon": "3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/metric-biggest-file-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class extends Metric {
declarePrometheusMesure (register: Registry): Gauge<any> {
return new Gauge({
name: this.name(),
help: "Last modified timestamp(milliseconds) for latest file in",
help: "Biggest file size",
labelNames: ["name"],
registers: [register],
});
Expand Down
40 changes: 40 additions & 0 deletions src/metrics/metric-latest-file-size.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { _Object } from "@aws-sdk/client-s3";
import { beforeEach, describe, expect, it } from "vitest";

import MetricLatestFileSize from "./metric-latest-file-size";

describe("metric latest file size", () => {
let metricLatestFileSize: MetricLatestFileSize;

beforeEach(() => {
metricLatestFileSize = new MetricLatestFileSize("test");
});

it("should return the name of the metric", () => {
expect(metricLatestFileSize.name()).toBe("s3_latest_file_size_test");
});

it("should return the size of the latest file (sorted by timestamp)", () => {
const data: Array<_Object> = [
{ Size: 23, LastModified: new Date("December 17, 1992") },
{ Size: 1234, LastModified: new Date("December 17, 1998") },
{ Size: 2345, LastModified: new Date("December 17, 1995") },
];

const answer = metricLatestFileSize.process(data);

expect(answer).toBe(1234);
});

it("should return the size of the latest file (sorted by timestamp)", () => {
const data: Array<_Object> = [
{ Size: 23, LastModified: new Date("December 17, 1992") },
{ Size: 1234, LastModified: new Date("December 17, 1988") },
{ Size: 2345, LastModified: new Date("December 17, 1995") },
];

const answer = metricLatestFileSize.process(data);

expect(answer).toBe(2345);
});
});
34 changes: 34 additions & 0 deletions src/metrics/metric-latest-file-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { _Object } from "@aws-sdk/client-s3";
import { Gauge, Registry } from "prom-client";

import Metric from "./metric";

export default class extends Metric {
constructor (prefix: string) {
super("latest_file_size", prefix);
}

declarePrometheusMesure (register: Registry): Gauge<any> {
return new Gauge({
name: this.name(),
help: "Most recent file size",
labelNames: ["name"],
registers: [register],
});
}

process (files: Array<_Object>): number {
let mostRecentFile: _Object = files[0];

for (const file of files) {
if (
file?.LastModified && mostRecentFile?.LastModified &&
file?.LastModified > mostRecentFile?.LastModified
) {
mostRecentFile = file;
}
}

return mostRecentFile?.Size ?? -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Metric from "./metric";

export default class extends Metric {
constructor (prefix: string) {
super("smallest_file_timestamp", prefix);
super("smallest_file_size", prefix);
}

declarePrometheusMesure (register: Registry): Gauge<any> {
return new Gauge({
name: this.name(),
help: "Last modified timestamp(milliseconds) for latest file in",
help: "Smallest file size",
labelNames: ["name"],
registers: [register],
});
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/metric.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ describe("metric", () => {
});

it("should process the name correctly", () => {
expect(metric.name()).toBe("s3_smallest_file_timestamp_test-01");
expect(metric.name()).toBe("s3_smallest_file_timestamp_test_01");
});
});
Loading

0 comments on commit 6e8f28a

Please sign in to comment.