|
| 1 | +package com.google.maps.metrics; |
| 2 | + |
| 3 | +import io.opencensus.stats.Aggregation; |
| 4 | +import io.opencensus.stats.Aggregation.Count; |
| 5 | +import io.opencensus.stats.Aggregation.Distribution; |
| 6 | +import io.opencensus.stats.BucketBoundaries; |
| 7 | +import io.opencensus.stats.Measure.MeasureLong; |
| 8 | +import io.opencensus.stats.Stats; |
| 9 | +import io.opencensus.stats.View; |
| 10 | +import io.opencensus.stats.ViewManager; |
| 11 | +import io.opencensus.tags.TagKey; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/* |
| 17 | + * OpenCensus metrics which are measured for every request. |
| 18 | + */ |
| 19 | +public final class OpenCensusMetrics { |
| 20 | + private OpenCensusMetrics() {} |
| 21 | + |
| 22 | + public static final class Tags { |
| 23 | + private Tags() {} |
| 24 | + |
| 25 | + public static final TagKey REQUEST_NAME = TagKey.create("request_name"); |
| 26 | + public static final TagKey HTTP_CODE = TagKey.create("http_code"); |
| 27 | + public static final TagKey API_STATUS = TagKey.create("api_status"); |
| 28 | + } |
| 29 | + |
| 30 | + public static final class Measures { |
| 31 | + private Measures() {} |
| 32 | + |
| 33 | + public static final MeasureLong LATENCY = |
| 34 | + MeasureLong.create( |
| 35 | + "maps.googleapis.com/measure/client/latency", |
| 36 | + "Total time between library method called and results returned", |
| 37 | + "ms"); |
| 38 | + |
| 39 | + public static final MeasureLong NETWORK_LATENCY = |
| 40 | + MeasureLong.create( |
| 41 | + "maps.googleapis.com/measure/client/network_latency", |
| 42 | + "Network time inside the library", |
| 43 | + "ms"); |
| 44 | + |
| 45 | + public static final MeasureLong RETRY_COUNT = |
| 46 | + MeasureLong.create( |
| 47 | + "maps.googleapis.com/measure/client/retry_count", |
| 48 | + "How many times any request was retried", |
| 49 | + "1"); |
| 50 | + } |
| 51 | + |
| 52 | + private static final class Aggregations { |
| 53 | + private Aggregations() {} |
| 54 | + |
| 55 | + private static final Aggregation COUNT = Count.create(); |
| 56 | + |
| 57 | + private static final Aggregation DISTRIBUTION_INTEGERS_10 = |
| 58 | + Distribution.create( |
| 59 | + BucketBoundaries.create( |
| 60 | + Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0))); |
| 61 | + |
| 62 | + // every bucket is ~25% bigger = 20 * 2^(N/3) |
| 63 | + private static final Aggregation DISTRIBUTION_LATENCY = |
| 64 | + Distribution.create( |
| 65 | + BucketBoundaries.create( |
| 66 | + Arrays.asList( |
| 67 | + 0.0, 20.0, 25.2, 31.7, 40.0, 50.4, 63.5, 80.0, 100.8, 127.0, 160.0, 201.6, |
| 68 | + 254.0, 320.0, 403.2, 508.0, 640.0, 806.3, 1015.9, 1280.0, 1612.7, 2031.9, |
| 69 | + 2560.0, 3225.4, 4063.7))); |
| 70 | + } |
| 71 | + |
| 72 | + public static final class Views { |
| 73 | + private Views() {} |
| 74 | + |
| 75 | + private static final List<TagKey> fields = |
| 76 | + tags(Tags.REQUEST_NAME, Tags.HTTP_CODE, Tags.API_STATUS); |
| 77 | + |
| 78 | + public static final View REQUEST_COUNT = |
| 79 | + View.create( |
| 80 | + View.Name.create("maps.googleapis.com/client/request_count"), |
| 81 | + "Request counts", |
| 82 | + Measures.LATENCY, |
| 83 | + Aggregations.COUNT, |
| 84 | + fields); |
| 85 | + |
| 86 | + public static final View REQUEST_LATENCY = |
| 87 | + View.create( |
| 88 | + View.Name.create("maps.googleapis.com/client/request_latency"), |
| 89 | + "Latency in msecs", |
| 90 | + Measures.LATENCY, |
| 91 | + Aggregations.DISTRIBUTION_LATENCY, |
| 92 | + fields); |
| 93 | + |
| 94 | + public static final View NETWORK_LATENCY = |
| 95 | + View.create( |
| 96 | + View.Name.create("maps.googleapis.com/client/network_latency"), |
| 97 | + "Network latency in msecs (internal)", |
| 98 | + Measures.NETWORK_LATENCY, |
| 99 | + Aggregations.DISTRIBUTION_LATENCY, |
| 100 | + fields); |
| 101 | + |
| 102 | + public static final View RETRY_COUNT = |
| 103 | + View.create( |
| 104 | + View.Name.create("maps.googleapis.com/client/retry_count"), |
| 105 | + "Retries per request", |
| 106 | + Measures.RETRY_COUNT, |
| 107 | + Aggregations.DISTRIBUTION_INTEGERS_10, |
| 108 | + fields); |
| 109 | + } |
| 110 | + |
| 111 | + public static void registerAllViews() { |
| 112 | + registerAllViews(Stats.getViewManager()); |
| 113 | + } |
| 114 | + |
| 115 | + public static void registerAllViews(ViewManager viewManager) { |
| 116 | + View[] views_to_register = |
| 117 | + new View[] { |
| 118 | + Views.REQUEST_COUNT, Views.REQUEST_LATENCY, Views.NETWORK_LATENCY, Views.RETRY_COUNT |
| 119 | + }; |
| 120 | + for (View view : views_to_register) { |
| 121 | + viewManager.registerView(view); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static List<TagKey> tags(TagKey... items) { |
| 126 | + return Collections.unmodifiableList(Arrays.asList(items)); |
| 127 | + } |
| 128 | +} |
0 commit comments