-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving to metrics and deltas, fixes for Issue #1
- Loading branch information
1 parent
a7de44a
commit 3e5483a
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
public class HttpAgent extends NodeAgent{ | ||
|
||
|
||
|
||
@Override | ||
public void execute(NodeStats nodeStats) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/org/elasticsearch/plugin/newrelic/model/Metric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
src/test/java/org/elasticsearch/plugin/newrelic/MetricTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |