Open Kilda Java Documentation
MetricGenBolt.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.topology.stats.metrics;
17 
18 import com.fasterxml.jackson.core.JsonProcessingException;
19 import org.apache.storm.task.OutputCollector;
20 import org.apache.storm.task.TopologyContext;
21 import org.apache.storm.topology.OutputFieldsDeclarer;
22 import org.apache.storm.topology.base.BaseRichBolt;
27 
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 
32 public abstract class MetricGenBolt extends BaseRichBolt {
33 
34  protected OutputCollector collector;
35 
36  protected static List<Object> tuple(String metric, long timestamp, Number value, Map<String, String> tag)
37  throws JsonEncodeException {
38  Datapoint datapoint = new Datapoint(metric, timestamp, tag, value);
39  String json;
40  try {
41  json = Utils.MAPPER.writeValueAsString(datapoint);
42  } catch (JsonProcessingException e) {
43  throw new JsonEncodeException(datapoint, e);
44  }
45  return Collections.singletonList(json);
46  }
47 
48  @Override
49  public void declareOutputFields(OutputFieldsDeclarer declarer) {
50  declarer.declare(AbstractTopology.fieldMessage);
51  }
52 
53  @Override
54  public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
55  this.collector = collector;
56  }
57 }
void prepare(Map stormConf, TopologyContext context, OutputCollector collector)
static final ObjectMapper MAPPER
Definition: Utils.java:31
value
Definition: nodes.py:62
static List< Object > tuple(String metric, long timestamp, Number value, Map< String, String > tag)
void declareOutputFields(OutputFieldsDeclarer declarer)