Skip to content

Commit cfea3e8

Browse files
committed
implement azuretracing metrics
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent abedbac commit cfea3e8

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ available on `/metrics`
9494
| `azure_loganalytics_query_results` | Number of results from query |
9595
| `azure_loganalytics_query_requests` | Count of requests (eg paged subqueries) per query |
9696

97+
### Azuretracing metrics
98+
99+
(with 22.2.0 and later)
100+
101+
Azuretracing metrics collects latency and latency from azure-sdk-for-go and creates metrics and is controllable using
102+
environment variables (eg. setting buckets, disabling metrics or disable autoreset).
103+
104+
| Metric | Description |
105+
|------------------------------------------|----------------------------------------------------------------------------------------|
106+
| `azurerm_api_ratelimit` | Azure ratelimit metrics (only on /metrics, resets after query due to limited validity) |
107+
| `azurerm_api_request_*` | Azure request count and latency as histogram |
108+
109+
| Environment variable | Example | Description |
110+
|------------------------------------------|----------------------------------|----------------------------------------------------------|
111+
| `METRIC_AZURERM_API_REQUEST_BUCKETS` | `1, 2.5, 5, 10, 30, 60, 90, 120` | Sets buckets for `azurerm_api_request` histogram metric |
112+
| `METRIC_AZURERM_API_REQUEST_DISABLE` | `false` | Disables `azurerm_api_request_*` metric |
113+
| `METRIC_AZURERM_API_RATELIMIT_DISABLE` | `false` | Disables `azurerm_api_ratelimit` metric |
114+
| `METRIC_AZURERM_API_RATELIMIT_AUTORESET` | `false` | Disables `azurerm_api_ratelimit` autoreset after fetch |
97115

98116
## Examples
99117

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/remeh/sizedwaitgroup v1.0.0
1414
github.com/sirupsen/logrus v1.8.1
1515
github.com/webdevops/azure-resourcegraph-exporter v0.0.0-20220211155142-96a70b1d912f
16+
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1
1617
)
1718

