-
Notifications
You must be signed in to change notification settings - Fork 1
/
statsdtoazureblob.ts
106 lines (88 loc) · 3.38 KB
/
statsdtoazureblob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as Azure from "@azure/storage-blob";
import * as os from "os";
import { EventEmitter } from "events";
let containerClient: Azure.ContainerClient;
// these env variables should be defined
// BLOB_ACCOUNT
// BLOB_ACCOUNT_KEY
// REQ_HOSTNAME
// BLOB_CONTAINER_NAME
exports.init = async function (startup_time: string, config: Object, events: EventEmitter) {
if (!startup_time || !config || !events) throw "(startup_time|config|events) undefined";
validateAndSetDefaultEnvVariables();
const account = process.env.BLOB_ACCOUNT as string;
const accountKey = process.env.BLOB_ACCOUNT_KEY as string;
const sharedKeyCredential = new Azure.SharedKeyCredential(account, accountKey);
const blobServiceClient = new Azure.BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
const containerName = process.env.BLOB_CONTAINER_NAME as string;
containerClient = blobServiceClient.getContainerClient(containerName);
if (!await containerExistsAsync(containerName, blobServiceClient)) {
await containerClient.create();
console.log(`Container ${containerName} created`);
}
else {
console.log(`Container ${containerName} exists`);
}
events.on("flush", onFlush);
return true;
};
async function onFlush(timestamp: string, metrics: Object) {
const content = `${timestamp} ${JSON.stringify(metrics)}\n`;
const blobName = `${process.env.REQ_HOSTNAME}-${getDate()}.txt`;
const blobClient = containerClient.getBlobClient(blobName);
const appendBlobClient = blobClient.getAppendBlobClient();
if (!await appendBlobExistsAsync(blobName, containerClient)) {
await appendBlobClient.create();
console.log(`AppendBlob ${blobName} created`);
}
else {
console.log(`AppendBlob ${blobName} exists`);
}
await appendBlobClient.appendBlock(content, content.length);
console.log(`Uploaded block blob ${blobName} successfully`);
}
async function containerExistsAsync(containerName: string, blobServiceClient: Azure.BlobServiceClient): Promise<boolean> {
const containers = blobServiceClient.listContainers({ prefix: containerName });
for await (const container of containers) {
if (container.name === containerName) {
return true;
}
}
return false;
}
async function appendBlobExistsAsync(appendBlobName: string, containerServiceClient: Azure.ContainerClient): Promise<boolean> {
const blobs = containerServiceClient.listBlobsFlat({ prefix: appendBlobName });
for await (const blob of blobs) {
if (blob.name === appendBlobName) {
return true;
}
}
return false;
}
function validateAndSetDefaultEnvVariables() {
if (!process.env.REQ_HOSTNAME) {
console.log("REQ_HOSTNAME is not defined, setting it to local hostname");
process.env.REQ_HOSTNAME = os.hostname();
}
if (!process.env.BLOB_ACCOUNT) {
throw "BLOB_ACCOUNT should be defined";
}
if (!process.env.BLOB_ACCOUNT_KEY) {
throw "BLOB_ACCOUNT_KEY should be defined";
}
if (!process.env.BLOB_CONTAINER_NAME) {
throw "BLOB_CONTAINER_NAME should be defined";
}
}
function pad2(n: number): string {
return (n < 10 ? '0' : '') + n;
}
function getDate(): string {
const d = new Date();
return d.getFullYear() +
pad2(d.getMonth() + 1) +
pad2(d.getDate());
}