@Test public void generateMetrics() { Trace trace = mock(Trace.class); when(trace.getFirstFrameOfType(OperationType.HTTP)).thenReturn(makeHttpFrame()); when(trace.getRange()).thenReturn(timeRange); List<MetricsBag> mbs = gen.generateMetrics(trace, ResourceKey.valueOf("EndPoint", "epName")); assertEquals(1, mbs.size()); MetricsBag mb = mbs.get(0); List<String> keys = mb.getMetricKeys(); assertEquals(2, keys.size()); boolean foundReq = false, foundRes = false; for (String key : keys) { if (!foundReq) foundReq = key.equals(RequestResponseSizeMetricsGenerator.ENDPOINT_REQUEST_SIZE) ? true : false; if (!foundRes) foundRes = key.equals(RequestResponseSizeMetricsGenerator.ENDPOINT_RESPONSE_SIZE) ? true : false; } assertTrue(foundRes && foundReq); List<IDataPoint> points = mb.getPoints(RequestResponseSizeMetricsGenerator.ENDPOINT_REQUEST_SIZE); assertEquals(1, points.size()); assertEquals(1000.0, points.get(0).getValue(), 0.01); points = mb.getPoints(RequestResponseSizeMetricsGenerator.ENDPOINT_RESPONSE_SIZE); assertEquals(1, points.size()); assertEquals(4000.0, points.get(0).getValue(), 0.01); for (String key : keys) { mb.getPoints(key); } }
@Override public List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames) { if (ListUtil.size(frames) != 1) { return Collections.emptyList(); } Frame frame = ListUtil.getFirstMember(frames); Operation op = frame.getOperation(); ResourceKey resourceKey = getResourceKey(op, endpointResourceKey); TimeRange range = trace.getRange(); int time = TimeUtil.nanosToSeconds(range.getStart()); MetricsBag mb = MetricsBag.create(resourceKey, range); OperationMap response = op.get("response", OperationMap.class); Number contentSize = (response == null) ? null : response.get("contentSize", Number.class); if (contentSize != null) { DataPoint responseSizePoint = new DataPoint(time, contentSize.doubleValue()); mb.add(ENDPOINT_RESPONSE_SIZE, PointType.GAUGE); mb.add(responseSizePoint, ENDPOINT_RESPONSE_SIZE); } OperationMap request = op.get("request", OperationMap.class); Number contentLength = (request == null) ? null : request.get("contentLength", Number.class); if (contentLength != null) { DataPoint requestSizePoint = new DataPoint(time, contentLength.doubleValue()); mb.add(ENDPOINT_REQUEST_SIZE, PointType.GAUGE); mb.add(requestSizePoint, ENDPOINT_REQUEST_SIZE); } if (mb.isEmpty()) { return Collections.emptyList(); } else { return Collections.singletonList(mb); } }
RequestResponseSizeMetricsGenerator extends AbstractMetricsGenerator { @Override public List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames) { if (ListUtil.size(frames) != 1) { return Collections.emptyList(); } Frame frame = ListUtil.getFirstMember(frames); Operation op = frame.getOperation(); ResourceKey resourceKey = getResourceKey(op, endpointResourceKey); TimeRange range = trace.getRange(); int time = TimeUtil.nanosToSeconds(range.getStart()); MetricsBag mb = MetricsBag.create(resourceKey, range); OperationMap response = op.get("response", OperationMap.class); Number contentSize = (response == null) ? null : response.get("contentSize", Number.class); if (contentSize != null) { DataPoint responseSizePoint = new DataPoint(time, contentSize.doubleValue()); mb.add(ENDPOINT_RESPONSE_SIZE, PointType.GAUGE); mb.add(responseSizePoint, ENDPOINT_RESPONSE_SIZE); } OperationMap request = op.get("request", OperationMap.class); Number contentLength = (request == null) ? null : request.get("contentLength", Number.class); if (contentLength != null) { DataPoint requestSizePoint = new DataPoint(time, contentLength.doubleValue()); mb.add(ENDPOINT_REQUEST_SIZE, PointType.GAUGE); mb.add(requestSizePoint, ENDPOINT_REQUEST_SIZE); } if (mb.isEmpty()) { return Collections.emptyList(); } else { return Collections.singletonList(mb); } } }
RequestResponseSizeMetricsGenerator extends AbstractMetricsGenerator { @Override public List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames) { if (ListUtil.size(frames) != 1) { return Collections.emptyList(); } Frame frame = ListUtil.getFirstMember(frames); Operation op = frame.getOperation(); ResourceKey resourceKey = getResourceKey(op, endpointResourceKey); TimeRange range = trace.getRange(); int time = TimeUtil.nanosToSeconds(range.getStart()); MetricsBag mb = MetricsBag.create(resourceKey, range); OperationMap response = op.get("response", OperationMap.class); Number contentSize = (response == null) ? null : response.get("contentSize", Number.class); if (contentSize != null) { DataPoint responseSizePoint = new DataPoint(time, contentSize.doubleValue()); mb.add(ENDPOINT_RESPONSE_SIZE, PointType.GAUGE); mb.add(responseSizePoint, ENDPOINT_RESPONSE_SIZE); } OperationMap request = op.get("request", OperationMap.class); Number contentLength = (request == null) ? null : request.get("contentLength", Number.class); if (contentLength != null) { DataPoint requestSizePoint = new DataPoint(time, contentLength.doubleValue()); mb.add(ENDPOINT_REQUEST_SIZE, PointType.GAUGE); mb.add(requestSizePoint, ENDPOINT_REQUEST_SIZE); } if (mb.isEmpty()) { return Collections.emptyList(); } else { return Collections.singletonList(mb); } } private  RequestResponseSizeMetricsGenerator(); }
RequestResponseSizeMetricsGenerator extends AbstractMetricsGenerator { @Override public List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames) { if (ListUtil.size(frames) != 1) { return Collections.emptyList(); } Frame frame = ListUtil.getFirstMember(frames); Operation op = frame.getOperation(); ResourceKey resourceKey = getResourceKey(op, endpointResourceKey); TimeRange range = trace.getRange(); int time = TimeUtil.nanosToSeconds(range.getStart()); MetricsBag mb = MetricsBag.create(resourceKey, range); OperationMap response = op.get("response", OperationMap.class); Number contentSize = (response == null) ? null : response.get("contentSize", Number.class); if (contentSize != null) { DataPoint responseSizePoint = new DataPoint(time, contentSize.doubleValue()); mb.add(ENDPOINT_RESPONSE_SIZE, PointType.GAUGE); mb.add(responseSizePoint, ENDPOINT_RESPONSE_SIZE); } OperationMap request = op.get("request", OperationMap.class); Number contentLength = (request == null) ? null : request.get("contentLength", Number.class); if (contentLength != null) { DataPoint requestSizePoint = new DataPoint(time, contentLength.doubleValue()); mb.add(ENDPOINT_REQUEST_SIZE, PointType.GAUGE); mb.add(requestSizePoint, ENDPOINT_REQUEST_SIZE); } if (mb.isEmpty()) { return Collections.emptyList(); } else { return Collections.singletonList(mb); } } private  RequestResponseSizeMetricsGenerator(); static final RequestResponseSizeMetricsGenerator getInstance(); @Override Collection<Frame> locateFrames(Trace trace); @Override List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames); }
RequestResponseSizeMetricsGenerator extends AbstractMetricsGenerator { @Override public List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames) { if (ListUtil.size(frames) != 1) { return Collections.emptyList(); } Frame frame = ListUtil.getFirstMember(frames); Operation op = frame.getOperation(); ResourceKey resourceKey = getResourceKey(op, endpointResourceKey); TimeRange range = trace.getRange(); int time = TimeUtil.nanosToSeconds(range.getStart()); MetricsBag mb = MetricsBag.create(resourceKey, range); OperationMap response = op.get("response", OperationMap.class); Number contentSize = (response == null) ? null : response.get("contentSize", Number.class); if (contentSize != null) { DataPoint responseSizePoint = new DataPoint(time, contentSize.doubleValue()); mb.add(ENDPOINT_RESPONSE_SIZE, PointType.GAUGE); mb.add(responseSizePoint, ENDPOINT_RESPONSE_SIZE); } OperationMap request = op.get("request", OperationMap.class); Number contentLength = (request == null) ? null : request.get("contentLength", Number.class); if (contentLength != null) { DataPoint requestSizePoint = new DataPoint(time, contentLength.doubleValue()); mb.add(ENDPOINT_REQUEST_SIZE, PointType.GAUGE); mb.add(requestSizePoint, ENDPOINT_REQUEST_SIZE); } if (mb.isEmpty()) { return Collections.emptyList(); } else { return Collections.singletonList(mb); } } private  RequestResponseSizeMetricsGenerator(); static final RequestResponseSizeMetricsGenerator getInstance(); @Override Collection<Frame> locateFrames(Trace trace); @Override List<MetricsBag> generateMetrics(Trace trace, ResourceKey endpointResourceKey, Collection<Frame> frames); static final String ENDPOINT_RESPONSE_SIZE; static final String ENDPOINT_REQUEST_SIZE; }