1819
require (

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
231231
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
232232
github.com/webdevops/azure-resourcegraph-exporter v0.0.0-20220211155142-96a70b1d912f h1:Hbtrj8gZOSPzywrok7vxakGiM9cloDiwZApEjyNQ9Js=
233233
github.com/webdevops/azure-resourcegraph-exporter v0.0.0-20220211155142-96a70b1d912f/go.mod h1:7/oZePL6o+quZtWhtM97XjTh2JCEAYSPkpkx9wmNz2s=
234+
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1 h1:wIf6O43jGEarp8ojgInXRIt6jHeQ94JUvsy4O3wqKHY=
234235
github.com/webdevops/go-prometheus-common v0.0.0-20220211101814-4c9913788fe1/go.mod h1:naEkgDRh6L+Ef0qzaS9Q60m1GkwN5Mu6xaNd/Ha84Sg=
235236
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
236237
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

loganalytics/prober.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
log "github.com/sirupsen/logrus"
1616
"github.com/webdevops/azure-loganalytics-exporter/config"
1717
"github.com/webdevops/azure-resourcegraph-exporter/kusto"
18+
"github.com/webdevops/go-prometheus-common/azuretracing"
1819
"net/http"
1920
"strconv"
2021
"time"
@@ -28,6 +29,7 @@ type (
2829
LogAnalyticsProber struct {
2930
QueryConfig kusto.Config
3031
Conf config.Opts
32+
UserAgent string
3133

3234
Azure struct {
3335
Environment azure.Environment
@@ -133,8 +135,8 @@ func (p *LogAnalyticsProber) AddWorkspaces(workspace ...string) {
133135
func (p *LogAnalyticsProber) LogAnalyticsQueryClient() operationalinsights.QueryClient {
134136
// Create and authorize a operationalinsights client
135137
client := operationalinsights.NewQueryClientWithBaseURI(p.Azure.Environment.ResourceIdentifiers.OperationalInsights + OperationInsightsWorkspaceUrlSuffix)
138+
p.decorateAzureAutoRest(&client.Client)
136139
client.Authorizer = p.Azure.OpInsightsAuthorizer
137-
client.ResponseInspector = p.respondDecorator(nil)
138140
return client
139141
}
140142

@@ -346,14 +348,6 @@ func (p *LogAnalyticsProber) sendQueryToWorkspace(logger *log.Entry, workspaceId
346348
}
347349
}
348350

349-
func (p *LogAnalyticsProber) respondDecorator(subscriptionId *string) autorest.RespondDecorator {
350-
return func(p autorest.Responder) autorest.Responder {
351-
return autorest.ResponderFunc(func(r *http.Response) error {
352-
return nil
353-
})
354-
}
355-
}
356-
357351
func (p *LogAnalyticsProber) parseCacheTime(r *http.Request) (time.Duration, error) {
358352
durationString := r.URL.Query().Get("cache")
359353
if durationString != "" {
@@ -379,3 +373,11 @@ func (p *LogAnalyticsProber) NewSizedWaitGroup() sizedwaitgroup.SizedWaitGroup {
379373

380374
return sizedwaitgroup.New(size)
381375
}
376+
377+
func (p *LogAnalyticsProber) decorateAzureAutoRest(client *autorest.Client) {
378+
client.Authorizer = p.Azure.AzureAuthorizer
379+
if err := client.AddToUserAgent(p.UserAgent); err != nil {
380+
log.Panic(err)
381+
}
382+
azuretracing.DecoreAzureAutoRest(client)
383+
}

loganalytics/servicediscovery.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ func (sd *LogAnalyticsServiceDiscovery) ResourcesClient(subscriptionId string) *
2121
prober := sd.prober
2222

2323
client := operationalinsightsProfile.NewWorkspacesClientWithBaseURI(prober.Azure.Environment.ResourceManagerEndpoint, subscriptionId)
24-
client.Authorizer = prober.Azure.AzureAuthorizer
25-
client.ResponseInspector = prober.respondDecorator(&subscriptionId)
24+
prober.decorateAzureAutoRest(&client.Client)
2625

2726
return &client
2827
}

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/webdevops/azure-loganalytics-exporter/config"
1313
"github.com/webdevops/azure-loganalytics-exporter/loganalytics"
1414
"github.com/webdevops/azure-resourcegraph-exporter/kusto"
15+
"github.com/webdevops/go-prometheus-common/azuretracing"
1516
"net/http"
1617
"os"
1718
"path"
@@ -22,6 +23,8 @@ import (
2223

2324
const (
2425
Author = "webdevops.io"
26+
27+
UserAgent = "azure-loganalytics-exporter/"
2528
)
2629

2730
var (
@@ -139,7 +142,14 @@ func initAzureConnection() {
139142

140143
// start and handle prometheus handler
141144
func startHttpServer() {
142-
http.Handle("/metrics", promhttp.Handler())
145+
// healthz
146+
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
147+
if _, err := fmt.Fprint(w, "Ok"); err != nil {
148+
log.Error(err)
149+
}
150+
})
151+
152+
http.Handle("/metrics", azuretracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
143153

144154
http.HandleFunc("/probe", handleProbeRequest)
145155
http.HandleFunc("/probe/workspace", handleProbeWorkspace)

probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func NewLogAnalyticsProber(w http.ResponseWriter, r *http.Request) *loganalytics
7575
prober := loganalytics.NewLogAnalyticsProber(w, r)
7676
prober.QueryConfig = Config
7777
prober.Conf = opts
78+
prober.UserAgent = UserAgent + gitTag
7879
prober.Azure.AzureAuthorizer = AzureAuthorizer
7980
prober.Azure.OpInsightsAuthorizer = OpInsightsAuthorizer
8081
prober.Azure.Environment = AzureEnvironment

0 commit comments

Comments
 (0)