-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a new metric "s3_latest_file_size_test" (#9)
* 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
1 parent
553b85a
commit 6e8f28a
Showing
8 changed files
with
914 additions
and
799 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.