-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAzureMetrics.fs
More file actions
84 lines (68 loc) · 2.73 KB
/
AzureMetrics.fs
File metadata and controls
84 lines (68 loc) · 2.73 KB
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
module Microsoft.FSharpLu.Azure.Metrics
open System
open System.Net.Http
open Microsoft.FSharpLu.Async
open Newtonsoft
/// Azure Metrics REST Api returns json of the following format:
/// {value: [{currentValue:float, id:string, limit: float, unit: string, name: {localizedValue: string, value: string}}] }
/// MetricName, Metric and MetricValues records map to the json returned by Azure Metrics API.
/// Requred for desrialization of the Json string.
/// Metric name returned by Azure Metrics REST API call
type MetricName =
{
localizedValue: string
value: string
}
/// Metric value returned as part of Azure Metrics REST API call
type Metric =
{
currentValue: float
id: string
limit: float
name: MetricName
unit: string
}
/// List of values returned as part of Azure Metrics REST API call
type MetricValues =
{
value: Metric list
}
/// Subscription
type Subscription = string
/// Region
type Region = string
/// Create an HTTP client to the Azure metric API
let newHttpClient (auth:Auth.ADAuthenticationMethod) =
async {
let! authToken = Microsoft.FSharpLu.Azure.Auth.getAuthorizationToken auth
let c = new HttpClient(BaseAddress = Uri(Microsoft.FSharpLu.Azure.Constants.management))
c.DefaultRequestHeaders.Add("Authorization", "Bearer " + authToken)
return c
}
/// Azure Metrics retrieval class. E.g.: cores used vs cores available, network infrastructure used vs available
type AzureMetrics(httpClient:HttpClient) =
let getMetrics (url: string) =
async {
let! metrics = httpClient.GetStringAsync(url).AsAsync
return metrics
}
let makeUrl (resourceType : string) (subscription: Subscription) (region: Region) =
sprintf "/subscriptions/%s/providers/%s/locations/%s/usages?api-version=%s" subscription resourceType region Constants.azureMetricsApiVersion
let getNetworkUsage(subscription: Subscription)(region: Region) =
makeUrl "Microsoft.Network" subscription region
|> getMetrics
let getComputeUsage(subscription: Subscription)(region: Region) =
makeUrl "Microsoft.Compute" subscription region
|> getMetrics
member this.GetNetworkUsage(subscription: Subscription)(region: Region) =
async{
let! json = getNetworkUsage subscription region
let vs = Json.JsonConvert.DeserializeObject<MetricValues>(json)
return vs.value
}
member this.GetComputeUsage(subscription: Subscription)(region: Region) =
async{
let! json = getComputeUsage subscription region
let vs = Json.JsonConvert.DeserializeObject<MetricValues>(json)
return vs.value
}