PrefixAddingPathMapping(String pathPrefix, PathMapping mapping) { requireNonNull(mapping, "mapping"); // mapping should be GlobPathMapping or RegexPathMapping assert mapping.regex().isPresent() : "unexpected mapping type: " + mapping.getClass().getName(); this.pathPrefix = requireNonNull(pathPrefix, "pathPrefix"); this.mapping = mapping; loggerName = newLoggerName(pathPrefix) + '.' + mapping.loggerName(); meterTag = PREFIX + pathPrefix + ',' + mapping.meterTag(); }
/** * Creates a new instance. */ HttpHeaderPathMapping(PathMapping pathStringMapping, Set<HttpMethod> supportedMethods, List<MediaType> consumeTypes, List<MediaType> produceTypes) { this.pathStringMapping = requireNonNull(pathStringMapping, "pathStringMapping"); this.supportedMethods = requireNonNull(supportedMethods, "supportedMethods"); this.consumeTypes = requireNonNull(consumeTypes, "consumeTypes"); this.produceTypes = requireNonNull(produceTypes, "produceTypes"); loggerName = generateLoggerName(pathStringMapping.loggerName(), supportedMethods, consumeTypes, produceTypes); meterTag = generateMeterTag(pathStringMapping.meterTag(), supportedMethods, consumeTypes, produceTypes); // Starts with 1 due to the HTTP method mapping. int complexity = 1; if (!consumeTypes.isEmpty()) { complexity += 2; } if (!produceTypes.isEmpty()) { complexity += 4; } this.complexity = complexity; }
/** * Returns a {@link Router} implementation which is using one of {@link RoutingTrie} and {@link List}. */ private static <V> Router<V> router(boolean isTrie, List<V> values, Function<V, PathMapping> pathMappingResolver) { final Comparator<V> valueComparator = Comparator.comparingInt(e -> -1 * pathMappingResolver.apply(e).complexity()); final Router<V> router; if (isTrie) { final RoutingTrie.Builder<V> builder = new Builder<>(); // Set a comparator to sort services by the number of conditions to be checked in a descending // order. builder.comparator(valueComparator); values.forEach(v -> builder.add(pathMappingResolver.apply(v).triePath().get(), v)); router = new TrieRouter<>(builder.build(), pathMappingResolver); } else { values.sort(valueComparator); router = new SequentialRouter<>(values, pathMappingResolver); } if (logger.isDebugEnabled()) { logger.debug("Router created for {} service(s): {}", values.size(), router.getClass().getSimpleName()); values.forEach(c -> { final PathMapping mapping = pathMappingResolver.apply(c); logger.debug("meterTag: {}, complexity: {}", mapping.meterTag(), mapping.complexity()); }); } values.clear(); return router; }
private List<Tag> buildTags(RequestLog log) { final RequestContext ctx = log.context(); final Object requestContent = log.requestContent(); String methodName = null; if (requestContent instanceof RpcRequest) { methodName = ((RpcRequest) requestContent).method(); } if (methodName == null) { final HttpHeaders requestHeaders = log.requestHeaders(); final HttpMethod httpMethod = requestHeaders.method(); if (httpMethod != null) { methodName = httpMethod.name(); } } if (methodName == null) { methodName = MoreObjects.firstNonNull(log.method().name(), "__UNKNOWN_METHOD__"); } final List<Tag> tags = new ArrayList<>(4); // method, hostNamePattern, pathMapping, status tags.add(Tag.of("method", methodName)); if (ctx instanceof ServiceRequestContext) { final ServiceRequestContext sCtx = (ServiceRequestContext) ctx; tags.add(Tag.of("hostnamePattern", sCtx.virtualHost().hostnamePattern())); tags.add(Tag.of("pathMapping", sCtx.pathMapping().meterTag())); } return tags; } };
@Override public void serviceAdded(ServiceConfig cfg) throws Exception { checkState(server == null, "cannot be added to more than one server"); server = cfg.server(); router = Routers.ofCompositeService(services); final MeterRegistry registry = server.meterRegistry(); final MeterIdPrefix meterIdPrefix = new MeterIdPrefix("armeria.server.router.compositeServiceCache", "hostnamePattern", cfg.virtualHost().hostnamePattern(), "pathMapping", cfg.pathMapping().meterTag()); router.registerMetrics(registry, meterIdPrefix); for (CompositeServiceEntry<I, O> e : services()) { ServiceCallbackInvoker.invokeServiceAdded(cfg, e.service()); } }
@Override public void serviceAdded(ServiceConfig cfg) throws Exception { final MeterRegistry registry = cfg.server().meterRegistry(); if (cache != null) { CaffeineMetricSupport.setup( registry, new MeterIdPrefix("armeria.server.file.vfsCache", "hostnamePattern", cfg.virtualHost().hostnamePattern(), "pathMapping", cfg.pathMapping().meterTag(), "vfs", config.vfs().meterTag()), cache); } }