Skip to content

Commit

Permalink
Moving to metrics and deltas, fixes for Issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusccarvalho committed Mar 26, 2013
1 parent a7de44a commit 3e5483a
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

public class HttpAgent extends NodeAgent{



@Override
public void execute(NodeStats nodeStats) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
*/
package org.elasticsearch.plugin.newrelic.agents;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.plugin.newrelic.collector.MetricCollector;
import org.elasticsearch.plugin.newrelic.collector.NewRelicCollector;
import org.elasticsearch.plugin.newrelic.model.Metric;

public abstract class NodeAgent {

Expand All @@ -34,6 +37,8 @@ public abstract class NodeAgent {

protected AtomicBoolean enabled = new AtomicBoolean(true);

protected Map<String, Metric> metrics = new HashMap<String, Metric>();

public void setCollector(MetricCollector collector) {
this.collector = collector;
}
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/org/elasticsearch/plugin/newrelic/model/Metric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to ElasticSearch under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.plugin.newrelic.model;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* Not complete thread safe, but should suffice for the needs of the plugin.
*
*/
public class Metric {

private Double current;
private Double last;
private final boolean delta;
private Double min;
private Double max;
private Lock lock = new ReentrantLock();

public Metric(){
this(0.0);
}

public Metric (Double current){
this(current,false);
}

public Metric(Double current, boolean delta) {
this.current = current;
this.delta = delta;
this.min = current;
this.max = current;
}

public void refresh(Double value){
try {
lock.lock();
last = current;
current = value;
if(value < min)
min = value;
if(value > max)
max = value;
} catch (Exception e) {
// TODO: handle exception
}finally {
lock.unlock();
}
}

public Double getValue(){
Double value = null;
try {
lock.lock();
if(delta){
value = (last == null) ? null : current-last;
}else{
value = current;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
lock.unlock();
}
return value;
}

public Map<String, Object> getValues(){
Map<String,Object> values = new HashMap<String, Object>();
try {
lock.lock();
values.put("current",current);
values.put("last", last);
values.put("min", min);
values.put("max", max);
values.put("delta", delta);
} catch (Exception e) {
// TODO: handle exception
}finally{
lock.unlock();
}
return values;
}

}
55 changes: 55 additions & 0 deletions src/test/java/org/elasticsearch/plugin/newrelic/MetricTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to ElasticSearch under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.plugin.newrelic;

import junit.framework.Assert;

import org.elasticsearch.plugin.newrelic.model.Metric;
import org.junit.Test;

public class MetricTest {

@Test
public void simpleMetric(){
Metric m = new Metric();
m.refresh(1.0);
Assert.assertEquals(1.0, m.getValue());
m.refresh(2.0);
Assert.assertEquals(2.0, m.getValue());
}

@Test
public void deltaMetric(){
Metric m = new Metric(1.0,true);
Assert.assertEquals(null, m.getValue());
m.refresh(3.0);
Assert.assertEquals(2.0, m.getValue());
}

@Test
public void deltaMetricValues(){
Metric m = new Metric(1.0,true);
Assert.assertEquals(null, m.getValue());
m.refresh(3.0);
Assert.assertEquals(2.0, m.getValue());

Assert.assertEquals(1.0, m.getValues().get("min"));
Assert.assertEquals(3.0, m.getValues().get("max"));
}
}

0 comments on commit 3e5483a

Please sign in to comment.