-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
stats/opentelemetry: Add OpenTelemetry instrumentation component #7066
Closed
zasweq
wants to merge
11
commits into
grpc:master
from
zasweq:opentelemetry-instrumentation-component
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d60d637
Add OpenTelemetry instrumentation component
zasweq 78fef76
Responded to Doug's comments
zasweq 796d697
Responded to Doug's comments
zasweq 9597843
Responded to all of Yashes comments and some of Doug's comments
zasweq 179082f
Respodned to Doug's comments
zasweq 4196745
drop remove leading slash
zasweq e078766
Fix leading slash
zasweq 84eae8c
Responded to Doug's comments
zasweq 196e201
Responded to Doug's comments
zasweq 4a14a16
Vet
zasweq 1f6a60c
Responded to Doug's comments
zasweq 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/* | ||
* Copyright 2024 gRPC 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 opentelemetry | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/stats" | ||
"google.golang.org/grpc/status" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
type clientStatsHandler struct { | ||
o Options | ||
|
||
clientMetrics clientMetrics | ||
} | ||
|
||
func (csh *clientStatsHandler) initializeMetrics() { | ||
// Will set no metrics to record, logically making this stats handler a | ||
// no-op. | ||
if csh.o.MetricsOptions.MeterProvider == nil { | ||
return | ||
} | ||
|
||
meter := csh.o.MetricsOptions.MeterProvider.Meter("grpc-go " + grpc.Version) | ||
if meter == nil { | ||
return | ||
} | ||
|
||
setOfMetrics := csh.o.MetricsOptions.Metrics.metrics | ||
|
||
clientMetrics := clientMetrics{} | ||
|
||
clientMetrics.attemptStarted = createInt64Counter(setOfMetrics, "grpc.client.attempt.started", meter, metric.WithUnit("attempt"), metric.WithDescription("Number of client call attempts started.")) | ||
clientMetrics.attemptDuration = createFloat64Histogram(setOfMetrics, "grpc.client.attempt.duration", meter, metric.WithUnit("s"), metric.WithDescription("End-to-end time taken to complete a client call attempt."), metric.WithExplicitBucketBoundaries(DefaultLatencyBounds...)) | ||
clientMetrics.attemptSentTotalCompressedMessageSize = createInt64Histogram(setOfMetrics, "grpc.client.attempt.sent_total_compressed_message_size", meter, metric.WithUnit("By"), metric.WithDescription("Compressed message bytes sent per client call attempt."), metric.WithExplicitBucketBoundaries(DefaultSizeBounds...)) | ||
clientMetrics.attemptRcvdTotalCompressedMessageSize = createInt64Histogram(setOfMetrics, "grpc.client.attempt.rcvd_total_compressed_message_size", meter, metric.WithUnit("By"), metric.WithDescription("Compressed message bytes received per call attempt."), metric.WithExplicitBucketBoundaries(DefaultSizeBounds...)) | ||
clientMetrics.callDuration = createFloat64Histogram(setOfMetrics, "grpc.client.call.duration", meter, metric.WithUnit("s"), metric.WithDescription("Time taken by gRPC to complete an RPC from application's perspective."), metric.WithExplicitBucketBoundaries(DefaultLatencyBounds...)) | ||
|
||
csh.clientMetrics = clientMetrics | ||
} | ||
|
||
func (csh *clientStatsHandler) unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
ci := &callInfo{ | ||
target: csh.determineTarget(cc), | ||
method: removeLeadingSlash(csh.determineMethod(method, opts...)), | ||
} | ||
ctx = setCallInfo(ctx, ci) | ||
|
||
startTime := time.Now() | ||
err := invoker(ctx, method, req, reply, cc, opts...) | ||
csh.perCallMetrics(ctx, err, startTime, ci) | ||
return err | ||
} | ||
|
||
// determineTarget determines the target to record attributes with. This will be | ||
// "other" if target filter is set and specifies, the target name as is | ||
// otherwise. | ||
func (csh *clientStatsHandler) determineTarget(cc *grpc.ClientConn) string { | ||
target := cc.CanonicalTarget() | ||
if f := csh.o.MetricsOptions.TargetAttributeFilter; f != nil && !f(target) { | ||
target = "other" | ||
} | ||
return target | ||
} | ||
|
||
// determineMethod determines the method to record attributes with. This will be | ||
// "other" if StaticMethod isn't specified or if method filter is set and | ||
// specifies, the method name as is otherwise. | ||
func (csh *clientStatsHandler) determineMethod(method string, opts ...grpc.CallOption) string { | ||
for _, opt := range opts { | ||
if _, ok := opt.(grpc.StaticMethodCallOption); ok { | ||
return method | ||
} | ||
} | ||
return "other" | ||
} | ||
|
||
func (csh *clientStatsHandler) streamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
ci := &callInfo{ | ||
target: csh.determineTarget(cc), | ||
method: removeLeadingSlash(csh.determineMethod(method, opts...)), | ||
} | ||
ctx = setCallInfo(ctx, ci) | ||
startTime := time.Now() | ||
|
||
callback := func(err error) { | ||
csh.perCallMetrics(ctx, err, startTime, ci) | ||
} | ||
opts = append([]grpc.CallOption{grpc.OnFinish(callback)}, opts...) | ||
return streamer(ctx, desc, cc, method, opts...) | ||
} | ||
|
||
func (csh *clientStatsHandler) perCallMetrics(ctx context.Context, err error, startTime time.Time, ci *callInfo) { | ||
s := status.Convert(err) | ||
callLatency := float64(time.Since(startTime)) / float64(time.Second) | ||
if csh.clientMetrics.callDuration != nil { | ||
csh.clientMetrics.callDuration.Record(ctx, callLatency, metric.WithAttributes(attribute.String("grpc.method", ci.method), attribute.String("grpc.target", ci.target), attribute.String("grpc.status", canonicalString(s.Code())))) | ||
} | ||
} | ||
|
||
// TagConn exists to satisfy stats.Handler. | ||
func (csh *clientStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context { | ||
return ctx | ||
} | ||
|
||
// HandleConn exists to satisfy stats.Handler. | ||
func (csh *clientStatsHandler) HandleConn(context.Context, stats.ConnStats) {} | ||
|
||
// TagRPC implements per RPC attempt context management. | ||
func (csh *clientStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { | ||
mi := &metricsInfo{ // populates information about RPC start. | ||
startTime: time.Now(), | ||
} | ||
ri := &rpcInfo{ | ||
mi: mi, | ||
} | ||
return setRPCInfo(ctx, ri) | ||
} | ||
|
||
func (csh *clientStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) { | ||
ri := getRPCInfo(ctx) | ||
if ri == nil { | ||
// Shouldn't happen because TagRPC populates this information. | ||
return | ||
} | ||
csh.processRPCEvent(ctx, rs, ri.mi) | ||
} | ||
|
||
func (csh *clientStatsHandler) processRPCEvent(ctx context.Context, s stats.RPCStats, mi *metricsInfo) { | ||
switch st := s.(type) { | ||
case *stats.InHeader, *stats.OutHeader, *stats.InTrailer, *stats.OutTrailer: | ||
case *stats.Begin: | ||
ci := getCallInfo(ctx) | ||
if ci == nil { | ||
logger.Error("ctx passed into client side stats handler metrics event handling has no metrics data present") | ||
return | ||
} | ||
|
||
if csh.clientMetrics.attemptStarted != nil { | ||
csh.clientMetrics.attemptStarted.Add(ctx, 1, metric.WithAttributes(attribute.String("grpc.method", ci.method), attribute.String("grpc.target", ci.target))) | ||
} | ||
case *stats.OutPayload: | ||
atomic.AddInt64(&mi.sentCompressedBytes, int64(st.CompressedLength)) | ||
case *stats.InPayload: | ||
atomic.AddInt64(&mi.recvCompressedBytes, int64(st.CompressedLength)) | ||
case *stats.End: | ||
csh.processRPCEnd(ctx, mi, st) | ||
default: | ||
// Shouldn't happen. gRPC calls into stats handler, and will never not | ||
// be one of the types above. | ||
logger.Errorf("Received unexpected stats type (%T) with data: %v", s, s) | ||
} | ||
} | ||
|
||
func (csh *clientStatsHandler) processRPCEnd(ctx context.Context, mi *metricsInfo, e *stats.End) { | ||
ci := getCallInfo(ctx) | ||
if ci == nil { | ||
// Shouldn't happen, set by interceptor, defensive programming. | ||
return | ||
} | ||
latency := float64(time.Since(mi.startTime)) / float64(time.Second) | ||
var st string | ||
if e.Error != nil { | ||
s, _ := status.FromError(e.Error) | ||
st = canonicalString(s.Code()) | ||
} else { | ||
st = "OK" | ||
} | ||
clientAttributeOption := metric.WithAttributes(attribute.String("grpc.method", ci.method), attribute.String("grpc.target", ci.target), attribute.String("grpc.status", st)) | ||
if csh.clientMetrics.attemptDuration != nil { | ||
csh.clientMetrics.attemptDuration.Record(ctx, latency, clientAttributeOption) | ||
} | ||
|
||
if csh.clientMetrics.attemptSentTotalCompressedMessageSize != nil { | ||
csh.clientMetrics.attemptSentTotalCompressedMessageSize.Record(ctx, atomic.LoadInt64(&mi.sentCompressedBytes), clientAttributeOption) | ||
} | ||
|
||
if csh.clientMetrics.attemptRcvdTotalCompressedMessageSize != nil { | ||
csh.clientMetrics.attemptRcvdTotalCompressedMessageSize.Record(ctx, atomic.LoadInt64(&mi.recvCompressedBytes), clientAttributeOption) | ||
} | ||
} | ||
|
||
const ( | ||
// ClientAttemptStarted is the number of client call attempts started. | ||
ClientAttemptStarted Metric = "grpc.client.attempt.started" | ||
// ClientAttemptDuration is the end-to-end time taken to complete a client | ||
// call attempt. | ||
ClientAttemptDuration Metric = "grpc.client.attempt.duration" | ||
// ClientAttemptSentCompressedTotalMessageSize is the compressed message | ||
// bytes sent per client call attempt. | ||
ClientAttemptSentCompressedTotalMessageSize Metric = "grpc.client.attempt.sent_total_compressed_message_size" | ||
// ClientAttemptRcvdCompressedTotalMessageSize is the compressed message | ||
// bytes received per call attempt. | ||
ClientAttemptRcvdCompressedTotalMessageSize Metric = "grpc.client.attempt.rcvd_total_compressed_message_size" | ||
// ClientCallDuration is the time taken by gRPC to complete an RPC from | ||
// application's perspective. | ||
ClientCallDuration Metric = "grpc.client.call.duration" | ||
) |
Oops, something went wrong.
Oops, something went wrong.
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.
what's the behavior over here? Can this actually happen?
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 is the no-op meter provider thingy. It just doesn't record metrics. I don't think this can actually happen, this is mainly for defensive programming.