-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metrics for otelhttp client #427
Changes from all commits
ccdcdd6
755ead1
5584474
d6a2098
5f70e63
f8745c6
ebcde2a
fa46122
6ef9064
7bca3f1
281e8f0
44f984f
3e3a025
b166165
9176a2e
acfd1c6
596d5f6
e13ab22
be431b8
439e653
acc3233
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 otelhttp | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/semconv" | ||
|
||
"go.opentelemetry.io/otel/unit" | ||
|
||
"go.opentelemetry.io/otel/api/metric" | ||
"go.opentelemetry.io/otel/label" | ||
) | ||
|
||
type instrumentedTransport struct { | ||
meter metric.Meter | ||
base *Transport | ||
clientDurationRecorder metric.Float64ValueRecorder | ||
} | ||
|
||
type tracker struct { | ||
ctx context.Context | ||
start time.Time | ||
body io.ReadCloser | ||
endOnce sync.Once | ||
labels []label.KeyValue | ||
|
||
clientDurationRecorder metric.Float64ValueRecorder | ||
} | ||
|
||
func (trans *instrumentedTransport) applyConfig(c *config) { | ||
trans.base.applyConfig(c) | ||
|
||
trans.meter = c.Meter | ||
trans.createMeasures() | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper, delegating to Base and recording stats for the request. | ||
func (trans *instrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
labels := semconv.HTTPClientAttributesFromHTTPRequest(req) | ||
|
||
ctx := req.Context() | ||
tracker := &tracker{ | ||
start: time.Now(), | ||
ctx: ctx, | ||
clientDurationRecorder: trans.clientDurationRecorder, | ||
} | ||
|
||
resp, err := trans.base.RoundTrip(req) | ||
if err != nil { | ||
tracker.labels = append(labels, semconv.HTTPAttributesFromHTTPStatusCode(http.StatusInternalServerError)...) | ||
tracker.end() | ||
} else { | ||
tracker.labels = append(labels, semconv.HTTPAttributesFromHTTPStatusCode(resp.StatusCode)...) | ||
if resp.Body == nil { | ||
tracker.end() | ||
} else { | ||
tracker.body = resp.Body | ||
resp.Body = wrappedBodyIO(tracker, resp.Body) | ||
} | ||
} | ||
return resp, err | ||
} | ||
|
||
// wrappedBodyIO returns a wrapped version of the original | ||
// Body and only implements the same combination of additional | ||
// interfaces as the original. | ||
func wrappedBodyIO(wrapper io.ReadCloser, body io.ReadCloser) io.ReadCloser { | ||
if wr, ok := body.(io.Writer); ok { | ||
return struct { | ||
io.ReadCloser | ||
io.Writer | ||
}{wrapper, wr} | ||
} | ||
return wrapper | ||
} | ||
|
||
func (trans *instrumentedTransport) createMeasures() { | ||
var err error | ||
trans.clientDurationRecorder, err = trans.meter.NewFloat64ValueRecorder( | ||
clientRequestDuration, | ||
metric.WithDescription("measures the duration of the outbound HTTP request"), | ||
metric.WithUnit(unit.Milliseconds), | ||
) | ||
handleErr(err) | ||
} | ||
|
||
var _ io.ReadCloser = (*tracker)(nil) | ||
|
||
func (tracker *tracker) end() { | ||
tracker.endOnce.Do(func() { | ||
latencyMs := float64(time.Since(tracker.start)) / float64(time.Millisecond) | ||
tracker.clientDurationRecorder.Record(tracker.ctx, latencyMs, tracker.labels...) | ||
}) | ||
} | ||
|
||
func (tracker *tracker) Read(b []byte) (int, error) { | ||
n, err := tracker.body.Read(b) | ||
switch err { | ||
case nil: | ||
return n, nil | ||
case io.EOF: | ||
tracker.end() | ||
} | ||
return n, err | ||
} | ||
|
||
func (tracker *tracker) Close() error { | ||
tracker.end() | ||
return tracker.body.Close() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,11 @@ var _ http.RoundTripper = &Transport{} | |
|
||
// NewTransport wraps the provided http.RoundTripper with one that | ||
// starts a span and injects the span context into the outbound request headers. | ||
func NewTransport(base http.RoundTripper, opts ...Option) *Transport { | ||
t := Transport{ | ||
rt: base, | ||
func NewTransport(base http.RoundTripper, opts ...Option) http.RoundTripper { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is the primary mechanism by which I expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to split up the implementation of metrics and tracing parts for ease of maintenance. |
||
t := instrumentedTransport{ | ||
base: &Transport{ | ||
rt: base, | ||
}, | ||
} | ||
|
||
defaultOpts := []Option{ | ||
|
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.
This should include the response status not
http.StatusInternalServerError
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.
In case of HTTP connection error occurred, should we recorded nothing or 500 as the HTTP status code. What's your opinion?