Skip to content

Commit

Permalink
Responded to Doug's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zasweq committed Apr 23, 2024
1 parent 4a14a16 commit 1f6a60c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 22 deletions.
7 changes: 2 additions & 5 deletions stats/opentelemetry/client_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ const (
// ClientAttemptRcvdCompressedTotalMessageSize is the compressed message
// bytes received per call attempt.
ClientAttemptRcvdCompressedTotalMessageSize Metric = "grpc.client.attempt.rcvd_total_compressed_message_size"
// ClientCallDurationName is the time taken by gRPC to complete an RPC from
// ClientCallDuration is the time taken by gRPC to complete an RPC from
// application's perspective.
ClientCallDurationName Metric = "grpc.client.call.duration"
ClientCallDuration Metric = "grpc.client.call.duration"
)

// DefaultClientMetrics are the default client metrics provided by this module.
var DefaultClientMetrics = NewMetrics(ClientAttemptStarted, ClientAttemptDuration, ClientAttemptSentCompressedTotalMessageSize, ClientAttemptRcvdCompressedTotalMessageSize, ClientCallDurationName)
123 changes: 110 additions & 13 deletions stats/opentelemetry/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"strings"
"testing"
"time"

Expand All @@ -36,19 +37,115 @@ import (
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
)

func ExampleNewMetrics() {
// Will disable metrics, set with no metrics specified.
NewMetrics()
// Will enable these two metrics.
NewMetrics(ClientAttemptDuration, ServerCallDuration)
// Use DefaultClientMetrics to enable default client metrics. Equivalent to
// unset, which will pick up defaults.
func Example_dialOption() {
// This is setting default bounds for a view. Setting these bounds through
// meter provider from SDK is recommended, as API calls in this module
// provide default bounds, but these calls are not guaranteed to be stable
// and API implementors are not required to implement bounds. Setting bounds
// through SDK ensures the bounds get picked up. The specific fields in
// Aggregation take precedence over defaults from API. For any fields unset
// in aggregation, defaults get picked up, so can have a mix of fields from
// SDK and fields created from API call. The overridden views themselves
// also follow same logic, only the specific views being created in the SDK
// use SDK information, the rest are created from API call.
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(
metric.WithReader(reader),
metric.WithView(metric.NewView(metric.Instrument{
Name: "grpc.client.call.duration",
},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: DefaultSizeBounds, // The specific fields set in SDK take precedence over API.
},
},
)),
)

opts := Options{
MetricsOptions: MetricsOptions{
MeterProvider: provider,
Metrics: DefaultMetrics, // equivalent to unset - distinct from empty
TargetAttributeFilter: func(str string) bool {
if strings.HasPrefix(str, "dns") { // Filter out DNS targets.
return false
}
return true
},
},
}
do := DialOption(opts)
cc, _ := grpc.NewClient("<target string>", do)
// Handle err.
if cc != nil {
defer cc.Close()
}
}

func Example_serverOption() {
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
opts := Options{
MetricsOptions: MetricsOptions{
MeterProvider: provider,
Metrics: DefaultMetrics,
MethodAttributeFilter: func(str string) bool {
if str == "/grpc.testing.TestService/UnaryCall" {
return false
}
// Will allow duplex/any other type of RPC.
return true
},
},
}
cc, _ := grpc.NewClient("some-target", DialOption(opts))
// Handle err.
defer cc.Close()
}

func ExampleMetrics_excludeSome() {
// To exclude specific metrics, initialize Options as follows:
opts := Options{
MetricsOptions: MetricsOptions{
Metrics: DefaultMetrics.Remove(ClientAttemptDuration, ClientAttemptRcvdCompressedTotalMessageSize),
},
}
do := DialOption(opts)
cc, _ := grpc.NewClient("<target string>", do)
// Handle err.
if cc != nil {
defer cc.Close()
}
}

