Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jkandasa committed Oct 8, 2015
2 parents 5a458ee + 4e9a641 commit 6f28892
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ System.out.println(hawkular.inventory().pinger()); // 'Hello World'
```
See [unit tests](src/test/java/org/hawkular/client/test) for more examples.

### How to run unit tests?
You have set your hawkular server url in to `HAWKULAR_ENDPOINT` environment variable.(example: `export HAWKULAR_ENDPOINT=http://<hawkular-host>:8080`)

To run `mvn test`

Run with debug log: `mvn test -Dorg.slf4j.simpleLogger.defaultLogLevel=debug`

# Help Wanted
This project is under active development. Pull requests are welcome.
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<resteasy.version>3.0.8.Final</resteasy.version>
<org.jboss.logging.version>3.3.0.Final</org.jboss.logging.version>
<excluded.tests>nothing-to-exclude</excluded.tests>
<excluded.groups>none</excluded.groups>
</properties>

<dependencies>
Expand Down Expand Up @@ -174,7 +175,7 @@
<profile>
<id>ci</id>
<properties>
<excluded.tests></excluded.tests>
<excluded.groups>openshift,known-failure</excluded.groups>
</properties>
</profile>
<profile>
Expand All @@ -190,8 +191,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups></groups>
<excludedGroups>${excluded.groups}</excludedGroups>
<excludes>
<exclude>${excluded.tests}</exclude>
<excluded.tests>${excluded.tests}</excluded.tests>
</excludes>
</configuration>
</plugin>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/hawkular/client/metrics/MetricsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public interface MetricsClient {
*/
boolean createTenant(Tenant tenant);

/**
* Find metric definitions
*/
List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags);

/**
* Create a Gauge metric definition
*/
Expand All @@ -59,6 +64,11 @@ public interface MetricsClient {
*/
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId);

/**
* Retrieve Gauge metric data with range
*/
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId, long startTime, long Endtime);

// /**
// * Retrieve most recent numeric metric data. See implementation for default time range.
// */
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/hawkular/client/metrics/MetricsClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId) {
return restApi().getGaugeData(tenantId, metricId);
}

@Override
public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId,
long startTime, long endTime) {
logger.debug("getGaugeData(): tenant={}, metricId={}, startTime={}, endTime={}",
tenantId, metricId, startTime, endTime);
return restApi().getGaugeData(tenantId, metricId, startTime, endTime);
}


@Override
public void createAvailabilityMetric(String tenantId,
MetricDefinition metricDefinition) {
Expand Down Expand Up @@ -144,6 +153,12 @@ public List<CounterDataPoint> getCounterData(String tenantId, String metricId) {
return restApi().getCounterData(tenantId, metricId);
}

@Override
public List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags) {
logger.debug("metrics(); tenantId={}, type={}, tags={}", tenantId, type, tags);
return restApi().metrics(tenantId, type, tags);
}


// @Override
// public List<NumericData> getNumericMetricData(String tenantId,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/hawkular/client/metrics/MetricsRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public interface MetricsRestApi {
@Path ("/tenants")
Response createTenant(TenantParam tenant);

@GET
@Path("metrics")
List<MetricDefinition> metrics(@HeaderParam ("Hawkular-Tenant") String tenantId,
@QueryParam("type") String type,
@QueryParam("tags") String tags);

