@@ -17,10 +17,12 @@ limitations under the License.
17
17
package api
18
18
19
19
import (
20
+ "context"
20
21
"crypto/tls"
21
22
"crypto/x509"
22
23
"fmt"
23
24
"net/http"
25
+ "strconv"
24
26
"time"
25
27
26
28
httptransport "github.com/go-openapi/runtime/client"
@@ -30,6 +32,7 @@ import (
30
32
31
33
"github.com/netbox-community/go-netbox/v3/netbox/client/extras"
32
34
"github.com/netbox-community/netbox-operator/pkg/netbox/interfaces"
35
+ "k8s.io/client-go/tools/metrics"
33
36
)
34
37
35
38
const (
@@ -64,6 +67,20 @@ func (r *NetboxClient) VerifyNetboxConfiguration() error {
64
67
return nil
65
68
}
66
69
70
+ type InstrumentedRoundTripper struct {
71
+ Transport http.RoundTripper
72
+ }
73
+
74
+ func (irt * InstrumentedRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
75
+ resp , err := irt .Transport .RoundTrip (req )
76
+ if err != nil {
77
+ return nil , err
78
+ }
79
+
80
+ metrics .RequestResult .Increment (context .TODO (), strconv .Itoa (resp .StatusCode ), req .Method , req .Host )
81
+ return resp , nil
82
+ }
83
+
67
84
func GetNetboxClient () (* NetboxClient , error ) {
68
85
69
86
logger := log .StandardLogger ()
@@ -92,8 +109,10 @@ func GetNetboxClient() (*NetboxClient, error) {
92
109
}
93
110
94
111
httpClient := & http.Client {
95
- Transport : & http.Transport {
96
- TLSClientConfig : tlsConfig ,
112
+ Transport : & InstrumentedRoundTripper {
113
+ Transport : & http.Transport {
114
+ TLSClientConfig : tlsConfig ,
115
+ },
97
116
},
98
117
Timeout : time .Second * time .Duration (RequestTimeout ),
99
118
}
0 commit comments