google.golang.org/grpc@v1.72.2/stats/opentelemetry/example_test.go (about) 1 /* 2 * Copyright 2024 gRPC authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package opentelemetry_test 18 19 import ( 20 "google.golang.org/grpc" 21 "google.golang.org/grpc/credentials/insecure" 22 "google.golang.org/grpc/stats" 23 "google.golang.org/grpc/stats/opentelemetry" 24 25 "go.opentelemetry.io/otel/sdk/metric" 26 ) 27 28 func Example_dialOption() { 29 // This is setting default bounds for a view. Setting these bounds through 30 // meter provider from SDK is recommended, as API calls in this module 31 // provide default bounds, but these calls are not guaranteed to be stable 32 // and API implementors are not required to implement bounds. Setting bounds 33 // through SDK ensures the bounds get picked up. The specific fields in 34 // Aggregation take precedence over defaults from API. For any fields unset 35 // in aggregation, defaults get picked up, so can have a mix of fields from 36 // SDK and fields created from API call. The overridden views themselves 37 // also follow same logic, only the specific views being created in the SDK 38 // use SDK information, the rest are created from API call. 39 reader := metric.NewManualReader() 40 provider := metric.NewMeterProvider( 41 metric.WithReader(reader), 42 metric.WithView(metric.NewView(metric.Instrument{ 43 Name: "grpc.client.call.duration", 44 }, 45 metric.Stream{ 46 Aggregation: metric.AggregationExplicitBucketHistogram{ 47 Boundaries: opentelemetry.DefaultSizeBounds, // The specific fields set in SDK take precedence over API. 48 }, 49 }, 50 )), 51 ) 52 53 opts := opentelemetry.Options{ 54 MetricsOptions: opentelemetry.MetricsOptions{ 55 MeterProvider: provider, 56 Metrics: opentelemetry.DefaultMetrics(), // equivalent to unset - distinct from empty 57 }, 58 } 59 do := opentelemetry.DialOption(opts) 60 cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials())) 61 if err != nil { 62 // Handle err. 63 } 64 defer cc.Close() 65 } 66 67 func Example_serverOption() { 68 reader := metric.NewManualReader() 69 provider := metric.NewMeterProvider(metric.WithReader(reader)) 70 opts := opentelemetry.Options{ 71 MetricsOptions: opentelemetry.MetricsOptions{ 72 MeterProvider: provider, 73 // Because Metrics is unset, the user will get default metrics. 74 MethodAttributeFilter: func(str string) bool { 75 // Will allow duplex/any other type of RPC. 76 return str != "/grpc.testing.TestService/UnaryCall" 77 }, 78 }, 79 } 80 cc, err := grpc.NewClient("some-target", opentelemetry.DialOption(opts), grpc.WithTransportCredentials(insecure.NewCredentials())) 81 if err != nil { 82 // Handle err. 83 } 84 defer cc.Close() 85 } 86 87 func ExampleMetrics_excludeSome() { 88 // To exclude specific metrics, initialize Options as follows: 89 opts := opentelemetry.Options{ 90 MetricsOptions: opentelemetry.MetricsOptions{ 91 Metrics: opentelemetry.DefaultMetrics().Remove(opentelemetry.ClientAttemptDurationMetricName, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSizeMetricName), 92 }, 93 } 94 do := opentelemetry.DialOption(opts) 95 cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials())) 96 if err != nil { 97 // Handle err. 98 } 99 defer cc.Close() 100 } 101 102 func ExampleMetrics_disableAll() { 103 // To disable all metrics, initialize Options as follows: 104 opts := opentelemetry.Options{ 105 MetricsOptions: opentelemetry.MetricsOptions{ 106 Metrics: stats.NewMetricSet(), // Distinct to nil, which creates default metrics. This empty set creates no metrics. 107 }, 108 } 109 do := opentelemetry.DialOption(opts) 110 cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials())) 111 if err != nil { 112 // Handle err. 113 } 114 defer cc.Close() 115 } 116 117 func ExampleMetrics_enableSome() { 118 // To only create specific metrics, initialize Options as follows: 119 opts := opentelemetry.Options{ 120 MetricsOptions: opentelemetry.MetricsOptions{ 121 Metrics: stats.NewMetricSet(opentelemetry.ClientAttemptDurationMetricName, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSizeMetricName), // only create these metrics 122 }, 123 } 124 do := opentelemetry.DialOption(opts) 125 cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials())) 126 if err != nil { // might fail vet 127 // Handle err. 128 } 129 defer cc.Close() 130 }