-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
64 lines (49 loc) · 1.2 KB
/
metrics.go
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
package azure
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"net/http"
"strconv"
"time"
"github.com/Azure/go-autorest/tracing"
"github.com/Azure/ARO-RP/pkg/metrics"
)
var _ tracing.Tracer = (*tracer)(nil)
type tracer struct {
m metrics.Interface
}
type contextKey int
const (
contextKeyMetric contextKey = iota
)
type metric struct {
t time.Time
name string
}
func New(m metrics.Interface) tracing.Tracer {
return &tracer{
m: m,
}
}
func (t *tracer) NewTransport(base *http.Transport) http.RoundTripper {
return base
}
func (t *tracer) StartSpan(ctx context.Context, name string) context.Context {
start := time.Now()
return context.WithValue(ctx, contextKeyMetric, metric{
name: name,
t: start,
})
}
func (t *tracer) EndSpan(ctx context.Context, httpStatusCode int, err error) {
metric := ctx.Value(contextKeyMetric).(metric)
t.m.EmitFloat("client.azure.duration", time.Now().Sub(metric.t).Seconds(), map[string]string{
"client": metric.name,
"code": strconv.Itoa(httpStatusCode),
})
t.m.EmitGauge("client.azure.count", 1, map[string]string{
"client": metric.name,
"code": strconv.Itoa(httpStatusCode),
})
}