func ExampleMetrics_Remove() {
// Use DefaultClientMetrics without ClientAttemptDuration and
// ClientCallDurationName.
DefaultClientMetrics.Remove(ClientAttemptDuration, ClientCallDurationName)
func ExampleMetrics_disableAll() {
// To disable all metrics, initialize Options as follows:
opts := Options{
MetricsOptions: MetricsOptions{
Metrics: NewMetrics(), // Distinct to nil, which creates default metrics. This empty set creates no metrics.
},
}
do := DialOption(opts)
cc, _ := grpc.NewClient("<target string>", do)
// Handle err.
if cc != nil {
defer cc.Close()
}
}

func ExampleMetrics_enableSome() {
// To only create specific metrics, initialize Options as follows:
opts := Options{
MetricsOptions: MetricsOptions{
Metrics: NewMetrics(ClientAttemptDuration, ClientAttemptRcvdCompressedTotalMessageSize), // only create these metrics
},
}
do := DialOption(opts)
cc, _ := grpc.NewClient("<target string>", do)
// Handle err.
if cc != nil {
defer cc.Close()
}
}

var defaultTestTimeout = 5 * time.Second
Expand Down Expand Up @@ -121,13 +218,13 @@ func setup(t *testing.T, tafOn bool, maf func(string) bool) (*metric.ManualReade
if err := ss.Start([]grpc.ServerOption{ServerOption(Options{
MetricsOptions: MetricsOptions{
MeterProvider: provider,
Metrics: DefaultServerMetrics,
Metrics: DefaultMetrics,
TargetAttributeFilter: taf,
MethodAttributeFilter: maf,
}})}, DialOption(Options{
MetricsOptions: MetricsOptions{
MeterProvider: provider,
Metrics: DefaultClientMetrics,
Metrics: DefaultMetrics,
TargetAttributeFilter: taf,
MethodAttributeFilter: maf,
},
Expand Down
4 changes: 3 additions & 1 deletion stats/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Metrics struct {
metrics map[Metric]bool
}

// NewMetrics returns a Metrics containing the Metric's provided.
// NewMetrics returns a Metrics containing Metrics.
func NewMetrics(metrics ...Metric) *Metrics {
newMetrics := make(map[Metric]bool)
for _, metric := range metrics {
Expand Down Expand Up @@ -293,4 +293,6 @@ var (
DefaultLatencyBounds = []float64{0, 0.00001, 0.00005, 0.0001, 0.0003, 0.0006, 0.0008, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.008, 0.01, 0.013, 0.016, 0.02, 0.025, 0.03, 0.04, 0.05, 0.065, 0.08, 0.1, 0.13, 0.16, 0.2, 0.25, 0.3, 0.4, 0.5, 0.65, 0.8, 1, 2, 5, 10, 20, 50, 100} // provide "advice" through API, SDK should set this too
// DefaultSizeBounds are the default bounds for metrics which record size.
DefaultSizeBounds = []float64{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296}
// DefaultMetrics are the default metrics provided by this module.
DefaultMetrics = NewMetrics(ClientAttemptStarted, ClientAttemptDuration, ClientAttemptSentCompressedTotalMessageSize, ClientAttemptRcvdCompressedTotalMessageSize, ClientCallDuration, ServerCallStarted, ServerCallSentCompressedTotalMessageSize, ServerCallRcvdCompressedTotalMessageSize, ServerCallDuration)
)
3 changes: 0 additions & 3 deletions stats/opentelemetry/server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,3 @@ const (
// server transport's perspective.
ServerCallDuration Metric = "grpc.server.call.duration"
)

// DefaultServerMetrics are the default server metrics provided by this module.
var DefaultServerMetrics = NewMetrics(ServerCallStarted, ServerCallSentCompressedTotalMessageSize, ServerCallRcvdCompressedTotalMessageSize, ServerCallDuration)

0 comments on commit 1f6a60c

Please sign in to comment.