|
| 1 | +/* |
| 2 | +Copyright 2025 Google LLC |
| 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 bigtable |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + mexporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric" |
| 26 | + "go.opentelemetry.io/contrib/detectors/gcp" |
| 27 | + "go.opentelemetry.io/otel/attribute" |
| 28 | + "go.opentelemetry.io/otel/sdk/metric" |
| 29 | + "go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 30 | + "go.opentelemetry.io/otel/sdk/resource" |
| 31 | + "google.golang.org/api/option" |
| 32 | + "google.golang.org/grpc" |
| 33 | + "google.golang.org/grpc/experimental/stats" |
| 34 | + "google.golang.org/grpc/stats/opentelemetry" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + bigtableClientMonitoredResourceName = "bigtable_client" |
| 39 | + bigtableClientMetricPrefix = "bigtable.googleapis.com/internal/client/" |
| 40 | + unKnownAtttr = "unknown" |
| 41 | +) |
| 42 | + |
| 43 | +var latenciesBoundaries = []float64{0.0, 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.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 400.0, 800.0, 1600.0, 3200.0} // max is 53.3 minutes |
| 44 | + |
| 45 | +// bigtable_client monitored resource labels |
| 46 | +type bigtableClientMonitoredResource struct { |
| 47 | + project string // project |
| 48 | + instance string // instance |
| 49 | + appProfile string // app_profile |
| 50 | + clientProject string // client_project |
| 51 | + cloudPlatform string // cloud_platform |
| 52 | + region string // client_region |
| 53 | + hostID string // host_id |
| 54 | + hostName string // host_name |
| 55 | + clientName string // client_name |
| 56 | + clientUID string // uuid |
| 57 | + resource *resource.Resource |
| 58 | +} |
| 59 | + |
| 60 | +func (bmr *bigtableClientMonitoredResource) exporter() (metric.Exporter, error) { |
| 61 | + exporter, err := mexporter.New( |
| 62 | + mexporter.WithProjectID(bmr.clientProject), |
| 63 | + mexporter.WithMetricDescriptorTypeFormatter(metricFormatter), |
| 64 | + mexporter.WithCreateServiceTimeSeries(), |
| 65 | + mexporter.WithMonitoredResourceDescription(bigtableClientMonitoredResourceName, []string{"project_id", "instance", "app_profile", "client_project", "cloud_platform", "host_id", "host_name", "client_name", "uuid", "region"}), |
| 66 | + ) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("bigtable: creating metrics exporter: %w", err) |
| 69 | + } |
| 70 | + return exporter, nil |
| 71 | +} |
| 72 | + |
| 73 | +func metricFormatter(m metricdata.Metrics) string { |
| 74 | + // converts grpc.lb.rls.target_picks to `bigtable.googleapis.com/internal/client/grpc/lb/rls/target_picks` |
| 75 | + return bigtableClientMetricPrefix + strings.ReplaceAll(string(m.Name), ".", "/") |
| 76 | +} |
| 77 | + |
| 78 | +func getAttribute(s *attribute.Set, key attribute.Key, defaultValue string) string { |
| 79 | + if val, ok := s.Value(key); ok { |
| 80 | + return val.AsString() |
| 81 | + } |
| 82 | + return defaultValue |
| 83 | +} |
| 84 | + |
| 85 | +func newBigtableClientMonitoredResource(ctx context.Context, project, appProfile, instance, clientName, clientUID string, opts ...resource.Option) (*bigtableClientMonitoredResource, error) { |
| 86 | + detectedAttrs, err := resource.New(ctx, opts...) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + smr := &bigtableClientMonitoredResource{ |
| 91 | + project: project, |
| 92 | + instance: instance, |
| 93 | + appProfile: appProfile, |
| 94 | + clientName: clientName, |
| 95 | + clientUID: clientUID, |
| 96 | + } |
| 97 | + s := detectedAttrs.Set() |
| 98 | + // Attempt to use resource detector project id if project id wasn't |
| 99 | + // identified using ADC as a last resort. Otherwise metrics cannot be started. |
| 100 | + |
| 101 | + smr.clientProject = getAttribute(s, "cloud.account.id", unKnownAtttr) |
| 102 | + smr.cloudPlatform = getAttribute(s, "cloud.platform", unKnownAtttr) |
| 103 | + smr.hostName = getAttribute(s, "host.name", unKnownAtttr) |
| 104 | + smr.hostID = getAttribute(s, "host.id", unKnownAtttr) |
| 105 | + if smr.hostID == "unknown" { |
| 106 | + // cloud run / cloud functions have faas.id instead of host.id |
| 107 | + |
| 108 | + smr.hostID = getAttribute(s, "faas.id", unKnownAtttr) |
| 109 | + } |
| 110 | + smr.region = getAttribute(s, "cloud.region", "global") |
| 111 | + smr.resource, err = resource.New(ctx, resource.WithAttributes([]attribute.KeyValue{ |
| 112 | + {Key: "gcp.resource_type", Value: attribute.StringValue(bigtableClientMonitoredResourceName)}, |
| 113 | + {Key: "project_id", Value: attribute.StringValue(project)}, |
| 114 | + {Key: "app_profile", Value: attribute.StringValue(smr.appProfile)}, |
| 115 | + {Key: "region", Value: attribute.StringValue(smr.region)}, |
| 116 | + {Key: "instance", Value: attribute.StringValue(smr.instance)}, |
| 117 | + {Key: "cloud_platform", Value: attribute.StringValue(smr.cloudPlatform)}, |
| 118 | + {Key: "host_id", Value: attribute.StringValue(smr.hostID)}, |
| 119 | + {Key: "host_name", Value: attribute.StringValue(smr.hostName)}, |
| 120 | + {Key: "client_name", Value: attribute.StringValue(smr.clientName)}, |
| 121 | + {Key: "uuid", Value: attribute.StringValue(smr.clientUID)}, |
| 122 | + {Key: "client_project", Value: attribute.StringValue(smr.clientProject)}, |
| 123 | + }...)) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return smr, nil |
| 128 | +} |
| 129 | + |
| 130 | +type metricsContext struct { |
| 131 | + // client options passed to gRPC channels |
| 132 | + clientOpts []option.ClientOption |
| 133 | + // instance of metric reader used by gRPC client-side metrics |
| 134 | + provider *metric.MeterProvider |
| 135 | + // clean func to call when closing gRPC client |
| 136 | + close func() |
| 137 | +} |
| 138 | + |
| 139 | +type metricsConfig struct { |
| 140 | + project string // project_id |
| 141 | + instance string // instance |
| 142 | + appProfile string // app_profile |
| 143 | + clientName string // client_name |
| 144 | + clientUID string // uuid |
| 145 | + interval time.Duration |
| 146 | + customExporter *metric.Exporter |
| 147 | + manualReader *metric.ManualReader // used by tests |
| 148 | + disableExporter bool // used by tests disables exports |
| 149 | + resourceOpts []resource.Option // used by tests |
| 150 | +} |
| 151 | + |
| 152 | +func newOtelMetricsContext(ctx context.Context, cfg metricsConfig) (*metricsContext, error) { |
| 153 | + var exporter metric.Exporter |
| 154 | + meterOpts := []metric.Option{} |
| 155 | + if cfg.customExporter == nil { |
| 156 | + var ropts []resource.Option |
| 157 | + if cfg.resourceOpts != nil { |
| 158 | + ropts = cfg.resourceOpts |
| 159 | + } else { |
| 160 | + ropts = []resource.Option{resource.WithDetectors(gcp.NewDetector())} |
| 161 | + } |
| 162 | + smr, err := newBigtableClientMonitoredResource(ctx, cfg.project, cfg.appProfile, cfg.instance, cfg.clientName, cfg.clientUID, ropts...) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + exporter, err = smr.exporter() |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + meterOpts = append(meterOpts, metric.WithResource(smr.resource)) |
| 171 | + } else { |
| 172 | + exporter = *cfg.customExporter |
| 173 | + } |
| 174 | + interval := time.Minute |
| 175 | + if cfg.interval > 0 { |
| 176 | + interval = cfg.interval |
| 177 | + } |
| 178 | + meterOpts = append(meterOpts, |
| 179 | + // customer histogram boundaries |
| 180 | + metric.WithView( |
| 181 | + createHistogramView("grpc.client.attempt.duration", latenciesBoundaries), |
| 182 | + )) |
| 183 | + if cfg.manualReader != nil { |
| 184 | + meterOpts = append(meterOpts, metric.WithReader(cfg.manualReader)) |
| 185 | + } |
| 186 | + if !cfg.disableExporter { |
| 187 | + meterOpts = append(meterOpts, metric.WithReader( |
| 188 | + metric.NewPeriodicReader(&exporterLogSuppressor{Exporter: exporter}, metric.WithInterval(interval)))) |
| 189 | + } |
| 190 | + provider := metric.NewMeterProvider(meterOpts...) |
| 191 | + mo := opentelemetry.MetricsOptions{ |
| 192 | + MeterProvider: provider, |
| 193 | + Metrics: stats.NewMetrics( |
| 194 | + "grpc.client.attempt.duration", |
| 195 | + "grpc.lb.rls.default_target_picks", |
| 196 | + "grpc.lb.rls.target_picks", |
| 197 | + "grpc.lb.rls.failed_picks", |
| 198 | + "grpc.xds_client.server_failure", |
| 199 | + "grpc.xds_client.resource_updates_invalid", |
| 200 | + ), |
| 201 | + OptionalLabels: []string{"grpc.lb.locality"}, |
| 202 | + } |
| 203 | + opts := []option.ClientOption{ |
| 204 | + option.WithGRPCDialOption( |
| 205 | + opentelemetry.DialOption(opentelemetry.Options{MetricsOptions: mo})), |
| 206 | + option.WithGRPCDialOption( |
| 207 | + grpc.WithDefaultCallOptions(grpc.StaticMethodCallOption{})), |
| 208 | + } |
| 209 | + return &metricsContext{ |
| 210 | + clientOpts: opts, |
| 211 | + provider: provider, |
| 212 | + close: func() { |
| 213 | + provider.Shutdown(ctx) |
| 214 | + }, |
| 215 | + }, nil |
| 216 | +} |
| 217 | + |
| 218 | +// Silences permission errors after initial error is emitted to prevent |
| 219 | +// chatty logs. |
| 220 | +type exporterLogSuppressor struct { |
| 221 | + metric.Exporter |
| 222 | + emittedFailure bool |
| 223 | +} |
| 224 | + |
| 225 | +// Implements OTel SDK metric.Exporter interface to prevent noisy logs from |
| 226 | +// lack of credentials after initial failure. |
| 227 | +func (e *exporterLogSuppressor) Export(ctx context.Context, rm *metricdata.ResourceMetrics) error { |
| 228 | + if err := e.Exporter.Export(ctx, rm); err != nil && !e.emittedFailure { |
| 229 | + if strings.Contains(err.Error(), "PermissionDenied") { |
| 230 | + e.emittedFailure = true |
| 231 | + return fmt.Errorf("gRPC metrics failed due permission issue: %w", err) |
| 232 | + } |
| 233 | + return err |
| 234 | + } |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +func createHistogramView(name string, boundaries []float64) metric.View { |
| 239 | + return metric.NewView(metric.Instrument{ |
| 240 | + Name: name, |
| 241 | + Kind: metric.InstrumentKindHistogram, |
| 242 | + }, metric.Stream{ |
| 243 | + Name: name, |
| 244 | + Aggregation: metric.AggregationExplicitBucketHistogram{Boundaries: boundaries}, |
| 245 | + }) |
| 246 | +} |
0 commit comments