001/* 002 * (C) Copyright 2013-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Delbosc Benoit 018 */ 019package org.nuxeo.runtime.metrics; 020 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026import java.util.stream.Collectors; 027 028import org.nuxeo.common.xmap.annotation.XNode; 029import org.nuxeo.common.xmap.annotation.XNodeList; 030import org.nuxeo.common.xmap.annotation.XObject; 031import org.nuxeo.runtime.model.Descriptor; 032 033import io.dropwizard.metrics5.Metric; 034import io.dropwizard.metrics5.MetricAttribute; 035import io.dropwizard.metrics5.MetricFilter; 036import io.dropwizard.metrics5.MetricName; 037 038@XObject("configuration") 039public class MetricsConfigurationDescriptor implements Descriptor, MetricFilter { 040 041 protected static final String ALL_METRICS = "ALL"; 042 043 @Override 044 public String getId() { 045 return UNIQUE_DESCRIPTOR_ID; 046 } 047 048 @XNode("@enabled") 049 protected boolean isEnabled = true; 050 051 @XObject(value = "instrument") 052 public static class InstrumentDescriptor implements Descriptor { 053 054 @XNode("@name") 055 protected String name; 056 057 @XNode("@enabled") 058 protected boolean isEnabled = true; 059 060 @Override 061 public String getId() { 062 return name; 063 } 064 065 public boolean isEnabled() { 066 return isEnabled; 067 } 068 } 069 070 @XNodeList(value = "instrument", type = ArrayList.class, componentType = InstrumentDescriptor.class) 071 protected List<InstrumentDescriptor> instruments = new ArrayList<>(); 072 073 @XObject(value = "filter") 074 public static class FilterDescriptor { 075 076 @XNodeList(value = "allow/prefix", type = ArrayList.class, componentType = String.class) 077 protected List<String> allowedPrefix = new ArrayList<>(); 078 079 @XNodeList(value = "deny/prefix", type = ArrayList.class, componentType = String.class) 080 protected List<String> deniedPrefix = new ArrayList<>(); 081 082 @XNodeList(value = "deny/expansion", type = ArrayList.class, componentType = String.class) 083 protected List<String> deniedExpansions = new ArrayList<>(); 084 085 public List<String> getAllowedPrefix() { 086 return Collections.unmodifiableList(allowedPrefix); 087 } 088 089 public List<String> getDeniedPrefix() { 090 return Collections.unmodifiableList(deniedPrefix); 091 } 092 093 public Set<MetricAttribute> getDeniedExpansions() { 094 if (deniedExpansions.isEmpty()) { 095 return Collections.emptySet(); 096 } 097 return deniedExpansions.stream() 098 .map(expansion -> MetricAttribute.valueOf(expansion.toUpperCase().strip())) 099 .collect(Collectors.toSet()); 100 } 101 } 102 103 @XNode(value = "filter") 104 protected FilterDescriptor filter = new FilterDescriptor(); 105 106 public static String expandName(MetricName metric) { 107 if (metric.getTags().isEmpty()) { 108 return metric.getKey(); 109 } 110 String name = metric.getKey(); 111 for (Map.Entry<String, String> entry : metric.getTags().entrySet()) { 112 String key = "." + entry.getKey() + "."; 113 String keyAndValue = key + entry.getValue() + "."; 114 name = name.replace(key, keyAndValue); 115 } 116 return name; 117 } 118 119 @Override 120 public boolean matches(MetricName name, Metric metric) { 121 String expandedName = expandName(name); 122 return filter.allowedPrefix.stream().anyMatch(f -> ALL_METRICS.equals(f) || expandedName.startsWith(f)) 123 || filter.deniedPrefix.stream().noneMatch(f -> ALL_METRICS.equals(f) || expandedName.startsWith(f)); 124 } 125 126 public Set<MetricAttribute> getDeniedExpansions() { 127 return filter.getDeniedExpansions(); 128 } 129 130 public boolean isEnabled() { 131 return isEnabled; 132 } 133 134 public List<InstrumentDescriptor> getInstruments() { 135 return instruments; 136 } 137 138}