Skip to content

Commit 6f28892

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 5a458ee + 4e9a641 commit 6f28892

File tree

11 files changed

+448
-8
lines changed

11 files changed

+448
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ System.out.println(hawkular.inventory().pinger()); // 'Hello World'
88
```
99
See [unit tests](src/test/java/org/hawkular/client/test) for more examples.
1010

11+
### How to run unit tests?
12+
You have set your hawkular server url in to `HAWKULAR_ENDPOINT` environment variable.(example: `export HAWKULAR_ENDPOINT=http://<hawkular-host>:8080`)
13+
14+
To run `mvn test`
15+
16+
Run with debug log: `mvn test -Dorg.slf4j.simpleLogger.defaultLogLevel=debug`
17+
1118
# Help Wanted
1219
This project is under active development. Pull requests are welcome.

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<resteasy.version>3.0.8.Final</resteasy.version>
5454
<org.jboss.logging.version>3.3.0.Final</org.jboss.logging.version>
5555
<excluded.tests>nothing-to-exclude</excluded.tests>
56+
<excluded.groups>none</excluded.groups>
5657
</properties>
5758

5859
<dependencies>
@@ -174,7 +175,7 @@
174175
<profile>
175176
<id>ci</id>
176177
<properties>
177-
<excluded.tests></excluded.tests>
178+
<excluded.groups>openshift,known-failure</excluded.groups>
178179
</properties>
179180
</profile>
180181
<profile>
@@ -190,8 +191,10 @@
190191
<groupId>org.apache.maven.plugins</groupId>
191192
<artifactId>maven-surefire-plugin</artifactId>
192193
<configuration>
194+
<groups></groups>
195+
<excludedGroups>${excluded.groups}</excludedGroups>
193196
<excludes>
194-
<exclude>${excluded.tests}</exclude>
197+
<excluded.tests>${excluded.tests}</excluded.tests>
195198
</excludes>
196199
</configuration>
197200
</plugin>

src/main/java/org/hawkular/client/metrics/MetricsClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public interface MetricsClient {
3939
*/
4040
boolean createTenant(Tenant tenant);
4141

42+
/**
43+
* Find metric definitions
44+
*/
45+
List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags);
46+
4247
/**
4348
* Create a Gauge metric definition
4449
*/
@@ -59,6 +64,11 @@ public interface MetricsClient {
5964
*/
6065
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId);
6166

67+
/**
68+
* Retrieve Gauge metric data with range
69+
*/
70+
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId, long startTime, long Endtime);
71+
6272
// /**
6373
// * Retrieve most recent numeric metric data. See implementation for default time range.
6474
// */

src/main/java/org/hawkular/client/metrics/MetricsClientImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId) {
9393
return restApi().getGaugeData(tenantId, metricId);
9494
}
9595

96+
@Override
97+
public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId,
98+
long startTime, long endTime) {
99+
logger.debug("getGaugeData(): tenant={}, metricId={}, startTime={}, endTime={}",
100+
tenantId, metricId, startTime, endTime);
101+
return restApi().getGaugeData(tenantId, metricId, startTime, endTime);
102+
}
103+
104+
96105
@Override
97106
public void createAvailabilityMetric(String tenantId,
98107
MetricDefinition metricDefinition) {
@@ -144,6 +153,12 @@ public List<CounterDataPoint> getCounterData(String tenantId, String metricId) {
144153
return restApi().getCounterData(tenantId, metricId);
145154
}
146155

156+
@Override
157+
public List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags) {
158+
logger.debug("metrics(); tenantId={}, type={}, tags={}", tenantId, type, tags);
159+
return restApi().metrics(tenantId, type, tags);
160+
}
161+
147162

148163
// @Override
149164
// public List<NumericData> getNumericMetricData(String tenantId,

