Skip to content

Commit

Permalink
Merge pull request #107 from smasset/refactor-counters
Browse files Browse the repository at this point in the history
Refactor counters
  • Loading branch information
Lennart Koopmann committed Jan 1, 2013
2 parents c619574 + 43f1b73 commit bb85b75
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 147 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/graylog2/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class Core implements GraylogServer {
public static final String GRAYLOG2_VERSION = "0.10.0-rc.2";

public static final String MASTER_COUNTER_NAME = "master";

public static final String THROUGHPUT_COUNTER_NAME = "throughput";

private int lastReceivedMessageTimestamp = 0;

Expand Down Expand Up @@ -150,6 +152,7 @@ public void initialize(Configuration configuration) {

messageCounterManager = new MessageCounterManagerImpl();
messageCounterManager.register(MASTER_COUNTER_NAME);
messageCounterManager.register(THROUGHPUT_COUNTER_NAME);

hostCounterCache = new HostCounterCacheImpl();

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/graylog2/CounterFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2011 Lennart Koopmann <lennart@socketfeed.com>
*
* This file is part of Graylog2.
*
* Graylog2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.graylog2;

import org.graylog2.plugin.Counter;

public class CounterFactory {

public static final Counter newCounter() {
return new CounterImpl(0);
}

public static final Counter newCounter(int value) {
return new CounterImpl(value);
}

public static final Counter newCounter(double value) {
return new CounterImpl(value);
}

public static final Counter newCounter(Counter value) {
return new CounterImpl(value);
}

}
152 changes: 152 additions & 0 deletions src/main/java/org/graylog2/CounterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* Copyright 2011 Lennart Koopmann <lennart@socketfeed.com>
*
* This file is part of Graylog2.
*
* Graylog2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.graylog2;

import org.graylog2.plugin.Counter;

/**
* A counter object with synchronized writes and utility methods.
*/
public class CounterImpl implements Counter, Comparable<Counter> {

private int count = 0;

public CounterImpl(int count) {
this.set(count);
}

public CounterImpl(double count) {
this.set(count);
}

public CounterImpl(Counter counter) {
this.set(counter);
}

@Override
public int get() {
return this.count;
}

/**
* Increment counter by 1.
*/
@Override
public void increment() {
this.add(1);
}

/**
* Reset counter to 0.
*/
@Override
public void reset() {
this.set(0);
}

/**
* Set counter to provided value.
*/
@Override
public void set(int value) {
this.innerSet(value);
}

/**
* Set counter to floored provided value.
*/
@Override
public void set(double value) {
this.set(Double.valueOf(Math.floor(value)).intValue());
}

/**
* Set counter to provided counter value. If null then do nothing.
*/
@Override
public void set(Counter value) {
if (value != null) {
this.set(value.get());
}
}

/**
* Add provided value to counter.
*/
@Override
public void add(int value) {
this.innerAdd(value);
}

/**
* Add floored provided value to counter.
*/
@Override
public void add(double value) {
this.add(Double.valueOf(Math.floor(value)).intValue());
}

/**
* Add provided counter value to counter. If null then do nothing.
*/
@Override
public void add(Counter value) {
if (value != null) {
this.add(value.get());
}
}

@Override
public String toString() {
return "" + this.count;
}

@Override
public boolean equals(Object comparedObject) {
boolean isEqual = false;

if (comparedObject != null) {
if (comparedObject instanceof Counter) {
isEqual = this.compareTo((Counter) comparedObject) == 0;
}
}

return isEqual;
}

@Override
public int compareTo(Counter comparedCounter) {
int compareResult = this.get();

if (comparedCounter != null) {
compareResult = this.get() - comparedCounter.get();
}

return compareResult;
}

private synchronized void innerAdd(int value) {
this.count += value;
}

private synchronized void innerSet(int value) {
this.count = value;
}
}
15 changes: 9 additions & 6 deletions src/main/java/org/graylog2/GraphiteFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

package org.graylog2;

import org.graylog2.plugin.Tools;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map.Entry;

import org.graylog2.plugin.Counter;
import org.graylog2.plugin.MessageCounter;
import org.graylog2.plugin.Tools;

import com.google.common.collect.Lists;

/**
* @author Lennart Koopmann <lennart@socketfeed.com>
Expand All @@ -49,14 +52,14 @@ public List<String> getAllMetrics() {
r.add(overall);

// Streams.
for(Entry<String, Integer> stream : counter.getStreamCounts().entrySet()) {
String sval = prefix() + "streams." + stream.getKey() + " " + stream.getValue() + " " + now;
for(Entry<String, Counter> stream : counter.getStreamCounts().entrySet()) {
String sval = prefix() + "streams." + stream.getKey() + " " + stream.getValue().get() + " " + now;
r.add(sval);
}

// Hosts.
for(Entry<String, Integer> host : counter.getHostCounts().entrySet()) {
String hval = prefix() + "hosts." + Tools.decodeBase64(host.getKey()).replaceAll("[^a-zA-Z0-9\\.]", "") + " " + host.getValue() + " " + Tools.getUTCTimestamp();
for(Entry<String, Counter> host : counter.getHostCounts().entrySet()) {
String hval = prefix() + "hosts." + Tools.decodeBase64(host.getKey()).replaceAll("[^a-zA-Z0-9\\.]", "") + " " + host.getValue().get() + " " + Tools.getUTCTimestamp();
r.add(hval);
}

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/graylog2/LibratoMetricsFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@

package org.graylog2;

import org.graylog2.plugin.Tools;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.graylog2.plugin.Counter;
import org.graylog2.plugin.MessageCounter;
import org.graylog2.plugin.Tools;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.streams.StreamImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* @author Lennart Koopmann <lennart@socketfeed.com>
Expand Down Expand Up @@ -86,28 +85,28 @@ public String asJson() {
gauges.add(overall);

// Streams.
for(Entry<String, Integer> stream : counter.getStreamCounts().entrySet()) {
for(Entry<String, Counter> stream : counter.getStreamCounts().entrySet()) {
if (streamFilter.contains(stream.getKey())) {
LOG.debug("Not sending stream <{}> to Librato Metrics because it is listed in libratometrics_stream_filter", stream.getKey());
continue;
}

Map<String, Object> s = Maps.newHashMap();
s.put("value", stream.getValue());
s.put("value", stream.getValue().get());
s.put("source", source);
s.put("name", "gl2-stream-" + buildStreamMetricName(stream.getKey()));
gauges.add(s);
}

// Hosts.
for(Entry<String, Integer> host : counter.getHostCounts().entrySet()) {
for(Entry<String, Counter> host : counter.getHostCounts().entrySet()) {
if (Tools.decodeBase64(host.getKey()).matches(hostFilter)) {
LOG.debug("Not sending host <{}> to Librato Metrics because it was matched by libratometrics_host_filter", host.getKey());
continue;
}

Map<String, Object> h = Maps.newHashMap();
h.put("value", host.getValue());
h.put("value", host.getValue().get());
h.put("source", source);
h.put("name", "gl2-host-" + Tools.decodeBase64(host.getKey()).replaceAll("[^a-zA-Z0-9]", ""));
gauges.add(h);
Expand Down
Loading

0 comments on commit bb85b75

Please sign in to comment.