1 /* 2 * Zed Attack Proxy (ZAP) and its related class files. 3 * 4 * ZAP is an HTTP/HTTPS proxy for assessing web application security. 5 * 6 * Copyright 2016 The ZAP Development Team 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.zaproxy.zap.extension.stats; 21 22 import java.io.IOException; 23 import java.net.UnknownHostException; 24 import org.zaproxy.zap.utils.StatsListener; 25 26 public class Statsd implements StatsListener { 27 28 private static final String GLOBAL_KEY = "global"; 29 30 private StatsdClient statsd2; 31 32 private String host; 33 private int port; 34 private String prefix; 35 private String separator = "."; 36 Statsd(String host, int port, String prefix)37 public Statsd(String host, int port, String prefix) throws UnknownHostException, IOException { 38 this.host = host; 39 this.port = port; 40 this.prefix = prefix; 41 statsd2 = new StatsdClient(host, port); 42 } 43 getHost()44 public String getHost() { 45 return host; 46 } 47 getPort()48 public int getPort() { 49 return port; 50 } 51 getPrefix()52 public String getPrefix() { 53 return prefix; 54 } 55 setPrefix(String prefix)56 public void setPrefix(String prefix) { 57 this.prefix = prefix; 58 } 59 getSeparator()60 public String getSeparator() { 61 return separator; 62 } 63 setSeparator(String separator)64 public void setSeparator(String separator) { 65 this.separator = separator; 66 } 67 getFullKey(String key)68 private String getFullKey(String key) { 69 return this.getFullKey(null, key); 70 } 71 getFullKey(String site, String key)72 private String getFullKey(String site, String key) { 73 StringBuilder sb = new StringBuilder(); 74 if (prefix != null) { 75 sb.append(prefix); 76 sb.append(separator); 77 } 78 if (site != null) { 79 // Remove http(s):// 80 sb.append(site.substring(site.indexOf(':') + 3)); 81 } else { 82 sb.append(GLOBAL_KEY); 83 } 84 sb.append(separator); 85 sb.append(key); 86 // Colon is a statsd separator 87 return sb.toString().replace(":", "-"); 88 } 89 90 @Override counterInc(String key)91 public void counterInc(String key) { 92 statsd2.increment(getFullKey(key)); 93 } 94 95 @Override counterInc(String site, String key)96 public void counterInc(String site, String key) { 97 statsd2.increment(getFullKey(site, key)); 98 } 99 100 @Override counterInc(String key, long inc)101 public void counterInc(String key, long inc) { 102 statsd2.increment(getFullKey(key), (int) inc); 103 } 104 105 @Override counterInc(String site, String key, long inc)106 public void counterInc(String site, String key, long inc) { 107 statsd2.increment(getFullKey(site, key), (int) inc); 108 } 109 110 @Override counterDec(String key)111 public void counterDec(String key) { 112 statsd2.decrement(getFullKey(key)); 113 } 114 115 @Override counterDec(String site, String key)116 public void counterDec(String site, String key) { 117 statsd2.decrement(getFullKey(site, key)); 118 } 119 120 @Override counterDec(String key, long dec)121 public void counterDec(String key, long dec) { 122 statsd2.decrement(getFullKey(key), (int) dec); 123 } 124 125 @Override counterDec(String site, String key, long dec)126 public void counterDec(String site, String key, long dec) { 127 statsd2.decrement(getFullKey(site, key), (int) dec); 128 } 129 130 @Override highwaterMarkSet(String key, long value)131 public void highwaterMarkSet(String key, long value) { 132 statsd2.gauge(getFullKey(key), value); 133 } 134 135 @Override highwaterMarkSet(String site, String key, long value)136 public void highwaterMarkSet(String site, String key, long value) { 137 statsd2.gauge(getFullKey(site, key), value); 138 } 139 140 @Override lowwaterMarkSet(String key, long value)141 public void lowwaterMarkSet(String key, long value) { 142 statsd2.gauge(getFullKey(key), value); 143 } 144 145 @Override lowwaterMarkSet(String site, String key, long value)146 public void lowwaterMarkSet(String site, String key, long value) { 147 statsd2.gauge(getFullKey(site, key), value); 148 } 149 150 @Override allCleared()151 public void allCleared() { 152 // No supported 153 } 154 155 @Override allCleared(String site)156 public void allCleared(String site) { 157 // No supported 158 } 159 160 @Override cleared(String keyPrefix)161 public void cleared(String keyPrefix) { 162 // No supported 163 } 164 165 @Override cleared(String site, String keyPrefix)166 public void cleared(String site, String keyPrefix) { 167 // No supported 168 } 169 } 170