public SignalFxRequestError(ErrorResponse errorResponse, String program, long start, long end, long resolution, String accountName) { super(String.format(MSG_TEMPLATE, program, start, end, resolution, accountName, errorResponse.toString())); } }
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException { if (!CONVERTIBLE_TYPES.contains(type)) { throw new ConversionException( String.format("The SignalFxConverter Retrofit converter can only handle Types: [ %s ], received: %s", CONVERTIBLE_TYPES.stream().map(Type::getTypeName).collect(Collectors.joining(", ")), type.getTypeName())); } if (type.getTypeName().equals(SignalFlowExecutionResult.class.getTypeName())) { return getSignalFlowExecutionResultFromBody(body); } else { return getErrorResponseFromBody(body); } }
@Test public void test_that_a_signalfx_signal_flow_response_can_be_parsed() throws Exception { InputStream response = getClass().getClassLoader().getResourceAsStream("signalfx-signalflow-response.text"); SignalFxConverter converter = new SignalFxConverter(); TypedInput typedInput = new TypedByteArray("text/plain", ByteStreams.toByteArray(response)); SignalFlowExecutionResult signalFlowExecutionResult = (SignalFlowExecutionResult) converter.fromBody(typedInput, SignalFlowExecutionResult.class); assertNotNull(signalFlowExecutionResult); assertThat("The signalFlowExecutionResult contains the channel messages", signalFlowExecutionResult.getChannelMessages().size(), greaterThan(1)); } }
try { signalFlowExecutionResult = signalFlowService .executeSignalFlowProgram(accessToken, startEpochMilli, endEpochMilli, stepMilli, 0, true, program); } catch (RetrofitError e) { ErrorResponse errorResponse = (ErrorResponse) e.getBodyAs(ErrorResponse.class); throw new SignalFxRequestError(errorResponse, program, startEpochMilli, endEpochMilli, stepMilli, metricsAccountName); .endTimeIso(Instant.ofEpochMilli(endEpochMilli).toString()) .stepMillis(stepMilli) .values(getTimeSeriesDataFromChannelMessages(signalFlowExecutionResult.getChannelMessages())) .tags(queryPairs.stream().collect(Collectors.toMap(QueryPair::getKey, QueryPair::getValue))) .attribute("signal-flow-program", program)
accountCredentialsBuilder.signalFlowService(retrofitClientFactory.createClient( SignalFxSignalFlowRemoteService.class, new SignalFxConverter(), signalFxSignalFlowEndpoint, okHttpClient
private SignalFlowExecutionResult getSignalFlowExecutionResultFromBody(TypedInput body) throws ConversionException { List<ChannelMessage> messages = new LinkedList<>(); try (ServerSentEventsTransport.TransportEventStreamParser parser = new ServerSentEventsTransport.TransportEventStreamParser(body.in())) { while (parser.hasNext()) { StreamMessage streamMessage = parser.next(); ChannelMessage channelMessage = ChannelMessage.decodeStreamMessage(streamMessage); messages.add(channelMessage); } } catch (Exception e) { throw new ConversionException("There was an issue parsing the SignalFlow response", e); } return new SignalFlowExecutionResult(messages); }
try { signalFlowExecutionResult = signalFlowService .executeSignalFlowProgram(accessToken, startEpochMilli, endEpochMilli, stepMilli, 0, true, program); } catch (RetrofitError e) { ErrorResponse errorResponse = (ErrorResponse) e.getBodyAs(ErrorResponse.class); throw new SignalFxRequestError(errorResponse, program, startEpochMilli, endEpochMilli, stepMilli, metricsAccountName); .endTimeIso(Instant.ofEpochMilli(endEpochMilli).toString()) .stepMillis(stepMilli) .values(getTimeSeriesDataFromChannelMessages(signalFlowExecutionResult.getChannelMessages())) .tags(queryPairs.stream().collect(Collectors.toMap(QueryPair::getKey, QueryPair::getValue))) .attribute("signal-flow-program", program)
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException { if (!CONVERTIBLE_TYPES.contains(type)) { throw new ConversionException( String.format("The SignalFxConverter Retrofit converter can only handle Types: [ %s ], received: %s", CONVERTIBLE_TYPES.stream().map(Type::getTypeName).collect(Collectors.joining(", ")), type.getTypeName())); } if (type.getTypeName().equals(SignalFlowExecutionResult.class.getTypeName())) { return getSignalFlowExecutionResultFromBody(body); } else { return getErrorResponseFromBody(body); } }
accountCredentialsBuilder.signalFlowService(retrofitClientFactory.createClient( SignalFxSignalFlowRemoteService.class, new SignalFxConverter(), signalFxSignalFlowEndpoint, okHttpClient
public SignalFxRequestError(ErrorResponse errorResponse, String program, long start, long end, long resolution, String accountName) { super(String.format(MSG_TEMPLATE, program, start, end, resolution, accountName, errorResponse.toString())); } }
private SignalFlowExecutionResult getSignalFlowExecutionResultFromBody(TypedInput body) throws ConversionException { List<ChannelMessage> messages = new LinkedList<>(); try (ServerSentEventsTransport.TransportEventStreamParser parser = new ServerSentEventsTransport.TransportEventStreamParser(body.in())) { while (parser.hasNext()) { StreamMessage streamMessage = parser.next(); ChannelMessage channelMessage = ChannelMessage.decodeStreamMessage(streamMessage); messages.add(channelMessage); } } catch (Exception e) { throw new ConversionException("There was an issue parsing the SignalFlow response", e); } return new SignalFlowExecutionResult(messages); }