src/main/java/org/hawkular/client/metrics/MetricsRestApi.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public interface MetricsRestApi {
4949
@Path ("/tenants")
5050
Response createTenant(TenantParam tenant);
5151

52+
@GET
53+
@Path("metrics")
54+
List<MetricDefinition> metrics(@HeaderParam ("Hawkular-Tenant") String tenantId,
55+
@QueryParam("type") String type,
56+
@QueryParam("tags") String tags);
57+
5258
@POST
5359
@Path ("/gauges")
5460
void createGaugeMetric(@HeaderParam ("Hawkular-Tenant") String tenantId,

src/test/java/org/hawkular/client/test/metrics/CounterMetricTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.testng.Reporter;
2727
import org.testng.annotations.Test;
2828

29-
import com.google.common.collect.Lists;
30-
3129
public class CounterMetricTest extends BaseTest {
3230

3331
private final MetricDefinition expectedDefinition = CounterDataGenerator.genDef();
@@ -62,6 +60,6 @@ public void getData() throws Exception {
6260
List<CounterDataPoint> actual =
6361
client().metrics().getCounterData(expectedDefinition.getTenantId(), expectedDefinition.getId());
6462
Reporter.log("Got: " + actual.toString(), true);
65-
Assert.assertEquals(Lists.reverse(actual), expectedData);
63+
Assert.assertEquals(actual, expectedData);
6664
}
6765
}

src/test/java/org/hawkular/client/test/metrics/GaugeMetricTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import org.testng.Reporter;
2727
import org.testng.annotations.Test;
2828

29-
import com.google.common.collect.Lists;
30-
29+
@Test(groups={"known-failure"})
3130
public class GaugeMetricTest extends BaseTest {
3231

3332
private final MetricDefinition expectedMetric = GaugeDataGenerator.genDef();
@@ -67,7 +66,7 @@ public void addData() {
6766
public void getData() throws Exception {
6867
List<?> actual = client().metrics().getGaugeData(expectedMetric.getTenantId(), expectedMetric.getId());
6968
Reporter.log("Got: " + actual.toString(), true);
70-
Assert.assertEquals(Lists.reverse(actual), expectedData1);
69+
Assert.assertEquals(actual, expectedData1);
7170
}
7271

7372
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2015 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hawkular.client.test.metrics.openshift;
18+
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
26+
import org.apache.commons.lang3.ArrayUtils;
27+
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
28+
import org.hawkular.client.metrics.model.GaugeDataPoint;
29+
import org.testng.Assert;
30+
import org.testng.Reporter;
31+
import org.testng.annotations.Test;
32+
33+
/**
34+
*Find value=0 datapoints
35+
* @author vnguyen
36+
*/
37+
@Test(groups={"openshift"})
38+
public class CollectionRateDetailTest extends OpenshiftBaseTest {
39+
40+
public CollectionRateDetailTest() throws Exception {
41+
super();
42+
}
43+
44+
@Test
45+
public void findZeroValuesTest() throws Exception {
46+
String project = "zproject9";
47+
String container = "stress";
48+
String testID = "namespace: " + project + ", container: " + container;
49+
50+
// for easy identification in the log
51+
String metricID = super.getMetricID(project, container, METRIC_SUFFIX.CPU_USAGE);
52+
53+
// Get a lot of data in the last few days
54+
long now = Instant.now().toEpochMilli();
55+
long start = now - Duration.ofHours(36).toMillis();
56+
long end = start + Duration.ofHours(36).toMillis();
57+
58+
Duration timeBucket = Duration.ofHours(1);
59+
getData(metricID, testID, start, end, timeBucket);
60+
61+
// get data in the last hour
62+
start = now - Duration.ofHours(1).toMillis();
63+
end = now;
64+
timeBucket = Duration.ofMinutes(1);
65+
getData(metricID, testID, start, end, timeBucket);
66+
}
67+
68+
private void getData(String metricID, String testID, long start, long end, Duration timeBucket) {
69+
Reporter.log("Fetching large data set... may take a couple minutes", true);
70+
List<GaugeDataPoint> rawData = client().metrics().getGaugeData(TENANT_ID, metricID, start, end);
71+
72+
Assert.assertNotNull(rawData, testID);
73+
Reporter.log("raw datapoints: " + rawData.size(), true);
74+
75+
List<Long> zeroList = findZeroValues(rawData);
76+
77+
Assert.assertTrue(zeroList == null || zeroList.size() == 0, testID);
78+
79+
Map<Long, Integer> hist = OpenshiftBaseTest.makeHistogram(rawData, timeBucket);
80+
81+
Double[] result = hist.entrySet().stream()
82+
.map(x -> new Double(x.getValue()))
83+
.toArray(size -> new Double[size]);
84+
85+
double[] d = ArrayUtils.toPrimitive(result);
86+
87+
// drop the first and last as they are usually outliers
88+
double samples[] = Arrays.copyOfRange(d,1, d.length-1);
89+
DescriptiveStatistics stats = new DescriptiveStatistics(samples);
90+
91+
Reporter.log(hist.toString(), true);
92+
Reporter.log("size: " + stats.getN(), true);
93+
Reporter.log("min/max: " + stats.getMin() + "/" + stats.getMax(), true);
94+
Reporter.log("mean: " + stats.getMean(), true);
95+
Reporter.log("variance: " + stats.getVariance(), true);
96+
Reporter.log("stddev: " + stats.getStandardDeviation(), true);
97+
}
98+
99+
private List<Long> findZeroValues(List<GaugeDataPoint> rawData) {
100+
List<Long> zeroList = rawData
101+
.stream()
102+
.filter(p -> !(p.getValue() != 0))
103+
.map(p -> p.getTimestamp())
104+
.collect(Collectors.toList());
105+
return zeroList;
106+
}
107+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2015 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hawkular.client.test.metrics.openshift;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
23+
import org.hawkular.client.metrics.model.MetricDefinition;
24+
import org.testng.Assert;
25+
import org.testng.Reporter;
26+
import org.testng.annotations.Test;
27+
28+
import com.google.common.collect.ImmutableList;
29+
30+
/**
31+
* Verify container metric definitions<p>
32+
* Test expects 1-container pod
33+
* @author vnguyen
34+
*
35+
*/
36+
@Test(groups={"openshift"})
37+
public class MetricDefinitionTest extends OpenshiftBaseTest {
38+
39+
private static final List<String> expectedMetricIDs = ImmutableList.of(
40+
"memory/page_faults",
41+
"cpu/usage",
42+
"memory/usage",
43+
"memory/major_page_faults",
44+
"memory/working_set",
45+
"cpu/limit",
46+
"uptime",
47+
"memory/limit");
48+
49+
public MetricDefinitionTest() throws Exception {
50+
super();
51+
}
52+
53+
@Test
54+
public void verifyMetricDefinitionForPod() {
55+
String project = "default";
56+
String container = "hawkular-metrics";
57+
58+
String type = null; // ignore metric type
59+
String tags = "container_name:" + container + ",pod_namespace:" + project;
60+
List<MetricDefinition> defs = client().metrics().findMetricDefinitions(
61+
OpenshiftBaseTest.TENANT_ID,
62+
type,
63+
tags);
64+
Reporter.log(defs.toString(), true);
65+
66+
Assert.assertTrue(defs != null && defs.size() == expectedMetricIDs.size());
67+
68+
// collect all metric IDs
69+
List<String> metricIDs = defs.stream()
70+
.map(x -> x.getId())
71+
.collect(Collectors.toList());
72+
73+
Reporter.log(metricIDs.toString(), true);
74+
75+
// pad expected metric id with container name + pod id
76+
String podId = defs.get(0).getTags().get("pod_id");
77+
String idPrefix = container + "/" + podId + "/";
78+
List<String> expectedIDs = expectedMetricIDs
79+
.stream()
80+
.map(item -> idPrefix + item)
81+
.collect(Collectors.toList());
82+
Collections.sort(metricIDs);
83+
Collections.sort(expectedIDs);
84+
85+
Assert.assertEquals(metricIDs, expectedIDs);
86+
}
87+
88+
@Test
89+
public void verifyTags() {
90+
//TBD
91+
}
92+
}

0 commit comments

Comments
 (0)