Skip to content

Commit

Permalink
Merge pull request #744 from jotak/merge-dropwizard
Browse files Browse the repository at this point in the history
[HWKMETRICS-585] Merge dropwizard reporter in metrics
  • Loading branch information
jsanda committed Mar 2, 2017
2 parents e0c542f + 4e34cae commit 3191005
Show file tree
Hide file tree
Showing 25 changed files with 3,065 additions and 0 deletions.
13 changes: 13 additions & 0 deletions clients/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
<artifactId>hawkular-metrics-clients-common</artifactId>

<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2014-2017 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.metrics.client.common;

import java.util.Map;

/**
* @author Joel Takvorian
*/
public interface HawkularClientConfig {
String getTenant();
String getUri();
String getBearerToken();
String getPrefix();
Map<String, String> getHeaders();
Map<String, String> getGlobalTags();
Map<String, Map<String, String>> getPerMetricTags();
String getUsername();
String getPassword();
Long getFailoverCacheDuration();
Integer getFailoverCacheMaxSize();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2014-2017 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.metrics.client.common.http;

import java.util.Map;
import java.util.Optional;

/**
* Http client interface for Hawkular, in case someone would like to use other than the default one
* @author Joel Takvorian
*/
public interface HawkularHttpClient {
void addHeaders(Map<String, String> headers);
HawkularHttpResponse postMetrics(String jsonBody);
HawkularHttpResponse putTags(String type, String metricName, String jsonBody);
void setFailoverOptions(Optional<Long> failoverCacheDuration, Optional<Integer> failoverCacheMaxSize);
void manageFailover();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2014-2017 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.metrics.client.common.http;

/**
* Minimalist and API-agnostic response wrapper
* @author Joel Takvorian
*/
public class HawkularHttpResponse {

private final String content;
private final int responseCode;
private final String errorMsg;

public HawkularHttpResponse(String content, int responseCode) {
this.content = content;
this.responseCode = responseCode;
this.errorMsg = null;
}

public HawkularHttpResponse(String content, int responseCode, String errorMsg) {
this.content = content;
this.responseCode = responseCode;
this.errorMsg = errorMsg;
}

public int getResponseCode() {
return responseCode;
}

public String getErrorMsg() {
return errorMsg;
}

public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2014-2017 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.metrics.client.common.http;

import java.util.Map;
import java.util.function.BiFunction;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

/**
* Some Json utility for Hawkular data model
* @author Joel Takvorian
*/
public final class HawkularJson {

private HawkularJson() {
}

public static String metricsToString(Long timestamp,
Map<String, Long> counters,
Map<String, Double> gauges) {
JsonObjectBuilder builder = Json.createObjectBuilder();
if (!counters.isEmpty()) {
builder.add("counters", metricsJson(timestamp, counters, HawkularJson::longDataPoint));
}
if (!gauges.isEmpty()) {
builder.add("gauges", metricsJson(timestamp, gauges, HawkularJson::doubleDataPoint));
}
return builder.build().toString();
}

public static String tagsToString(Map<String, String> tags) {
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
tags.forEach(jsonObjectBuilder::add);
return jsonObjectBuilder.build().toString();
}

private static <T> JsonArray metricsJson(Long timestamp,
Map<String, T> metricsPoints,
BiFunction<Long, T, JsonObject> bf) {
final JsonArrayBuilder builder = Json.createArrayBuilder();
metricsPoints.entrySet().stream()
.map(e -> metricJson(e.getKey(), bf.apply(timestamp, e.getValue())))
.forEach(builder::add);
return builder.build();
}

private static JsonObject metricJson(String name, JsonObject dataPoint) {
return Json.createObjectBuilder()
.add("id", name)
.add("dataPoints", Json.createArrayBuilder().add(dataPoint).build())
.build();
}

private static JsonObject doubleDataPoint(long timestamp, double value) {
return Json.createObjectBuilder()
.add("timestamp", timestamp)
.add("value", value)
.build();
}

private static JsonObject longDataPoint(long timestamp, long value) {
return Json.createObjectBuilder()
.add("timestamp", timestamp)
.add("value", value)
.build();
}
}

0 comments on commit 3191005

Please sign in to comment.