Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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