@Override public void process(JCas aJCas) throws AnalysisEngineProcessException { monitor.trace("Starting: " + getClass().getSimpleName()); monitor.startFunction("process"); doProcess(aJCas); monitor.finishFunction("process"); monitor.persistCounts(); monitor.trace("Finished: " + getClass().getSimpleName()); }
@Override public void accept(byte[] message) { try { channel.basicPublish(exchangeName, routingKey, null, message); } catch (final IOException e) { monitor.warn("Failed to publish message", e); } } }
@Override protected void doProcess(JCas jCas) throws AnalysisEngineProcessException { JCasUtil.select(jCas, clazz) .stream() .map(this::print) .filter(Objects::nonNull) .forEach(s -> getMonitor().info("{}:\n{}", clazz.getName(), s)); }
private static JsonNode loadCountriesJson(ObjectMapper mapper, UimaMonitor uimaMonitor) throws ResourceInitializationException { try (InputStream is = SharedCountryResource.class.getResourceAsStream("countries/countries.json"); ) { return mapper.readTree(is); } catch (IOException ioe) { uimaMonitor.error("Unable to read nationalities from countries.json", ioe); throw new ResourceInitializationException(ioe); } }
@Override protected void doDestroy() { if (connection != null) { try { connection.close(); } catch (SQLException e) { getMonitor().debug("Failed to close PostgreSQL Connection", e); // Do nothing } connection = null; } }
@Override public void doGetNext(JCas jCas) throws IOException, CollectionException { if (queue.isEmpty()) { getMonitor().error("No documents on the queue - this method should not have been called"); throw new CollectionException(); } Path path = queue.remove(0); getMonitor().info("Processing file {}", path.toString()); try (InputStream is = new FileInputStream(path.toFile()); ) { extractor.processStream(is, path.toString(), jCas); } }
/** * Create a monitor based on the pipeline name. * * @param pipelineName * @return monitor (non null) */ protected UimaMonitor createMonitor(String pipelineName) { return new UimaMonitor(pipelineName, this.getClass()); } }
@Override public final void initialize(final UimaContext context) throws ResourceInitializationException { // This will do initialization of resources, // but won't be included in the metrics super.initialize(context); final String pipelineName = UimaUtils.getPipelineName(context); monitor = new UimaMonitor(pipelineName, this.getClass()); getMonitor().startFunction("initialize"); // Pull the config parameters out for job settings config = BaleenScheduler.getConfigParameters(context); doInitialize(context); getMonitor().finishFunction("initialize"); }
@Test public void testMetrics() { UimaMonitor monitor = new UimaMonitor(PIPELINE, this.getClass()); assertNotNull(monitor.counter("a")); assertNotNull(monitor.meter("b")); assertNotNull(monitor.histogram("c")); assertNotNull(monitor.timer("d")); }
@Test public void testEntityAdded() { UimaMonitor monitor = new UimaMonitor(PIPELINE, this.getClass()); monitor.entityAdded(ENTITY_TYPE_1); monitor.entityAdded(ENTITY_TYPE_1); monitor.entityAdded(ENTITY_TYPE_2); monitor.entityAdded(ENTITY_TYPE_2); monitor.entityAdded(ENTITY_TYPE_1); monitor.persistCounts(); Metrics m = MetricsFactory.getMetrics(PIPELINE, this.getClass()); assertEquals(3, m.getCounter(ENTITY_TYPE_1 + "-added").getCount()); assertEquals(2, m.getCounter(ENTITY_TYPE_2 + "-added").getCount()); }
@Test public void testEntityRemoved() { UimaMonitor monitor = new UimaMonitor(PIPELINE, this.getClass()); monitor.entityRemoved(ENTITY_TYPE_1); monitor.entityRemoved(ENTITY_TYPE_1); monitor.entityRemoved(ENTITY_TYPE_2); monitor.entityRemoved(ENTITY_TYPE_2); monitor.entityRemoved(ENTITY_TYPE_1); monitor.persistCounts(); Metrics m = MetricsFactory.getMetrics(PIPELINE, this.getClass()); assertEquals(3, m.getCounter(ENTITY_TYPE_1 + "-removed").getCount()); assertEquals(2, m.getCounter(ENTITY_TYPE_2 + "-removed").getCount()); }
protected void addMapping(final String externalId, final String id) { if (ids.containsKey(externalId)) { getMonitor().warn("Duplicate external id {} added, previous will be discarded", externalId); } ids.put(externalId, id); } }
@Override public void flush() throws IOException { super.flush(); monitor.info("{}:\n{}", AbstractDocumentGraphFormatConsumer.class.getName(), toString()); } };
private static JsonNode loadCountriesJson(ObjectMapper mapper, UimaMonitor uimaMonitor) throws ResourceInitializationException { try (InputStream is = SharedCountryResource.class.getResourceAsStream("countries/countries.json"); ) { return mapper.readTree(is); } catch (IOException ioe) { uimaMonitor.error("Unable to read nationalities from countries.json", ioe); throw new ResourceInitializationException(ioe); } }
@Override protected void doDestroy() { if (connection != null) { try { connection.close(); } catch (SQLException e) { getMonitor().debug("Failed to close PostgreSQL Connection", e); // Do nothing } connection = null; } }
@Override public void doGetNext(JCas jCas) throws IOException, CollectionException { if (queue.isEmpty()) { getMonitor().error("No documents on the queue - this method should not have been called"); throw new CollectionException(); } Path path = queue.remove(0); getMonitor().info("Processing file {}", path.toString()); try (InputStream is = new FileInputStream(path.toFile()); ) { extractContent(is, path.toString(), jCas); } }
/** * Create a monitor based on pipeline. * * @param pipelineName * @return monitor (non-null) */ protected UimaMonitor createMonitor(String pipelineName) { return new UimaMonitor(pipelineName, this.getClass()); } }