Skip to content

Commit 96c5805

Browse files
committed
implement azuretracing metrics
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 3b44e64 commit 96c5805

File tree

7 files changed

+68
-16
lines changed

7 files changed

+68
-16
lines changed

README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Azurer Kubernetes Autopilot
2-
============================
1+
# Azure Kubernetes Autopilot
32

43
[![license](https://img.shields.io/github/license/webdevops/azure-k8s-autopilot.svg)](https://github.com/webdevops/azure-k8s-autopilot/blob/master/LICENSE)
54
[![DockerHub](https://img.shields.io/badge/DockerHub-webdevops%2Fazure--k8s--autopilot-blue)](https://hub.docker.com/r/webdevops/azure-k8s-autopilot/)
65
[![Quay.io](https://img.shields.io/badge/Quay.io-webdevops%2Fazure--k8s--autopilot-blue)](https://quay.io/repository/webdevops/azure-k8s-autopilot)
6+
[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/azure-k8s-autopilot)](https://artifacthub.io/packages/search?repo=azure-k8s-autopilot)
77

88
Kubernetess service for automatic maintenance of an Azure cluster.
99

@@ -16,8 +16,7 @@ Supports [shoutrrr](https://containrrr.github.io/shoutrrr/) notifications.
1616

1717
(Successor of `azure-k8s-autorepair`)
1818

19-
Configuration
20-
-------------
19+
## Configuration
2120

2221
```
2322
Usage:
@@ -134,8 +133,7 @@ for Azure API authentication (using ENV vars) see https://docs.microsoft.com/en-
134133

135134
for Kubernetes ServiceAccont is discoverd automatically (or you can use env path `KUBECONFIG` to specify path to your kubeconfig file)
136135

137-
Metrics
138-
-------
136+
## Metrics
139137

140138
(see `:8080/metrics`)
141139

@@ -146,3 +144,22 @@ Metrics
146144
| `autopilot_repair_duration` | Duration of repair task |
147145
| `autopilot_update_count` | Count of update actions |
148146
| `autopilot_update_duration` | Duration of last exec |
147+
148+
### Azuretracing metrics
149+
150+
(with 22.2.0 and later)
151+
152+
Azuretracing metrics collects latency and latency from azure-sdk-for-go and creates metrics and is controllable using
153+
environment variables (eg. setting buckets, disabling metrics or disable autoreset).
154+
155+
| Metric | Description |
156+
|------------------------------------------|----------------------------------------------------------------------------------------|
157+
| `azurerm_api_ratelimit` | Azure ratelimit metrics (only on /metrics, resets after query due to limited validity) |
158+
| `azurerm_api_request_*` | Azure request count and latency as histogram |
159+
160+
| Environment variable | Example | Description |
161+
|------------------------------------------|----------------------------------|----------------------------------------------------------|
162+
| `METRIC_AZURERM_API_REQUEST_BUCKETS` | `1, 2.5, 5, 10, 30, 60, 90, 120` | Sets buckets for `azurerm_api_request` histogram metric |
163+
| `METRIC_AZURERM_API_REQUEST_DISABLE` | `false` | Disables `azurerm_api_request_*` metric |
164+
| `METRIC_AZURERM_API_RATELIMIT_DISABLE` | `false` | Disables `azurerm_api_ratelimit` metric |
165+
| `METRIC_AZURERM_API_RATELIMIT_AUTORESET` | `false` | Disables `azurerm_api_ratelimit` autoreset after fetch |

autopilot/lib.azure.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func (r *AzureK8sAutopilot) azureVmssInstanceRepair(contextLogger *log.Entry, no
1919
}
2020

2121
vmssClient := compute.NewVirtualMachineScaleSetsClientWithBaseURI(r.azureEnvironment.ResourceManagerEndpoint, nodeInfo.Subscription)
22-
vmssClient.Authorizer = r.azureAuthorizer
22+
r.decorateAzureAutoRest(&vmssClient.BaseClient.Client)
2323

2424
vmssVmClient := compute.NewVirtualMachineScaleSetVMsClientWithBaseURI(r.azureEnvironment.ResourceManagerEndpoint, nodeInfo.Subscription)
25-
vmssVmClient.Authorizer = r.azureAuthorizer
25+
r.decorateAzureAutoRest(&vmssVmClient.BaseClient.Client)
2626

2727
// fetch instances
2828
vmInstance, err := vmssVmClient.Get(r.ctx, nodeInfo.ResourceGroup, nodeInfo.VMScaleSetName, nodeInfo.VMInstanceID, "")
@@ -87,7 +87,7 @@ func (r *AzureK8sAutopilot) azureVmRepair(contextLogger *log.Entry, nodeInfo k8s
8787
var err error
8888

8989
client := compute.NewVirtualMachinesClientWithBaseURI(r.azureEnvironment.ResourceManagerEndpoint, nodeInfo.Subscription)
90-
client.Authorizer = r.azureAuthorizer
90+
r.decorateAzureAutoRest(&client.BaseClient.Client)
9191

9292
// fetch instances
9393
vmInstance, err := client.Get(r.ctx, nodeInfo.ResourceGroup, nodeInfo.VMname, "")
@@ -132,10 +132,10 @@ func (r *AzureK8sAutopilot) azureVmssInstanceUpdate(contextLogger *log.Entry, no
132132
var err error
133133

134134
vmssClient := compute.NewVirtualMachineScaleSetsClientWithBaseURI(r.azureEnvironment.ResourceManagerEndpoint, nodeInfo.Subscription)
135-
vmssClient.Authorizer = r.azureAuthorizer
135+
r.decorateAzureAutoRest(&vmssClient.BaseClient.Client)
136136

137137
vmssVmClient := compute.NewVirtualMachineScaleSetVMsClientWithBaseURI(r.azureEnvironment.ResourceManagerEndpoint, nodeInfo.Subscription)
138-
vmssVmClient.Authorizer = r.azureAuthorizer
138+
r.decorateAzureAutoRest(&vmssVmClient.BaseClient.Client)
139139

140140
// fetch instances
141141
vmInstance, err := vmssVmClient.Get(r.ctx, nodeInfo.ResourceGroup, nodeInfo.VMScaleSetName, nodeInfo.VMInstanceID, "")

autopilot/main.go

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
log "github.com/sirupsen/logrus"
1414
"github.com/webdevopos/azure-k8s-autopilot/config"
1515
"github.com/webdevopos/azure-k8s-autopilot/k8s"
16+
"github.com/webdevops/go-prometheus-common/azuretracing"
1617
"golang.org/x/net/context"
1718
"k8s.io/api/policy/v1beta1"
1819
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -33,6 +34,8 @@ type (
3334
ctx context.Context
3435
Config config.Opts
3536

37+
UserAgent string
38+
3639
cron struct {
3740
repair *cron.Cron
3841
update *cron.Cron
@@ -93,6 +96,7 @@ func (r *AzureK8sAutopilot) Init() {
9396
AzureAuthorizer: r.azureAuthorizer,
9497
AzureEnvironment: r.azureEnvironment,
9598
Client: r.k8sClient,
99+
UserAgent: r.UserAgent,
96100
}
97101

98102
r.Config.Repair.ProvisioningStateAll = false
@@ -451,3 +455,12 @@ func (r *AzureK8sAutopilot) autoUncordonExpiredNodes(contextLogger *log.Entry, n
451455
}
452456
}
453457
}
458+
459+
func (r *AzureK8sAutopilot) decorateAzureAutoRest(client *autorest.Client) {
460+
client.Authorizer = r.azureAuthorizer
461+
if err := client.AddToUserAgent(r.UserAgent); err != nil {
462+
log.Panic(err)
463+
}
464+
465+
azuretracing.DecoreAzureAutoRest(client)
466+
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/prometheus/client_golang v1.12.1
1414
github.com/robfig/cron/v3 v3.0.1
1515
github.com/sirupsen/logrus v1.8.1
16+
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1
1617
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
1718
k8s.io/api v0.23.0
1819
k8s.io/apimachinery v0.23.0
@@ -61,7 +62,7 @@ require (
6162
github.com/spf13/pflag v1.0.5 // indirect
6263
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 // indirect
6364
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
64-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
65+
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
6566
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
6667
golang.org/x/text v0.3.7 // indirect
6768
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect

go.sum

+5-1
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
600600
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
601601
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
602602
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
603+
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1 h1:wIf6O43jGEarp8ojgInXRIt6jHeQ94JUvsy4O3wqKHY=
604+
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1/go.mod h1:naEkgDRh6L+Ef0qzaS9Q60m1GkwN5Mu6xaNd/Ha84Sg=
603605
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
604606
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
605607
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -865,8 +867,10 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
865867
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
866868
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
867869
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
868-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
869870
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
871+
golang.org/x/sys v0.0.0-20220207234003-57398862261d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
872+
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
873+
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
870874
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
871875
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
872876
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

k8s/nodelist.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/Azure/go-autorest/autorest/azure"
99
"github.com/patrickmn/go-cache"
1010
log "github.com/sirupsen/logrus"
11+
"github.com/webdevops/go-prometheus-common/azuretracing"
1112
corev1 "k8s.io/api/core/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/apimachinery/pkg/watch"
@@ -26,6 +27,8 @@ type (
2627
AzureAuthorizer autorest.Authorizer
2728
AzureEnvironment azure.Environment
2829

30+
UserAgent string
31+
2932
nodeWatcher watch.Interface
3033
azureCache *cache.Cache
3134
ctx context.Context
@@ -188,7 +191,7 @@ func (n *NodeList) refreshAzureVmssCache() error {
188191
})
189192

190193
vmssVmClient := compute.NewVirtualMachineScaleSetVMsClientWithBaseURI(n.AzureEnvironment.ResourceManagerEndpoint, vmssInfo.Subscription)
191-
vmssVmClient.Authorizer = n.AzureAuthorizer
194+
n.decorateAzureAutoRest(&vmssVmClient.BaseClient.Client)
192195

193196
vmssInstanceList, err := vmssVmClient.List(n.ctx, vmssInfo.ResourceGroup, vmssInfo.VMScaleSetName, "", "", "")
194197
if err != nil {
@@ -249,3 +252,12 @@ func (n *NodeList) GetAzureVmssList() (vmssList map[string]*NodeInfo, err error)
249252

250253
return
251254
}
255+
256+
func (n *NodeList) decorateAzureAutoRest(client *autorest.Client) {
257+
client.Authorizer = n.AzureAuthorizer
258+
if err := client.AddToUserAgent(n.UserAgent); err != nil {
259+
log.Panic(err)
260+
}
261+
262+
azuretracing.DecoreAzureAutoRest(client)
263+
}

main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
log "github.com/sirupsen/logrus"
88
"github.com/webdevopos/azure-k8s-autopilot/autopilot"
99
"github.com/webdevopos/azure-k8s-autopilot/config"
10+
"github.com/webdevops/go-prometheus-common/azuretracing"
1011
"net/http"
1112
"os"
1213
"os/signal"
@@ -18,6 +19,8 @@ import (
1819

1920
const (
2021
Author = "webdevops.io"
22+
23+
UserAgent = "azure-k8s-autopilot/"
2124
)
2225

2326
var (
@@ -37,7 +40,8 @@ func main() {
3740
log.Info(string(opts.GetJson()))
3841

3942
pilot := autopilot.AzureK8sAutopilot{
40-
Config: opts,
43+
Config: opts,
44+
UserAgent: UserAgent + gitTag,
4145
}
4246
pilot.Init()
4347
pilot.Start()
@@ -115,7 +119,8 @@ func startHttpServer() {
115119
}
116120
})
117121

118-
http.Handle("/metrics", promhttp.Handler())
122+
http.Handle("/metrics", azuretracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
123+
119124
go func() {
120125
log.Fatal(http.ListenAndServe(opts.ServerBind, nil))
121126
}()

0 commit comments

Comments
 (0)