/**
   * POST /api/v1/janitor will try a add a new event with the information in the url context.
   *
   * @param content the Json content passed to the http POST request
   * @return the response
   * @throws IOException
   */
  @POST
  public Response addEvent(String content) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    LOGGER.info(String.format("JSON content: '%s'", content));
    JsonNode input = mapper.readTree(content);

    String eventType = getStringField(input, "eventType");
    String resourceId = getStringField(input, "resourceId");

    Response.Status responseStatus;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
    gen.writeStartObject();
    gen.writeStringField("eventType", eventType);
    gen.writeStringField("resourceId", resourceId);

    if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(resourceId)) {
      responseStatus = Response.Status.BAD_REQUEST;
      gen.writeStringField("message", "eventType and resourceId parameters are all required");
    } else {
      if (eventType.equals("OPTIN")) {
        responseStatus = optInResource(resourceId, true, gen);
      } else if (eventType.equals("OPTOUT")) {
        responseStatus = optInResource(resourceId, false, gen);
      } else {
        responseStatus = Response.Status.BAD_REQUEST;
        gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
      }
    }
    gen.writeEndObject();
    gen.close();
    LOGGER.info("entity content is '{}'", baos.toString("UTF-8"));
    return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
  }
예제 #2
0
 @Override
 public void close() throws XMLStreamException {
   try {
     generator.close();
   } catch (IOException e) {
     throw new XMLStreamException(e);
   }
 }
예제 #3
0
 private String readBody(JsonParser jp) throws IOException {
   JsonNode node = mapper.readTree(jp);
   StringWriter out = new StringWriter();
   JsonGenerator gen = jsonFactory.createJsonGenerator(out);
   mapper.writeTree(gen, node);
   gen.flush();
   gen.close();
   return out.toString();
 }
예제 #4
0
 public String marshall(String definitions)
     throws IOException { // TODO fix this when we have the EPN ecore model
   StringWriter writer = new StringWriter();
   JsonFactory f = new JsonFactory();
   JsonGenerator generator = f.createJsonGenerator(writer);
   // TODO do the heavy lifting here passing in the writer and the json generator
   generator.close();
   return writer.toString();
 }
예제 #5
0
 private JSONObject outputAsObject() throws IOException {
   generator.close();
   try {
     return new JSONObject(out.toString());
   } catch (JSONException e) {
     log.info("Generated JSON: {}", out.toString());
     fail("Generated JSON is not valid: " + e.getMessage());
     return null; // Unreachable
   }
 }
예제 #6
0
 private void write(JsonGenerator jg, TwitterEntry entry) throws IOException {
   jg.writeStartObject();
   // can either do "jg.writeFieldName(...) + jg.writeNumber()", or this:
   jg.writeNumberField("id", entry.getId());
   jg.writeStringField("text", entry.getText());
   jg.writeNumberField("fromUserId", entry.getFromUserId());
   jg.writeNumberField("toUserId", entry.getToUserId());
   jg.writeStringField("langugeCode", entry.getLanguageCode());
   jg.writeEndObject();
   jg.close();
 }
