com.metamx.emitter.service.ServiceMetricEvent Maven / Gradle / Ivy
/*
* Copyright 2012 Metamarkets Group Inc.
*
* 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 com.metamx.emitter.service;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.metamx.common.ISE;
import org.joda.time.DateTime;
import java.util.Arrays;
import java.util.Map;
/**
*/
public class ServiceMetricEvent implements ServiceEvent
{
public static Builder builder()
{
return new Builder();
}
private final DateTime createdTime;
private final ImmutableMap serviceDims;
private final Map userDims;
private final String feed;
private final String metric;
private final Number value;
private ServiceMetricEvent(
DateTime createdTime,
ImmutableMap serviceDims,
Map userDims,
String feed,
String metric,
Number value
)
{
this.createdTime = createdTime != null ? createdTime : new DateTime();
this.serviceDims = serviceDims;
this.userDims = userDims;
this.feed = feed;
this.metric = metric;
this.value = value;
}
public DateTime getCreatedTime()
{
return createdTime;
}
public String getFeed()
{
return feed;
}
public String getService()
{
return serviceDims.get("service");
}
public String getHost()
{
return serviceDims.get("host");
}
public Map getUserDims()
{
return ImmutableMap.copyOf(userDims);
}
public String getMetric()
{
return metric;
}
public Number getValue()
{
return value;
}
public boolean isSafeToBuffer()
{
return true;
}
@Override
@JsonValue
public Map toMap()
{
return ImmutableMap.builder()
.put("feed", getFeed())
.put("timestamp", createdTime.toString())
.putAll(serviceDims)
.put("metric", metric)
.put("value", value)
.putAll(
Maps.filterEntries(
userDims,
new Predicate>()
{
@Override
public boolean apply(Map.Entry input)
{
return input.getKey() != null;
}
}
)
)
.build();
}
public static class Builder
{
private final Map userDims = Maps.newTreeMap();
private String feed = "metrics";
public Builder setFeed(String feed)
{
this.feed = feed;
return this;
}
public Builder setDimension(String dim, String[] values)
{
userDims.put(dim, Arrays.asList(values));
return this;
}
public Builder setDimension(String dim, String value)
{
userDims.put(dim, value);
return this;
}
public Object getDimension(String dim)
{
return userDims.get(dim);
}
public ServiceEventBuilder build(
final String metric,
final Number value
)
{
return build(null, metric, value);
}
public ServiceEventBuilder build(
final DateTime createdTime,
final String metric,
final Number value
)
{
if (Double.isNaN(value.doubleValue())) {
throw new ISE("Value of NaN is not allowed!");
}
if (Double.isInfinite(value.doubleValue())) {
throw new ISE("Value of Infinite is not allowed!");
}
return new ServiceEventBuilder()
{
@Override
public ServiceMetricEvent build(ImmutableMap serviceDimensions)
{
return new ServiceMetricEvent(
createdTime,
serviceDimensions,
userDims,
feed,
metric,
value
);
}
};
}
}
}