Skip to content

Commit

Permalink
add metrics for rest requests towards netbox
Browse files Browse the repository at this point in the history
  • Loading branch information
bruelea committed Feb 20, 2025
1 parent 7522f01 commit 89b9473
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/netbox/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package api

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"strconv"
"time"

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

"github.com/netbox-community/go-netbox/v3/netbox/client/extras"
"github.com/netbox-community/netbox-operator/pkg/netbox/interfaces"
"k8s.io/client-go/tools/metrics"
)

const (
Expand Down Expand Up @@ -64,6 +67,20 @@ func (r *NetboxClient) VerifyNetboxConfiguration() error {
return nil
}

type InstrumentedRoundTripper struct {
Transport http.RoundTripper
}

func (irt *InstrumentedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := irt.Transport.RoundTrip(req)
if err != nil {
return nil, err
}

metrics.RequestResult.Increment(context.TODO(), strconv.Itoa(resp.StatusCode), req.Method, req.Host)
return resp, nil
}

func GetNetboxClient() (*NetboxClient, error) {

logger := log.StandardLogger()
Expand Down Expand Up @@ -92,8 +109,10 @@ func GetNetboxClient() (*NetboxClient, error) {
}

httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Transport: &InstrumentedRoundTripper{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
},
Timeout: time.Second * time.Duration(RequestTimeout),
}
Expand Down

0 comments on commit 89b9473

Please sign in to comment.