예제 #7
0
 public static String beanToJson(Object bean) {
   StringWriter sw = new StringWriter();
   try {
     JsonGenerator jsongenerator = objmapper.getJsonFactory().createJsonGenerator(sw);
     objmapper.writeValue(jsongenerator, bean);
     jsongenerator.close();
   } catch (IOException e) {
     LOG.error("", e);
     return "";
   }
   return sw.toString();
 }
 /**
  * Gets the janitor status (e.g. to support an AWS ELB Healthcheck on an instance running
  * JanitorMonkey). Creates GET /api/v1/janitor api which responds 200 OK if JanitorMonkey is
  * running.
  *
  * @param uriInfo the uri info
  * @return the chaos events json response
  * @throws IOException Signals that an I/O exception has occurred.
  */
 @GET
 public Response getJanitorStatus(@Context UriInfo uriInfo) throws IOException {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
   gen.writeStartArray();
   gen.writeStartObject();
   gen.writeStringField("JanitorMonkeyStatus", "OnLikeDonkeyKong");
   gen.writeEndObject();
   gen.writeEndArray();
   gen.close();
   return Response.status(Response.Status.OK).entity(baos.toString("UTF-8")).build();
 }
  public void jacksonTest() throws Exception {
    JsonFactory factory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator generator = factory.createJsonGenerator(writer);
    generator.writeStartObject();
    generator.writeFieldName("bool");
    generator.writeBoolean(true);
    generator.writeFieldName("firstName");
    generator.writeString("john");
    generator.writeFieldName("age");
    generator.writeNumber(1);
    generator.writeFieldName("gg");
    generator.writeStartObject();
    generator.writeFieldName("firstName");
    generator.writeString("john");
    generator.writeEndObject();

    generator.writeEndObject();
    generator.close();
    String generated = writer.toString();
    System.out.print(generated);

    JsonParser parser = factory.createJsonParser(generated);
    assertTrue(parser.nextToken() == JsonToken.START_OBJECT);
    parser.nextToken();

    assertEquals("bool", parser.getCurrentName());
    assertTrue(parser.nextToken() == JsonToken.VALUE_TRUE);
    parser.nextToken();

    assertEquals("firstName", parser.getCurrentName());
    parser.nextToken();
    assertEquals("john", parser.getText());
    parser.nextToken();

    assertEquals("age", parser.getCurrentName());
    parser.nextToken();
    assertTrue(1 == parser.getIntValue());
    parser.nextToken();

    assertEquals("gg", parser.getCurrentName());
    assertTrue(parser.nextToken() == JsonToken.START_OBJECT);
    parser.nextToken();
    assertEquals("firstName", parser.getCurrentName());
    parser.nextToken();
    assertEquals("john", parser.getText());
    assertTrue(parser.nextToken() == JsonToken.END_OBJECT);

    assertTrue(parser.nextToken() == JsonToken.END_OBJECT);

    parser.close();
  }
예제 #10
0
 /**
  * Writes the response values back to the http response. This allows the calling code to parse the
  * response values for display to the user.
  *
  * @param responseMap the response params to write to the http response
  * @param response the http response
  * @throws IOException
  */
 private void writeToResponse(Map<String, String> responseMap, HttpServletResponse response)
     throws IOException {
   // Note: setting the content-type to text/html because otherwise IE prompt the user to download
   // the result rather than handing it off to the GWT form response handler.
   // See JIRA issue https://issues.jboss.org/browse/SRAMPUI-103
   response.setContentType("text/html; charset=UTF8"); // $NON-NLS-1$
   JsonFactory f = new JsonFactory();
   JsonGenerator g = f.createJsonGenerator(response.getOutputStream(), JsonEncoding.UTF8);
   g.useDefaultPrettyPrinter();
   g.writeStartObject();
   for (java.util.Map.Entry<String, String> entry : responseMap.entrySet()) {
     String key = entry.getKey();
     String val = entry.getValue();
     g.writeStringField(key, val);
   }
   g.writeEndObject();
   g.flush();
   g.close();
 }
 public void writeJson(JsonGenerator g) throws JsonGenerationException, IOException {
   g.writeStartObject();
   g.writeBooleanField(SUCCESS, success);
   g.writeStringField(MESSAGE, message);
   g.writeArrayFieldStart(DATA);
   if (null != list && !list.isEmpty()) {
     for (T model : list) {
       // write model data
       g.writeStartObject();
       g.writeStringField("key", model.getKey());
       g.writeStringField("label", model.getLabel());
       g.writeEndObject();
     }
   }
   g.writeEndArray();
   // write model data end
   g.writeEndObject();
   g.flush();
   g.close();
 }
    void send(List<String> eventLog) throws IOException {
      CloseableHttpClient httpClient = HttpClients.createDefault();
      try {

        JsonFactory factory = new JsonFactory();
        StringWriter writer = new StringWriter();
        JsonGenerator generator = factory.createJsonGenerator(writer);
        generator.writeStartObject();
        generator.writeArrayFieldStart("events");
        for (String eventJson : eventLog) {
          generator.writeRawValue(eventJson);
        }
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);

        RestPostProcessorDelegate.LOG.info(
            "Posting " + eventLog.size() + " events to " + targetUrl);
        HttpPost httpPost = new HttpPost(targetUrl);
        httpPost.setEntity(new StringEntity(writer.toString(), ContentType.APPLICATION_JSON));
        CloseableHttpResponse response = httpClient.execute(httpPost, context);
        int statusCode = response.getStatusLine().getStatusCode();
        LOG.info("Status code was " + statusCode + " when invoking " + targetUrl);
        if (statusCode >= 400) {
          throw new RiceRuntimeException(
              "Failed to invoke " + targetUrl + ", response code was " + statusCode);
        }

      } finally {
        httpClient.close();
      }
    }
