(
43 | "aconex.system.startTime", "", "java.lang:type=Runtime", "StartTime", "");
44 | assertEquals(ManagementFactory.getRuntimeMXBean().getStartTime(), f.getObject().get().longValue());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/parfait-core/src/main/java/io/pcp/parfait/MonitoredConstant.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait;
18 |
19 | import static tech.units.indriya.AbstractUnit.ONE;
20 | import javax.measure.Unit;
21 |
22 | /**
23 | * MonitoredConstant provides an implementation of {@link Monitorable} for
24 | * simple values that are rarely (read: never) updated, once initialised.
25 | *
26 | * This class should be used as for values which rarely change, such as the
27 | * number of installed CPUs or the application version number.
28 | *
29 | * A setter exists for those cases where the value is not known until after
30 | * creation (pre-registration of the Monitorable is required, but the value is
31 | * not known at creation time), but this should not be called as a matter of
32 | * course.
33 | */
34 | public class MonitoredConstant extends SettableValue {
35 | public MonitoredConstant(String name, String description, T initialValue) {
36 | this(name, description, MonitorableRegistry.DEFAULT_REGISTRY,
37 | initialValue);
38 | }
39 |
40 | public MonitoredConstant(String name, String description, T initialValue, Unit> unit) {
41 | this(name, description, MonitorableRegistry.DEFAULT_REGISTRY, initialValue, unit);
42 | }
43 |
44 | public MonitoredConstant(String name, String description, MonitorableRegistry registry,
45 | T initialValue) {
46 | this(name, description, registry, initialValue, ONE);
47 | }
48 |
49 | public MonitoredConstant(String name, String description, MonitorableRegistry registry,
50 | T initialValue, Unit> unit) {
51 | super(name, description, registry, initialValue, unit, ValueSemantics.CONSTANT);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/parfait-pcp/src/main/java/io/pcp/parfait/pcp/StringParsingTextSource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait.pcp;
18 |
19 | import java.util.Map;
20 |
21 | import io.pcp.parfait.Monitorable;
22 | import io.pcp.parfait.dxm.MetricName;
23 | import com.google.common.collect.Maps;
24 |
25 | public class StringParsingTextSource implements TextSource {
26 | private final TextSource delegate;
27 |
28 | public StringParsingTextSource(Iterable input, TextSource fallback) {
29 | delegate = new MapTextSource(fallback, parseMap(input));
30 | }
31 |
32 | private Map parseMap(Iterable input) {
33 | Map output = Maps.newHashMap();
34 | int lineNumber = 0;
35 | for (String currentLine : input) {
36 | lineNumber++;
37 | if (!(currentLine.trim().isEmpty() || currentLine.trim().startsWith("#"))) {
38 | String[] elements = currentLine.trim().split("\t");
39 | if (elements.length != 2) {
40 | throw new IllegalArgumentException(
41 | "Error parsing line "
42 | + lineNumber
43 | + " of input; should have two tab-delimited columns in format \\t");
44 | }
45 | String metricName = elements[0];
46 | String text = elements[1];
47 |
48 | output.put(metricName, text);
49 | }
50 | }
51 | return output;
52 | }
53 |
54 | @Override
55 | public String getText(Monitorable> monitorable, MetricName mappedName) {
56 | return delegate.getText(monitorable, mappedName);
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/parfait-dropwizard/src/main/java/io/pcp/parfait/dropwizard/metricadapters/HistogramAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait.dropwizard.metricadapters;
18 |
19 | import static com.codahale.metrics.MetricRegistry.name;
20 | import static tech.units.indriya.AbstractUnit.ONE;
21 |
22 | import javax.measure.Unit;
23 | import java.util.Set;
24 |
25 | import com.codahale.metrics.Histogram;
26 | import io.pcp.parfait.Monitorable;
27 | import io.pcp.parfait.ValueSemantics;
28 | import io.pcp.parfait.dropwizard.MetricAdapter;
29 | import com.google.common.collect.Sets;
30 |
31 | public class HistogramAdapter implements MetricAdapter {
32 |
33 | private final SamplingAdapter samplingAdapter;
34 | private final CountingAdapter countingAdapter;
35 |
36 | public HistogramAdapter(Histogram histogram, String name, String description, Unit> unit) {
37 | this.samplingAdapter = new SamplingAdapter(histogram, name, description, unit);
38 | this.countingAdapter = new CountingAdapter(histogram, name(name, "count"), description + " - Count", ValueSemantics.MONOTONICALLY_INCREASING);
39 | }
40 |
41 | public HistogramAdapter(Histogram histogram, String name, String description) {
42 | this(histogram, name, description, ONE);
43 | }
44 |
45 | @Override
46 | public Set getMonitorables() {
47 | Set monitorables = Sets.newHashSet();
48 | monitorables.addAll(samplingAdapter.getMonitorables());
49 | monitorables.addAll(countingAdapter.getMonitorables());
50 | return monitorables;
51 | }
52 |
53 | @Override
54 | public void updateMonitorables() {
55 | samplingAdapter.updateMonitorables();
56 | countingAdapter.updateMonitorables();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/parfait-core/src/main/java/io/pcp/parfait/MonitoredLongValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait;
18 |
19 | import static tech.units.indriya.AbstractUnit.ONE;
20 |
21 | import java.util.concurrent.atomic.AtomicLong;
22 | import javax.measure.Unit;
23 |
24 | /**
25 | * {@link Monitorable} implementation for a free-running Long value.
26 | */
27 | public class MonitoredLongValue extends MonitoredNumeric implements Counter {
28 | public MonitoredLongValue(String name, String description,
29 | MonitorableRegistry registry, Long initialValue) {
30 | this(name, description, registry, initialValue, ONE);
31 | }
32 |
33 | public MonitoredLongValue(String name, String description,
34 | Long initialValue) {
35 | this(name, description, MonitorableRegistry.DEFAULT_REGISTRY, initialValue, ONE);
36 | }
37 |
38 | public MonitoredLongValue(String name, String description,
39 | MonitorableRegistry registry, Long initialValue, Unit> unit) {
40 | super(name, description, registry, new AtomicLong(initialValue), unit);
41 | }
42 |
43 | /**
44 | * Convenience method to increment atomic numeric types.
45 | */
46 | public void inc() {
47 | inc(1);
48 | }
49 |
50 | @Override
51 | public void inc(int delta) {
52 | value.addAndGet(delta);
53 | notifyMonitors();
54 | }
55 |
56 | /**
57 | * Convenience method to decrement atomic numeric types.
58 | */
59 | public void dec() {
60 | dec(1);
61 | }
62 |
63 | @Override
64 | public void dec(int delta) {
65 | inc(-delta);
66 | }
67 |
68 | @Override
69 | public void inc(long increment) {
70 | value.addAndGet(increment);
71 | notifyMonitors();
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/parfait-core/src/test/java/io/pcp/parfait/MonitoredIntValueTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static tech.units.indriya.AbstractUnit.ONE;
21 |
22 | import java.util.concurrent.atomic.AtomicInteger;
23 | import org.junit.Test;
24 |
25 | public class MonitoredIntValueTest {
26 | @Test
27 | public void newMonitoredIntegerHasCorrectSemantics() {
28 | assertEquals(ValueSemantics.FREE_RUNNING, newValue().getSemantics());
29 | }
30 |
31 | @Test
32 | public void newMonitoredIntegerHasSuppliedValues() {
33 | assertEquals("AAA", newValue().getName());
34 | assertEquals("BBB", newValue().getDescription());
35 | assertEquals(23, newValue().get().intValue());
36 | assertEquals(ONE, newValue().getUnit());
37 | assertEquals(AtomicInteger.class, newValue().getType());
38 | }
39 |
40 | @Test
41 | public void incrementIncreasesValueByOne() {
42 | MonitoredIntValue value = newValue();
43 | value.inc();
44 | assertEquals(24, value.get().intValue());
45 | }
46 |
47 | @Test
48 | public void decrementDecreasesValueByOne() {
49 | MonitoredIntValue value = newValue();
50 | value.dec();
51 | assertEquals(22, value.get().intValue());
52 | }
53 |
54 | @Test
55 | public void setReplacesValue() {
56 | MonitoredIntValue value = newValue();
57 | value.set(new AtomicInteger(17));
58 | assertEquals(17, value.get().intValue());
59 | }
60 |
61 | private MonitoredIntValue newValue() {
62 | return new MonitoredIntValue("AAA", "BBB", new MonitorableRegistry(), 23);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/parfait-core/src/main/java/io/pcp/parfait/MonitoredValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait;
18 |
19 | import static tech.units.indriya.AbstractUnit.ONE;
20 |
21 | import javax.measure.Unit;
22 |
23 | /**
24 | * MonitoredValue provides a convenient implementation of {@link Monitorable}
25 | * for free-running values that are updatable through a single set method call.
26 | *
27 | * A free-running value is a value that increments and decrements at-will over
28 | * time. This is essentially a "point in time" measurement. An example of an
29 | * instantaneous value is the number of active HTTP sessions.
30 | *
31 | * It is recommended that monotonically-increasing counters be implemented using
32 | * the class {@link MonitoredCounter} in preference to this class.
33 | */
34 | public class MonitoredValue extends SettableValue {
35 | public MonitoredValue(String name, String description, T initialValue) {
36 | this(name, description, MonitorableRegistry.DEFAULT_REGISTRY,
37 | initialValue);
38 | }
39 |
40 | public MonitoredValue(String name, String description, T initialValue, Unit> unit) {
41 | this(name, description, MonitorableRegistry.DEFAULT_REGISTRY, initialValue, unit);
42 | }
43 |
44 | public MonitoredValue(String name, String description, MonitorableRegistry registry,
45 | T initialValue) {
46 | this(name, description, registry, initialValue, ONE);
47 | }
48 |
49 | public MonitoredValue(String name, String description, MonitorableRegistry registry,
50 | T initialValue, Unit> unit) {
51 | super(name, description, registry, initialValue, unit, ValueSemantics.FREE_RUNNING);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/parfait-core/src/test/java/io/pcp/parfait/MonitoredLongValueTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009-2017 Aconex
3 | *
4 | * Licensed under the Apache License, Version 2.0
5 | * (the "License"); you may not use this file except in compliance with
6 | * the License. You may obtain a copy of the License at:
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 | * implied. See the License for the specific language governing
14 | * permissions and limitations under the License.
15 | */
16 |
17 | package io.pcp.parfait;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import static tech.units.indriya.AbstractUnit.ONE;
22 |
23 | import java.util.concurrent.atomic.AtomicLong;
24 |
25 | import org.junit.Test;
26 |
27 | public class MonitoredLongValueTest {
28 | @Test
29 | public void newMonitoredLongHasCorrectSemantics() {
30 | assertEquals(ValueSemantics.FREE_RUNNING, newValue().getSemantics());
31 | }
32 |
33 | @Test
34 | public void newMonitoredLongHasSuppliedValues() {
35 | assertEquals("AAA", newValue().getName());
36 | assertEquals("BBB", newValue().getDescription());
37 | assertEquals(23L, newValue().get().longValue());
38 | assertEquals(ONE, newValue().getUnit());
39 | assertEquals(AtomicLong.class, newValue().getType());
40 | }
41 |
42 | @Test
43 | public void incrementIncreasesValueByOne() {
44 | MonitoredLongValue value = newValue();
45 | value.inc();
46 | assertEquals(24L, value.get().longValue());
47 | }
48 |
49 | @Test
50 | public void decrementDecreasesValueByOne() {
51 | MonitoredLongValue value = newValue();
52 | value.dec();
53 | assertEquals(22, value.get().longValue());
54 | }
55 |
56 | @Test
57 | public void setReplacesValue() {
58 | MonitoredLongValue value = newValue();
59 | value.set(new AtomicLong(17));
60 | assertEquals(17L, value.get().longValue());
61 | }
62 |
63 | private MonitoredLongValue newValue() {
64 | return new MonitoredLongValue("AAA", "BBB", new MonitorableRegistry(), 23L);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------