@POST
@Path ("/gauges")
void createGaugeMetric(@HeaderParam ("Hawkular-Tenant") String tenantId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.testng.Reporter;
import org.testng.annotations.Test;

import com.google.common.collect.Lists;

public class CounterMetricTest extends BaseTest {

private final MetricDefinition expectedDefinition = CounterDataGenerator.genDef();
Expand Down Expand Up @@ -62,6 +60,6 @@ public void getData() throws Exception {
List<CounterDataPoint> actual =
client().metrics().getCounterData(expectedDefinition.getTenantId(), expectedDefinition.getId());
Reporter.log("Got: " + actual.toString(), true);
Assert.assertEquals(Lists.reverse(actual), expectedData);
Assert.assertEquals(actual, expectedData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import org.testng.Reporter;
import org.testng.annotations.Test;

import com.google.common.collect.Lists;

@Test(groups={"known-failure"})
public class GaugeMetricTest extends BaseTest {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.client.test.metrics.openshift;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.hawkular.client.metrics.model.GaugeDataPoint;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

/**
*Find value=0 datapoints
* @author vnguyen
*/
@Test(groups={"openshift"})
public class CollectionRateDetailTest extends OpenshiftBaseTest {

public CollectionRateDetailTest() throws Exception {
super();
}

@Test
public void findZeroValuesTest() throws Exception {
String project = "zproject9";
String container = "stress";
String testID = "namespace: " + project + ", container: " + container;

// for easy identification in the log
String metricID = super.getMetricID(project, container, METRIC_SUFFIX.CPU_USAGE);

// Get a lot of data in the last few days
long now = Instant.now().toEpochMilli();
long start = now - Duration.ofHours(36).toMillis();
long end = start + Duration.ofHours(36).toMillis();

Duration timeBucket = Duration.ofHours(1);
getData(metricID, testID, start, end, timeBucket);

// get data in the last hour
start = now - Duration.ofHours(1).toMillis();
end = now;
timeBucket = Duration.ofMinutes(1);
getData(metricID, testID, start, end, timeBucket);
}

private void getData(String metricID, String testID, long start, long end, Duration timeBucket) {
Reporter.log("Fetching large data set... may take a couple minutes", true);
List<GaugeDataPoint> rawData = client().metrics().getGaugeData(TENANT_ID, metricID, start, end);

Assert.assertNotNull(rawData, testID);
Reporter.log("raw datapoints: " + rawData.size(), true);

List<Long> zeroList = findZeroValues(rawData);

Assert.assertTrue(zeroList == null || zeroList.size() == 0, testID);

Map<Long, Integer> hist = OpenshiftBaseTest.makeHistogram(rawData, timeBucket);

Double[] result = hist.entrySet().stream()
.map(x -> new Double(x.getValue()))
.toArray(size -> new Double[size]);

double[] d = ArrayUtils.toPrimitive(result);

// drop the first and last as they are usually outliers
double samples[] = Arrays.copyOfRange(d,1, d.length-1);
DescriptiveStatistics stats = new DescriptiveStatistics(samples);

Reporter.log(hist.toString(), true);
Reporter.log("size: " + stats.getN(), true);
Reporter.log("min/max: " + stats.getMin() + "/" + stats.getMax(), true);
Reporter.log("mean: " + stats.getMean(), true);
Reporter.log("variance: " + stats.getVariance(), true);
Reporter.log("stddev: " + stats.getStandardDeviation(), true);
}

private List<Long> findZeroValues(List<GaugeDataPoint> rawData) {
List<Long> zeroList = rawData
.stream()
.filter(p -> !(p.getValue() != 0))
.map(p -> p.getTimestamp())
.collect(Collectors.toList());
return zeroList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.client.test.metrics.openshift;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.hawkular.client.metrics.model.MetricDefinition;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableList;

/**
* Verify container metric definitions<p>
* Test expects 1-container pod
* @author vnguyen
*
*/
@Test(groups={"openshift"})
public class MetricDefinitionTest extends OpenshiftBaseTest {

private static final List<String> expectedMetricIDs = ImmutableList.of(
"memory/page_faults",
"cpu/usage",
"memory/usage",
"memory/major_page_faults",
"memory/working_set",
"cpu/limit",
"uptime",
"memory/limit");

public MetricDefinitionTest() throws Exception {
super();
}

@Test
public void verifyMetricDefinitionForPod() {
String project = "default";
String container = "hawkular-metrics";

String type = null; // ignore metric type
String tags = "container_name:" + container + ",pod_namespace:" + project;
List<MetricDefinition> defs = client().metrics().findMetricDefinitions(
OpenshiftBaseTest.TENANT_ID,
type,
tags);
Reporter.log(defs.toString(), true);

Assert.assertTrue(defs != null && defs.size() == expectedMetricIDs.size());

// collect all metric IDs
List<String> metricIDs = defs.stream()
.map(x -> x.getId())
.collect(Collectors.toList());

Reporter.log(metricIDs.toString(), true);

// pad expected metric id with container name + pod id
String podId = defs.get(0).getTags().get("pod_id");
String idPrefix = container + "/" + podId + "/";
List<String> expectedIDs = expectedMetricIDs
.stream()
.map(item -> idPrefix + item)
.collect(Collectors.toList());
Collections.sort(metricIDs);
Collections.sort(expectedIDs);

Assert.assertEquals(metricIDs, expectedIDs);
}

@Test
public void verifyTags() {
//TBD
}
}
Loading

0 comments on commit 6f28892

Please sign in to comment.