예제 #13
0
  @Override
  public void run() {

    try {
      // command metrics
      for (HystrixCommandMetrics commandMetrics : HystrixCommandMetrics.getInstances()) {
        HystrixCommandKey key = commandMetrics.getCommandKey();
        HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);
        StringWriter jsonString = new StringWriter();
        JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);

        // Informational and Status
        json.writeStartObject();
        json.writeStringField(type.value, HystrixCommand.value);
        json.writeStringField(name.value, key.name());
        json.writeStringField(group.value, commandMetrics.getCommandGroup().name());
        json.writeNumberField(currentTime.value, (int) (System.currentTimeMillis() / 1000));

        // circuit breaker
        json.writeBooleanField(isCircuitBreakerOpen.value, circuitBreaker.isOpen());
        HystrixCommandMetrics.HealthCounts healthCounts = commandMetrics.getHealthCounts();
        json.writeNumberField(errorPercentage.value, healthCounts.getErrorPercentage());
        json.writeNumberField(errorCount.value, healthCounts.getErrorCount());
        json.writeNumberField(requestCount.value, healthCounts.getTotalRequests());

        // rolling counters  Gauge
        json.writeNumberField(
            rollingCountCollapsedRequests.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED));
        json.writeNumberField(
            rollingCountExceptionsThrown.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
        json.writeNumberField(
            rollingCountFailure.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));
        json.writeNumberField(
            rollingCountFallbackFailure.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE));
        json.writeNumberField(
            rollingCountFallbackRejection.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION));
        json.writeNumberField(
            rollingCountFallbackSuccess.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS));
        json.writeNumberField(
            rollingCountResponsesFromCache.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE));
        json.writeNumberField(
            rollingCountSemaphoreRejected.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
        json.writeNumberField(
            rollingCountShortCircuited.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED));
        json.writeNumberField(
            rollingCountSuccess.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
        json.writeNumberField(
            rollingCountThreadPoolRejected.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED));
        json.writeNumberField(
            rollingCountTimeout.value,
            commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT));

        json.writeNumberField(
            currentConcurrentExecutionCount.value,
            commandMetrics.getCurrentConcurrentExecutionCount());

        // latency percentiles
        json.writeNumberField(latencyExecute_mean.value, commandMetrics.getExecutionTimeMean());
        json.writeObjectFieldStart(latencyExecute.value);
        json.writeNumberField("0", commandMetrics.getExecutionTimePercentile(0));
        json.writeNumberField("25", commandMetrics.getExecutionTimePercentile(25));
        json.writeNumberField("50", commandMetrics.getExecutionTimePercentile(50));
        json.writeNumberField("75", commandMetrics.getExecutionTimePercentile(75));
        json.writeNumberField("90", commandMetrics.getExecutionTimePercentile(90));
        json.writeNumberField("95", commandMetrics.getExecutionTimePercentile(95));
        json.writeNumberField("99", commandMetrics.getExecutionTimePercentile(99));
        json.writeNumberField("99.5", commandMetrics.getExecutionTimePercentile(99.5));
        json.writeNumberField("100", commandMetrics.getExecutionTimePercentile(100));
        json.writeEndObject();
        //
        json.writeNumberField(latencyTotal_mean.value, commandMetrics.getTotalTimeMean());
        json.writeObjectFieldStart(latencyTotal.value);
        json.writeNumberField("0", commandMetrics.getTotalTimePercentile(0));
        json.writeNumberField("25", commandMetrics.getTotalTimePercentile(25));
        json.writeNumberField("50", commandMetrics.getTotalTimePercentile(50));
        json.writeNumberField("75", commandMetrics.getTotalTimePercentile(75));
        json.writeNumberField("90", commandMetrics.getTotalTimePercentile(90));
        json.writeNumberField("95", commandMetrics.getTotalTimePercentile(95));
        json.writeNumberField("99", commandMetrics.getTotalTimePercentile(99));
        json.writeNumberField("99.5", commandMetrics.getTotalTimePercentile(99.5));
        json.writeNumberField("100", commandMetrics.getTotalTimePercentile(100));
        json.writeEndObject();

        // property values for reporting what is actually seen by the command rather than what was
        // set somewhere
        HystrixCommandProperties commandProperties = commandMetrics.getProperties();

        json.writeNumberField(
            propertyValue_circuitBreakerRequestVolumeThreshold.value,
            commandProperties.circuitBreakerRequestVolumeThreshold().get());
        json.writeNumberField(
            propertyValue_circuitBreakerSleepWindowInMilliseconds.value,
            commandProperties.circuitBreakerSleepWindowInMilliseconds().get());
        json.writeNumberField(
            propertyValue_circuitBreakerErrorThresholdPercentage.value,
            commandProperties.circuitBreakerErrorThresholdPercentage().get());
        json.writeBooleanField(
            propertyValue_circuitBreakerForceOpen.value,
            commandProperties.circuitBreakerForceOpen().get());
        json.writeBooleanField(
            propertyValue_circuitBreakerForceClosed.value,
            commandProperties.circuitBreakerForceClosed().get());
        json.writeBooleanField(
            propertyValue_circuitBreakerEnabled.value,
            commandProperties.circuitBreakerEnabled().get());

        json.writeStringField(
            propertyValue_executionIsolationStrategy.value,
            commandProperties.executionIsolationStrategy().get().name());
        json.writeNumberField(
            propertyValue_executionIsolationThreadTimeoutInMilliseconds.value,
            commandProperties.executionIsolationThreadTimeoutInMilliseconds().get());
        json.writeBooleanField(
            propertyValue_executionIsolationThreadInterruptOnTimeout.value,
            commandProperties.executionIsolationThreadInterruptOnTimeout().get());
        json.writeStringField(
            propertyValue_executionIsolationThreadPoolKeyOverride.value,
            commandProperties.executionIsolationThreadPoolKeyOverride().get());
        json.writeNumberField(
            propertyValue_executionIsolationSemaphoreMaxConcurrentRequests.value,
            commandProperties.executionIsolationSemaphoreMaxConcurrentRequests().get());
        json.writeNumberField(
            propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests.value,
            commandProperties.fallbackIsolationSemaphoreMaxConcurrentRequests().get());

        /*
         * The following are commented out as these rarely change and are verbose for streaming for something people don't change.
         * We could perhaps allow a property or request argument to include these.
         */

        //                    json.put("propertyValue_metricsRollingPercentileEnabled",
        // commandProperties.metricsRollingPercentileEnabled().get());
        //                    json.put("propertyValue_metricsRollingPercentileBucketSize",
        // commandProperties.metricsRollingPercentileBucketSize().get());
        //                    json.put("propertyValue_metricsRollingPercentileWindow",
        // commandProperties.metricsRollingPercentileWindowInMilliseconds().get());
        //                    json.put("propertyValue_metricsRollingPercentileWindowBuckets",
        // commandProperties.metricsRollingPercentileWindowBuckets().get());
        //                    json.put("propertyValue_metricsRollingStatisticalWindowBuckets",
        // commandProperties.metricsRollingStatisticalWindowBuckets().get());
        json.writeNumberField(
            propertyValue_metricsRollingStatisticalWindowInMilliseconds.value,
            commandProperties.metricsRollingStatisticalWindowInMilliseconds().get());
        json.writeBooleanField(
            propertyValue_requestCacheEnabled.value, commandProperties.requestCacheEnabled().get());
        json.writeBooleanField(
            propertyValue_requestLogEnabled.value, commandProperties.requestLogEnabled().get());
        json.writeNumberField(reportingHosts.value, 1);

        json.writeStringField(Key.ip.value, app_ip);

        json.writeEndObject();
        json.close();
        //                System.out.println(ip + ":" + port + "||" +
        // jsonString.getBuffer().toString());
        UDPClient.send(
            socketIp, socketPort, jsonString.getBuffer().toString().getBytes(), new byte[] {});
      }

      // thread pool metrics
      for (HystrixThreadPoolMetrics threadPoolMetrics : HystrixThreadPoolMetrics.getInstances()) {
        HystrixThreadPoolKey key = threadPoolMetrics.getThreadPoolKey();

        StringWriter jsonString = new StringWriter();
        JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);
        json.writeStartObject();

        json.writeStringField(type.value, HystrixThreadPool.value);
        json.writeStringField(name.value, key.name());
        json.writeNumberField(currentTime.value, System.currentTimeMillis());

        // 101.3 80 154

        json.writeNumberField(
            currentActiveCount.value, threadPoolMetrics.getCurrentActiveCount().intValue());
        json.writeNumberField(
            currentCompletedTaskCount.value,
            threadPoolMetrics.getCurrentCompletedTaskCount().longValue());
        json.writeNumberField(
            currentCorePoolSize.value, threadPoolMetrics.getCurrentCorePoolSize().intValue());
        json.writeNumberField(
            currentLargestPoolSize.value, threadPoolMetrics.getCurrentLargestPoolSize().intValue());
        json.writeNumberField(
            currentMaximumPoolSize.value, threadPoolMetrics.getCurrentMaximumPoolSize().intValue());
        json.writeNumberField(
            currentPoolSize.value, threadPoolMetrics.getCurrentPoolSize().intValue());
        json.writeNumberField(
            currentQueueSize.value, threadPoolMetrics.getCurrentQueueSize().intValue());
        json.writeNumberField(
            currentTaskCount.value, threadPoolMetrics.getCurrentTaskCount().longValue());
        json.writeNumberField(
            rollingCountThreadsExecuted.value, threadPoolMetrics.getRollingCountThreadsExecuted());
        json.writeNumberField(
            rollingMaxActiveThreads.value, threadPoolMetrics.getRollingMaxActiveThreads());

        json.writeNumberField(
            propertyValue_queueSizeRejectionThreshold.value,
            threadPoolMetrics.getProperties().queueSizeRejectionThreshold().get());
        json.writeNumberField(
            propertyValue_metricsRollingStatisticalWindowInMilliseconds.value,
            threadPoolMetrics
                .getProperties()
                .metricsRollingStatisticalWindowInMilliseconds()
                .get());

        json.writeNumberField(
            reportingHosts.value, 1); // this will get summed across all instances in a cluster
        json.writeStringField(Key.ip.value, app_ip);
        json.writeEndObject();
        json.close();

        String str = jsonString.getBuffer().toString();
        byte[] ret = str.getBytes();
        UDPClient.send(socketIp, socketPort, ret, new byte[] {});
      }
    } catch (Exception e) {
      System.err.println("Failed to output metrics as JSON");
      e.printStackTrace();
    }
  }
예제 #14
-1
  private void serialize(Query query, OutputStream outputStream) throws IOException {
    JsonGenerator g = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8);
    g.useDefaultPrettyPrinter();
    g.writeStartObject();
    g.writeStringField("name", "jmxtrans");
    g.writeStringField("type", "metric");
    g.writeStringField("handler", sensuhandler);

    StringBuffer jsonoutput = new StringBuffer();
    List<String> typeNames = getTypeNames();
    for (Result result : query.getResults()) {
      Map<String, Object> resultValues = result.getValues();
      if (resultValues != null) {
        for (Map.Entry<String, Object> values : resultValues.entrySet()) {
          if (NumberUtils.isNumeric(values.getValue())) {
            Object value = values.getValue();
            jsonoutput
                .append(JmxUtils.getKeyString(query, result, values, typeNames, null))
                .append(" ")
                .append(value)
                .append(" ")
                .append(TimeUnit.SECONDS.convert(result.getEpoch(), TimeUnit.MILLISECONDS))
                .append(System.getProperty("line.separator"));
          }
        }
      }
    }
    g.writeStringField("output", jsonoutput.toString());
    g.writeEndObject();
    g.flush();
    g.close();
  }