Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
DefaultMetricService.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.engine.e1.services;
2 
3 import com.griddynamics.jagger.coordinator.NodeContext;
4 import com.griddynamics.jagger.engine.e1.collector.MetricDescription;
5 import com.griddynamics.jagger.storage.KeyValueStorage;
6 import com.griddynamics.jagger.storage.Namespace;
7 import com.griddynamics.jagger.storage.fs.logging.LogWriter;
8 import com.griddynamics.jagger.storage.fs.logging.MetricLogEntry;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 
12 import java.io.File;
13 import java.io.UnsupportedEncodingException;
14 import java.net.URLEncoder;
15 
29 public class DefaultMetricService implements MetricService {
30  private static final Logger log = LoggerFactory.getLogger(DefaultMetricService.class);
31 
32 
33  public static final String METRIC_MARKER = "METRIC";
34 
35  protected String sessionId;
36  protected String taskId;
37  protected NodeContext context;
38 
39  public DefaultMetricService(String sessionId, String taskId, NodeContext context){
40  this.sessionId = sessionId;
41  this.taskId = taskId;
42  this.context = context;
43  }
44 
45  @Override
46  public void createMetric(MetricDescription metricDescription) {
47  KeyValueStorage storage = context.getService(KeyValueStorage.class);
48 
49  storage.put(Namespace.of(sessionId, taskId, "metricDescription"),
50  metricDescription.getMetricId(),
51  metricDescription
52  );
53  }
54 
55  @Override
56  public void saveValue(String metricId, Number value) {
57  long current = System.currentTimeMillis();
58  saveValue(metricId, value, current);
59  }
60 
61  @Override
62  public void saveValue(String metricId, Number value, long timeStamp) {
63  LogWriter logWriter = context.getService(LogWriter.class);
64  try {
65  metricId = URLEncoder.encode(metricId, "UTF-8");
66  logWriter.log(sessionId, taskId + File.separatorChar + METRIC_MARKER + File.separatorChar + metricId, context.getId().getIdentifier(),
67  new MetricLogEntry(timeStamp, metricId, value));
68  } catch (UnsupportedEncodingException e) {
69  log.error("Can't save metric value with id={}", metricId, e);
70  }
71  }
72 
73  @Override
74  public void flush() {
75  LogWriter logWriter = context.getService(LogWriter.class);
76  logWriter.flush();
77  }
78 
79  @Override
80  public boolean isAvailable() {
81  return true;
82  }
83 }