Skip to content

Commit

Permalink
Use new counter objects in MessageCounters
Browse files Browse the repository at this point in the history
  • Loading branch information
smasset committed Dec 31, 2012
1 parent 05bc0a0 commit 43f1b73
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 83 deletions.
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
53 changes: 27 additions & 26 deletions src/main/java/org/graylog2/MessageCounterImpl.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 org.bson.types.ObjectId;
import java.util.Map;
import com.google.common.collect.Maps;

import org.bson.types.ObjectId;
import org.graylog2.plugin.Counter;
import org.graylog2.plugin.MessageCounter;
import org.graylog2.plugin.Tools;

import com.google.common.collect.Maps;

/**
* Singleton holding the number of received messages for streams,
Expand All @@ -34,19 +37,19 @@
*/
public final class MessageCounterImpl implements MessageCounter {

private int total;
private final Map<String, Integer> streams = Maps.newConcurrentMap();
private final Map<String, Integer> hosts = Maps.newConcurrentMap();
private Counter total = CounterFactory.newCounter();
private final Map<String, Counter> streams = Maps.newConcurrentMap();
private final Map<String, Counter> hosts = Maps.newConcurrentMap();

public int getTotalCount() {
public Counter getTotalCount() {
return this.total;
}

public Map<String, Integer> getStreamCounts() {
public Map<String, Counter> getStreamCounts() {
return this.streams;
}

public Map<String, Integer> getHostCounts() {
public Map<String, Counter> getHostCounts() {
return this.hosts;
}

Expand All @@ -65,7 +68,7 @@ public void resetStreamCounts() {
}

public void resetTotal() {
this.total = 0;
this.total.reset();
}

/**
Expand All @@ -81,7 +84,7 @@ public void incrementTotal() {
* @param x The value to add on top of current total count.
*/
public void countUpTotal(final int x) {
this.total += x;
this.total.add(x);
}

/**
Expand All @@ -100,14 +103,13 @@ public void incrementStream(final ObjectId streamId) {
* @param x The value to add on top of the current stream count.
*/
public synchronized void countUpStream(final ObjectId streamId, final int x) {
if (this.streams.containsKey(streamId.toString())) {
// There already is an entry. Increment.
final int oldCount = this.streams.get(streamId.toString());
this.streams.put(streamId.toString(), oldCount+x); // Overwrites old entry.
} else {
// First entry for this stream.
this.streams.put(streamId.toString(), x);
}
Counter counter = this.streams.get(streamId.toString());
if (counter == null) {
counter = CounterFactory.newCounter();
}

counter.add(x);
this.streams.put(streamId.toString(), counter);
}

/**
Expand All @@ -127,14 +129,13 @@ public void incrementHost(final String hostname) {
*/
public synchronized void countUpHost(String hostname, final int x) {
hostname = Tools.encodeBase64(hostname);
if (this.hosts.containsKey(hostname)) {
// There already is an entry. Increment.
final int oldCount = this.hosts.get(hostname);
this.hosts.put(hostname, oldCount+x); // Overwrites old entry.
} else {
// First entry for this stream.
this.hosts.put(hostname, x);
Counter counter = this.hosts.get(hostname);
if (counter == null) {
counter = CounterFactory.newCounter();
}

counter.add(x);
this.hosts.put(hostname, counter);
}

}
Loading

0 comments on commit 43f1b73

Please sign in to comment.