-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for oci cli metrics (#268)
* Add support for OCI Client metrics
- Loading branch information
1 parent
47fd880
commit b4a8ffa
Showing
5 changed files
with
192 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright (c) 2023 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/oracle/oci-go-sdk/v65/common" | ||
) | ||
|
||
// HttpRequestDispatcherWrapper is a wrapper around standard common.HTTPRequestDispatcher to handle | ||
// metrics | ||
type HttpRequestDispatcherWrapper struct { | ||
dispatcher common.HTTPRequestDispatcher | ||
region string | ||
} | ||
|
||
// Do is wrapper implementation of common.HTTPRequestDispatcher Do method | ||
func (wrapper HttpRequestDispatcherWrapper) Do(req *http.Request) (*http.Response, error) { | ||
t := time.Now() | ||
resp, err := wrapper.dispatcher.Do(req) | ||
defer func() { | ||
// taken from https://docs.oracle.com/en-us/iaas/Content/API/Concepts/usingapi.htm | ||
// a URL consists of a version string and then a resource | ||
urlSplit := strings.Split(req.URL.Path, "/") | ||
if len(urlSplit) < 2 { | ||
return | ||
} | ||
resource := urlSplit[2] | ||
IncRequestCounter(err, resource, req.Method, wrapper.region, resp) | ||
ObserverRequestDuration(resource, req.Method, wrapper.region, time.Since(t)) | ||
}() | ||
return resp, err | ||
} | ||
|
||
// NewHttpRequestDispatcherWrapper creates a new instance of HttpRequestDispatcherWrapper | ||
func NewHttpRequestDispatcherWrapper(dispatcher common.HTTPRequestDispatcher, region string) HttpRequestDispatcherWrapper { | ||
return HttpRequestDispatcherWrapper{ | ||
dispatcher: dispatcher, | ||
region: region, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright (c) 2023 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
type verb string | ||
|
||
const ( | ||
SubSystemOCI = "oci" | ||
OCIRequestsTotal = "requests_total" | ||
Duration = "request_duration" | ||
Resource = "resource" | ||
StatusCode = "status_code" | ||
Operation = "operation" | ||
|
||
Region = "region" | ||
Get string = "get" | ||
List string = "list" | ||
Create string = "create" | ||
Update string = "update" | ||
Delete string = "delete" | ||
) | ||
|
||
var ( | ||
ociRequestCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Subsystem: SubSystemOCI, | ||
Name: OCIRequestsTotal, | ||
Help: "OCI API requests total.", | ||
}, | ||
[]string{Resource, StatusCode, Operation, Region}, | ||
) | ||
ociRequestDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Subsystem: SubSystemOCI, | ||
Name: Duration, | ||
Help: "Duration/Latency of HTTP requests to OCI", | ||
}, []string{Resource, Operation, Region}) | ||
) | ||
|
||
// IncRequestCounter increments the request count metric for the given resource. | ||
// Unknown errors from request dispatcher will have response code of 999 | ||
func IncRequestCounter(err error, resource string, operation string, region string, response *http.Response) { | ||
statusCode := 999 | ||
if err == nil { | ||
statusCode = response.StatusCode | ||
} | ||
ociRequestCounter.With(prometheus.Labels{ | ||
Resource: resource, | ||
Operation: operation, | ||
StatusCode: strconv.Itoa(statusCode), | ||
Region: region, | ||
}).Inc() | ||
} | ||
|
||
// ObserverRequestDuration observes the request duration for the partcular OCI request | ||
func ObserverRequestDuration(resource string, operation string, region string, duration time.Duration) { | ||
ociRequestDurationSeconds.With(prometheus.Labels{ | ||
Resource: resource, | ||
Operation: operation, | ||
Region: region, | ||
}).Observe(duration.Seconds()) | ||
} | ||
|
||
func init() { | ||
metrics.Registry.MustRegister(ociRequestCounter) | ||
metrics.Registry.MustRegister(ociRequestDurationSeconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.