Skip to content

Commit 5cc352c

Browse files
committed
Merge pull request Netflix#351 from dmuino/newApacheOutput
Deal with new apache status output
2 parents 1be9bd7 + a77ebbf commit 5cc352c

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

servo-apache/src/main/java/com/netflix/servo/publish/apache/ApacheStatusPoller.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.netflix.servo.tag.Tag;
2323
import com.netflix.servo.tag.Tags;
2424
import com.netflix.servo.util.UnmodifiableList;
25+
import com.netflix.servo.util.UnmodifiableSet;
2526

2627
import java.io.BufferedReader;
2728
import java.io.IOException;
@@ -33,6 +34,7 @@
3334
import java.util.ArrayList;
3435
import java.util.Collections;
3536
import java.util.List;
37+
import java.util.Set;
3638
import java.util.regex.Matcher;
3739
import java.util.regex.Pattern;
3840

@@ -91,14 +93,17 @@ private StatusPageParser() {
9193
}
9294

9395
private static final Pattern INVALID_CHARS = Pattern.compile("[^a-zA-Z0-9_\\-\\.]");
94-
private static final Pattern STAT_LINE = Pattern.compile("^([^:]+): (\\S+)$");
96+
private static final Pattern STAT_LINE = Pattern.compile("^([^:]+): ([.\\d]+)$");
97+
private static final Pattern SCOREBOARD_LINE = Pattern.compile("^Scoreboard: (\\S+)$");
9598
private static final char[] SCOREBOARD_CHARS = {
9699
'_', 'S', 'R', 'W', 'K', 'D', 'C', 'L', 'G', 'I', '.', '*'};
97100
private static final Tag CLASS_TAG = Tags.newTag("class", "ApacheStatusPoller");
98101
/**
99-
* Metrics that should not be included. These can be confusing for end users.
102+
* Metrics that should be included.
100103
*/
101-
private static final List<String> BLACKLISTED_METRICS = UnmodifiableList.of("CPULoad");
104+
private static final Set<String> WHITELISTED_METRICS = UnmodifiableSet.of("Total_Accesses",
105+
"Total_kBytes", "Uptime", "ReqPerSec", "BytesPerSec", "BytesPerReq",
106+
"BusyWorkers", "IdleWorkers");
102107
private static final int ASCII_CHARS = 128;
103108
private static final String SCOREBOARD = "Scoreboard";
104109

@@ -138,7 +143,7 @@ static List<Metric> parseStatLine(String line, long timestamp) {
138143
}
139144

