|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * https://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 | +package com.google.cloud.spanner.spi.v1; |
| 17 | + |
| 18 | +import com.google.common.annotations.VisibleForTesting; |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import io.opencensus.stats.*; |
| 21 | +import io.opencensus.stats.Aggregation.Distribution; |
| 22 | +import io.opencensus.stats.Aggregation.Sum; |
| 23 | +import io.opencensus.stats.Measure.MeasureLong; |
| 24 | +import io.opencensus.tags.TagKey; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +@VisibleForTesting |
| 30 | +public class SpannerRpcViews { |
| 31 | + |
| 32 | + /** Unit to represent milliseconds. */ |
| 33 | + private static final String MILLISECOND = "ms"; |
| 34 | + /** Unit to represent counts. */ |
| 35 | + private static final String COUNT = "1"; |
| 36 | + |
| 37 | + /** TagKeys */ |
| 38 | + public static final TagKey METHOD = TagKey.create("grpc_client_method"); |
| 39 | + |
| 40 | + public static final TagKey PROJECT_ID = TagKey.create("spanner_project_id"); |
| 41 | + public static final TagKey INSTANCE_ID = TagKey.create("spanner_instance_id"); |
| 42 | + public static final TagKey DATABASE_ID = TagKey.create("spanner_database_id"); |
| 43 | + |
| 44 | + public static final StatsRecorder STATS_RECORDER = Stats.getStatsRecorder(); |
| 45 | + /** GFE t4t7 latency extracted from server-timing header. */ |
| 46 | + public static final MeasureLong SPANNER_GFE_LATENCY = |
| 47 | + MeasureLong.create( |
| 48 | + "cloud.google.com/java/spanner/gfe_latency", |
| 49 | + "Latency between Google's network receiving an RPC and reading back the first byte of the response", |
| 50 | + MILLISECOND); |
| 51 | + /** Number of responses without the server-timing header. */ |
| 52 | + public static final MeasureLong SPANNER_GFE_HEADER_MISSING_COUNT = |
| 53 | + MeasureLong.create( |
| 54 | + "cloud.google.com/java/spanner/gfe_header_missing_count", |
| 55 | + "Number of RPC responses received without the server-timing header, most likely means that the RPC never reached Google's network", |
| 56 | + COUNT); |
| 57 | + |
| 58 | + static final List<Double> RPC_MILLIS_BUCKET_BOUNDARIES = |
| 59 | + Collections.unmodifiableList( |
| 60 | + Arrays.asList( |
| 61 | + 0.0, 0.01, 0.05, 0.1, 0.3, 0.6, 0.8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 13.0, |
| 62 | + 16.0, 20.0, 25.0, 30.0, 40.0, 50.0, 65.0, 80.0, 100.0, 130.0, 160.0, 200.0, 250.0, |
| 63 | + 300.0, 400.0, 500.0, 650.0, 800.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0, 50000.0, |
| 64 | + 100000.0)); |
| 65 | + static final Aggregation AGGREGATION_WITH_MILLIS_HISTOGRAM = |
| 66 | + Distribution.create(BucketBoundaries.create(RPC_MILLIS_BUCKET_BOUNDARIES)); |
| 67 | + static final View SPANNER_GFE_LATENCY_VIEW = |
| 68 | + View.create( |
| 69 | + View.Name.create("cloud.google.com/java/spanner/gfe_latency"), |
| 70 | + "Latency between Google's network receiving an RPC and reading back the first byte of the response", |
| 71 | + SPANNER_GFE_LATENCY, |
| 72 | + AGGREGATION_WITH_MILLIS_HISTOGRAM, |
| 73 | + ImmutableList.of(METHOD, PROJECT_ID, INSTANCE_ID, DATABASE_ID)); |
| 74 | + |
| 75 | + private static final Aggregation SUM = Sum.create(); |
| 76 | + static final View SPANNER_GFE_HEADER_MISSING_COUNT_VIEW = |
| 77 | + View.create( |
| 78 | + View.Name.create("cloud.google.com/java/spanner/gfe_header_missing_count"), |
| 79 | + "Number of RPC responses received without the server-timing header, most likely means that the RPC never reached Google's network", |
| 80 | + SPANNER_GFE_HEADER_MISSING_COUNT, |
| 81 | + SUM, |
| 82 | + ImmutableList.of(METHOD, PROJECT_ID, INSTANCE_ID, DATABASE_ID)); |
| 83 | + |
| 84 | + public static ViewManager viewManager = Stats.getViewManager(); |
| 85 | + |
| 86 | + /** |
| 87 | + * Register views for GFE metrics, including gfe_latency and gfe_header_missing_count. gfe_latency |
| 88 | + * measures the latency between Google's network receives an RPC and reads back the first byte of |
| 89 | + * the response. gfe_header_missing_count is a counter of the number of RPC responses without a |
| 90 | + * server-timing header. |
| 91 | + */ |
| 92 | + @VisibleForTesting |
| 93 | + public static void registerGfeLatencyAndHeaderMissingCountViews() { |
| 94 | + viewManager.registerView(SPANNER_GFE_LATENCY_VIEW); |
| 95 | + viewManager.registerView(SPANNER_GFE_HEADER_MISSING_COUNT_VIEW); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Register GFE Latency view. gfe_latency measures the latency between Google's network receives |
| 100 | + * an RPC and reads back the first byte of the response. |
| 101 | + */ |
| 102 | + @VisibleForTesting |
| 103 | + public static void registerGfeLatencyView() { |
| 104 | + viewManager.registerView(SPANNER_GFE_LATENCY_VIEW); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Register GFE Header Missing Count view. gfe_header_missing_count is a counter of the number of |
| 109 | + * RPC responses without a server-timing header. |
| 110 | + */ |
| 111 | + @VisibleForTesting |
| 112 | + public static void registerGfeHeaderMissingCountView() { |
| 113 | + viewManager.registerView(SPANNER_GFE_HEADER_MISSING_COUNT_VIEW); |
| 114 | + } |
| 115 | +} |
0 commit comments