This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
110 lines (99 loc) · 3.58 KB
/
index.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
107
108
109
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure";
import * as eventhub from "@pulumi/azure/eventhub";
import * as crypto from "crypto";
import Axios, * as axios from "axios";
// Create an Azure Resource Group
const resourceGroup = new azure.core.ResourceGroup("pulumi", { location: azure.Locations.EastUS });
const testAnalyticsWorkspace = new azure.operationalinsights.AnalyticsWorkspace("test", {
name: pulumi.interpolate`iotEdgeMetricsLa-${resourceGroup.name}`,
resourceGroupName: resourceGroup.name,
retentionInDays: 30,
sku: "PerGB2018",
});
// Create an Event Hub endpoint to route metrics messages
const ehNamespace = new eventhub.EventHubNamespace("test", {
resourceGroupName: resourceGroup.name,
sku: "standard",
});
const metricsEventHub = new eventhub.EventHub("test", {
resourceGroupName: resourceGroup.name,
namespaceName: ehNamespace.name,
partitionCount: 4,
messageRetention: 7,
});
const testIoTHub = new azure.iot.IoTHub("test", {
endpoints: [{
connectionString:
pulumi.interpolate`${ehNamespace.defaultPrimaryConnectionString};EntityPath=${metricsEventHub.name}`,
name: "exportMetrics",
type: "AzureIotHub.EventHub",
}],
fallbackRoute: {
source: "DeviceMessages",
enabled: true,
endpointNames: ["events"],
condition: "true",
},
location: resourceGroup.location,
name: pulumi.interpolate`piothub-${resourceGroup.name}`,
resourceGroupName: resourceGroup.name,
routes: [{
condition: "MessageIdentifier = 'IoTEdgeMetrics'",
enabled: true,
endpointNames: ["exportMetrics"],
name: "metricsRoute",
source: "DeviceMessages",
}],
sku: {
capacity: 1,
name: "S1",
tier: "Standard",
}
});
const standardPlan = new azure.appservice.Plan("my-appservice", {
resourceGroupName: resourceGroup.name,
sku: {
tier: "Standard",
size: "S1",
},
maximumElasticWorkerCount: 20,
});
metricsEventHub.onEvent("metricsEvent", {
plan: standardPlan,
cardinality: "one",
appSettings: {
WID: testAnalyticsWorkspace.workspaceId,
WKEY: testAnalyticsWorkspace.primarySharedKey
},
callback: async (context, msg) => {
const body = JSON.stringify(msg);
const wId = process.env.WID
const wKey = process.env.WKEY
const apiVersion = '2016-04-01';
let processingDate = new Date().toUTCString();
let contentLength = Buffer.byteLength(body, 'utf8');
let stringToSign = 'POST\n' + contentLength + '\napplication/json\nx-ms-date:' + processingDate + '\n/api/logs';
let signature = crypto.createHmac('sha256', new Buffer(String(wKey), 'base64')).update(stringToSign, 'utf8').digest('base64');
let authorization = 'SharedKey ' + wId + ':' + signature;
var headers = {
"content-type": "application/json",
"Authorization": authorization,
"Log-Type": "promMetrics",
"x-ms-date": processingDate
};
const args = {
headers: headers,
body: body
};
const url = 'https://' + wId + '.ods.opinsights.azure.com/api/logs?api-version=' + apiVersion;
try {
const response = await Axios.post(url, body, { headers: headers })
console.log(`statusCode: ${response.status}`);
console.log(response);
} catch (error) {
// If the promise rejects, an error will be thrown and caught here
console.log(error);
}
}
});