140145
final String name = INVALID_CHARS.matcher(m.group(1)).replaceAll("_");
141-
if (BLACKLISTED_METRICS.contains(name)) {
146+
if (!WHITELISTED_METRICS.contains(name)) {
142147
return EMPTY_LIST;
143148
}
144149
final double value = Double.parseDouble(m.group(2));
@@ -168,12 +173,12 @@ static List<Metric> parseStatLine(String line, long timestamp) {
168173
* ." Open slot with no current process (ignored)
169174
*/
170175
static List<Metric> parseScoreboardLine(String line, long timestamp) {
171-
final Matcher m = STAT_LINE.matcher(line);
176+
final Matcher m = SCOREBOARD_LINE.matcher(line);
172177
if (!m.matches()) {
173178
return EMPTY_LIST;
174179
}
175180

176-
final char[] scoreboard = m.group(2).toCharArray();
181+
final char[] scoreboard = m.group(1).toCharArray();
177182

178183
final double[] tally = new double[ASCII_CHARS];
179184
for (final char item : SCOREBOARD_CHARS) {

servo-apache/src/test/java/com/netflix/servo/publish/apache/ApacheStatusPollerTest.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.testng.annotations.Test;
2424

2525
import java.io.ByteArrayInputStream;
26-
import java.io.IOException;
27-
import java.io.InputStream;
2826
import java.util.ArrayList;
2927
import java.util.Arrays;
3028
import java.util.List;
@@ -127,4 +125,76 @@ public void testParse() throws Exception {
127125

128126
assertEquals(metrics, expected);
129127
}
128+
129+
@Test
130+
public void testParseNewFormat() throws Exception {
131+
final String statusText = "localhost\n"
132+
+ "ServerVersion: Apache/2.4.16 (Ubuntu)\n"
133+
+ "ServerMPM: worker\n"
134+
+ "Server Built: 2015-09-08T00:00:00\n"
135+
+ "CurrentTime: Tuesday, 08-Sep-2015 21:07:08 UTC\n"
136+
+ "RestartTime: Tuesday, 08-Sep-2015 19:51:38 UTC\n"
137+
+ "ParentServerConfigGeneration: 1\n"
138+
+ "ParentServerMPMGeneration: 0\n"
139+
+ "ServerUptimeSeconds: \n"
140+
+ "ServerUptime: 1 hour 15 minutes 29 seconds\n"
141+
+ "Load1: 0.04\n"
142+
+ "Load5: 0.03\n"
143+
+ "Load15: 0.05\n"
144+
+ "Total Accesses: " + TOTAL_ACCESSES + "\n"
145+
+ "Total kBytes: " + KBYTES + "\n"
146+
+ "CPUUser: .36\n"
147+
+ "CPUSystem: .26\n"
148+
+ "CPUChildrenUser: 0\n"
149+
+ "CPUChildrenSystem: 0\n"
150+
+ "CPULoad: .044688\n"
151+
+ "Uptime: " + UPTIME + "\n"
152+
+ "ReqPerSec: " + RPS + "\n"
153+
+ "BytesPerSec: " + BPS + "\n"
154+
+ "BytesPerReq: " + BPR + "\n"
155+
+ "BusyWorkers: " + BUSY_WORKERS + "\n"
156+
+ "IdleWorkers: " + IDLE_WORKERS + "\n"
157+
+ "Scoreboard: __________________K___W_K_____K______________K____"
158+
+ repeat('.', OPEN_SLOTS)
159+
+ "\n";
160+
ApacheStatusPoller.StatusFetcher fetcher = () -> new ByteArrayInputStream(statusText.getBytes("UTF-8"));
161+
162+
ApacheStatusPoller poller = new ApacheStatusPoller(fetcher);
163+
164+
List<Metric> metrics = poller.pollImpl(TIMESTAMP);
165+
166+
Metric accesses = counter("Total_Accesses", TOTAL_ACCESSES);
167+
Metric kBytes = counter("Total_kBytes", KBYTES);
168+
Metric uptime = counter("Uptime", UPTIME);
169+
170+
List<Metric> counters = UnmodifiableList.of(accesses, kBytes, uptime);
171+
Metric rps = gauge("ReqPerSec", RPS);
172+
Metric bps = gauge("BytesPerSec", BPS);
173+
Metric bpr = gauge("BytesPerReq", BPR);
174+
Metric busyWorkers = gauge("BusyWorkers", BUSY_WORKERS);
175+
Metric idleWorkers = gauge("IdleWorkers", IDLE_WORKERS);
176+
List<Metric> gauges = UnmodifiableList.of(rps, bps, bpr, busyWorkers, idleWorkers);
177+
178+
Metric waitingForConnection = scoreboard("WaitingForConnection", 45.0);
179+
Metric startingUp = scoreboard("StartingUp", 0.0);
180+
Metric readingRequest = scoreboard("ReadingRequest", 0.0);
181+
Metric sendingReply = scoreboard("SendingReply", 1.0);
182+
Metric keepalive = scoreboard("Keepalive", 4.0);
183+
Metric dnsLookup = scoreboard("DnsLookup", 0.0);
184+
Metric closingConnection = scoreboard("ClosingConnection", 0.0);
185+
Metric logging = scoreboard("Logging", 0.0);
186+
Metric gracefullyFinishing = scoreboard("GracefullyFinishing", 0.0);
187+
Metric idleCleanupOfWorker = scoreboard("IdleCleanupOfWorker", 0.0);
188+
Metric unknownState = scoreboard("UnknownState", 0.0);
189+
List<Metric> scoreboard = UnmodifiableList.of(waitingForConnection,
190+
startingUp, readingRequest, sendingReply, keepalive, dnsLookup, closingConnection,
191+
logging, gracefullyFinishing, idleCleanupOfWorker, unknownState);
192+
193+
List<Metric> expected = new ArrayList<>();
194+
expected.addAll(counters);
195+
expected.addAll(gauges);
196+
expected.addAll(scoreboard);
197+
198+
assertEquals(metrics, expected);
199+
}
130200
}

0 commit comments

Comments
 (0)