Skip to content

Commit be67e9d

Browse files
authored
Add metrics for rest requests towards netbox (#231)
add metrics for rest requests towards netbox
1 parent 7522f01 commit be67e9d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pkg/netbox/api/client.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ limitations under the License.
1717
package api
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"crypto/x509"
2223
"fmt"
2324
"net/http"
25+
"strconv"
2426
"time"
2527

2628
httptransport "github.com/go-openapi/runtime/client"
@@ -30,6 +32,7 @@ import (
3032

3133
"github.com/netbox-community/go-netbox/v3/netbox/client/extras"
3234
"github.com/netbox-community/netbox-operator/pkg/netbox/interfaces"
35+
"k8s.io/client-go/tools/metrics"
3336
)
3437

3538
const (
@@ -64,6 +67,20 @@ func (r *NetboxClient) VerifyNetboxConfiguration() error {
6467
return nil
6568
}
6669

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+
6784
func GetNetboxClient() (*NetboxClient, error) {
6885

6986
logger := log.StandardLogger()
@@ -92,8 +109,10 @@ func GetNetboxClient() (*NetboxClient, error) {
92109
}
93110

94111
httpClient := &http.Client{
95-
Transport: &http.Transport{
96-
TLSClientConfig: tlsConfig,
112+
Transport: &InstrumentedRoundTripper{
113+
Transport: &http.Transport{
114+
TLSClientConfig: tlsConfig,
115+
},
97116
},
98117
Timeout: time.Second * time.Duration(RequestTimeout),
99118
}

0 commit comments

Comments
 (0)