This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Api latency #58
Merged
Merged
Api latency #58
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8deb87e
Implement API Latency metric for Openstack
stevesloka 51d4fe8
Update docs with API Latency request change
stevesloka d4a8c60
Refactor http client / move clusterType to main
stevesloka 460796d
Remove unneeded code
stevesloka 074208f
Fix tests / Fix passing clustertype
stevesloka 1322987
Refactor http transport
stevesloka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright © 2018 Heptio | ||
// 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 openstack | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"net/http" | ||
"net/http/httptrace" | ||
"time" | ||
|
||
localmetrics "github.com/heptio/gimbal/discovery/pkg/metrics" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// LogRoundTripper satisfies the http.RoundTripper interface and is used to | ||
// customize the default Gophercloud RoundTripper to allow for logging. | ||
type LogRoundTripper struct { | ||
RoundTripper http.RoundTripper | ||
numReauthAttempts int | ||
Log *logrus.Logger | ||
Metrics *localmetrics.DiscovererMetrics | ||
ClusterName string | ||
ClusterType string | ||
} | ||
|
||
// RoundTrip performs a round-trip HTTP request and logs relevant information about it. | ||
func (lrt *LogRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { | ||
lrt.Log.Debugf("Request URL: %s", request.URL) | ||
|
||
start := time.Now() | ||
var latency time.Duration | ||
|
||
trace := &httptrace.ClientTrace{ | ||
GotFirstResponseByte: func() { | ||
latency = time.Now().Sub(start) | ||
lrt.Log.Debug("-- API Latency: ", math.Floor(latency.Seconds()*1e3)) | ||
}, | ||
} | ||
request = request.WithContext(httptrace.WithClientTrace(request.Context(), trace)) | ||
|
||
response, err := lrt.RoundTripper.RoundTrip(request) | ||
if response == nil { | ||
return nil, err | ||
} | ||
|
||
if response.StatusCode == http.StatusUnauthorized { | ||
if lrt.numReauthAttempts == 3 { | ||
return response, fmt.Errorf("Tried to re-authenticate 3 times with no success.") | ||
} | ||
lrt.numReauthAttempts++ | ||
} | ||
|
||
lrt.Log.Debugf("-- Response Status: %s", response.Status) | ||
|
||
lrt.Metrics.APILatencyMetric(lrt.ClusterName, lrt.ClusterType, request.URL.Path, latency) | ||
|
||
return response, nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will overwrite the
openstack.LogRoundTripper
that we are configuring in L126.I think something like this would work?