Line data Source code
1 : // Copyright (C) 2019 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.server; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.server.config.GerritServerConfig; 19 : import com.google.gerrit.server.logging.RequestId; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Singleton; 22 : import org.eclipse.jgit.lib.Config; 23 : 24 : /** 25 : * Request listener that sets additional logging tags and enables tracing automatically if the 26 : * request matches any tracing configuration in gerrit.config (see description of 27 : * 'tracing.<trace-id>' subsection in config-gerrit.txt). 28 : */ 29 : @Singleton 30 : public class TraceRequestListener implements RequestListener { 31 138 : public static String TAG_REQUEST = "request"; 32 : 33 138 : private static String TAG_PROJECT = "project"; 34 138 : private static String SECTION_TRACING = "tracing"; 35 : 36 : private final ImmutableList<RequestConfig> traceConfigs; 37 : 38 : @Inject 39 138 : TraceRequestListener(@GerritServerConfig Config cfg) { 40 138 : this.traceConfigs = RequestConfig.parseConfigs(cfg, SECTION_TRACING); 41 138 : } 42 : 43 : @Override 44 : public void onRequest(RequestInfo requestInfo) { 45 105 : requestInfo.traceContext().addTag(TAG_REQUEST, requestInfo.formatForLogging()); 46 105 : requestInfo.project().ifPresent(p -> requestInfo.traceContext().addTag(TAG_PROJECT, p)); 47 105 : traceConfigs.stream() 48 105 : .filter(traceConfig -> traceConfig.matches(requestInfo)) 49 105 : .forEach( 50 : traceConfig -> 51 1 : requestInfo 52 1 : .traceContext() 53 1 : .forceLogging() 54 1 : .addTag(RequestId.Type.TRACE_ID, traceConfig.id())); 55 105 : } 56 : }