Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd.restapi; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.extensions.registration.PluginName; 20 : import com.google.gerrit.httpd.restapi.RestApiServlet.ViewData; 21 : import com.google.gerrit.metrics.Counter1; 22 : import com.google.gerrit.metrics.Counter3; 23 : import com.google.gerrit.metrics.Description; 24 : import com.google.gerrit.metrics.Description.Units; 25 : import com.google.gerrit.metrics.Field; 26 : import com.google.gerrit.metrics.Histogram1; 27 : import com.google.gerrit.metrics.MetricMaker; 28 : import com.google.gerrit.metrics.Timer1; 29 : import com.google.gerrit.server.logging.Metadata; 30 : import com.google.inject.Inject; 31 : import com.google.inject.Singleton; 32 : 33 : @Singleton 34 : public class RestApiMetrics { 35 99 : private static final String[] PKGS = { 36 : "com.google.gerrit.server.", "com.google.gerrit.", 37 : }; 38 : 39 : final Counter1<String> count; 40 : final Counter3<String, Integer, String> errorCount; 41 : final Timer1<String> serverLatency; 42 : final Histogram1<String> responseBytes; 43 : 44 : @Inject 45 99 : RestApiMetrics(MetricMaker metrics) { 46 99 : Field<String> viewField = 47 99 : Field.ofString("view", Metadata.Builder::className) 48 99 : .description("view implementation class") 49 99 : .build(); 50 99 : count = 51 99 : metrics.newCounter( 52 : "http/server/rest_api/count", 53 99 : new Description("REST API calls by view").setRate(), 54 : viewField); 55 : 56 99 : errorCount = 57 99 : metrics.newCounter( 58 : "http/server/rest_api/error_count", 59 99 : new Description("REST API errors by view").setRate(), 60 : viewField, 61 99 : Field.ofInteger("error_code", Metadata.Builder::httpStatus) 62 99 : .description("HTTP status code") 63 99 : .build(), 64 99 : Field.ofString("cause", Metadata.Builder::cause) 65 99 : .description("The cause of the error.") 66 99 : .build()); 67 : 68 99 : serverLatency = 69 99 : metrics.newTimer( 70 : "http/server/rest_api/server_latency", 71 : new Description("REST API call latency by view") 72 99 : .setCumulative() 73 99 : .setUnit(Units.MILLISECONDS), 74 : viewField); 75 : 76 99 : responseBytes = 77 99 : metrics.newHistogram( 78 : "http/server/rest_api/response_bytes", 79 : new Description("Size of response on network (may be gzip compressed)") 80 99 : .setCumulative() 81 99 : .setUnit(Units.BYTES), 82 : viewField); 83 99 : } 84 : 85 : String view(ViewData viewData) { 86 28 : return view(viewData.view.getClass(), viewData.pluginName); 87 : } 88 : 89 : String view(Class<?> clazz, @Nullable String pluginName) { 90 28 : String impl = clazz.getName().replace('$', '.'); 91 28 : for (String p : PKGS) { 92 28 : if (impl.startsWith(p)) { 93 28 : impl = impl.substring(p.length()); 94 28 : break; 95 : } 96 : } 97 28 : if (!Strings.isNullOrEmpty(pluginName) && !PluginName.GERRIT.equals(pluginName)) { 98 2 : impl = pluginName + '-' + impl; 99 : } 100 28 : return impl; 101 : } 102 : }