Search in sources :

Example 1 with Predicate

use of io.fabric8.insight.log.support.Predicate in project fabric8 by jboss-fuse.

the class Log4jLogQuery method filterLogResults.

protected LogResults filterLogResults(Predicate<LogEvent> predicate, int maxCount) {
    int matched = 0;
    long from = Long.MAX_VALUE;
    long to = Long.MIN_VALUE;
    List<LogEvent> list = new ArrayList<LogEvent>();
    Iterable<LoggingEvent> elements = getEvents().getElements();
    for (LoggingEvent element : elements) {
        LogEvent logEvent = toLogEvent(element);
        long timestamp = element.getTimeStamp();
        if (timestamp > to) {
            to = timestamp;
        }
        if (timestamp < from) {
            from = timestamp;
        }
        if (logEvent != null) {
            if (predicate == null || predicate.matches(logEvent)) {
                list.add(logEvent);
                matched += 1;
                if (maxCount > 0 && matched >= maxCount) {
                    break;
                }
            }
        }
    }
    LogResults results = new LogResults();
    results.setEvents(list);
    if (from < Long.MAX_VALUE) {
        results.setFromTimestamp(from);
    }
    if (to > Long.MIN_VALUE) {
        results.setToTimestamp(to);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Requested " + maxCount + " logging items. returning " + results.getEvents().size() + " event(s) from a possible " + getEvents().size());
    }
    return results;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) LogResults(io.fabric8.insight.log.LogResults) LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList)

Example 2 with Predicate

use of io.fabric8.insight.log.support.Predicate in project fabric8 by jboss-fuse.

the class Log4jLogQuery method createPredicate.

private Predicate<LogEvent> createPredicate(LogFilter filter) {
    if (filter == null) {
        return null;
    }
    final List<Predicate<LogEvent>> predicates = new ArrayList<Predicate<LogEvent>>();
    final Set<String> levels = filter.getLevelsSet();
    if (levels.size() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                String level = event.getLevel();
                return level != null && levels.contains(level.toString());
            }
        });
    }
    final Long before = filter.getBeforeTimestamp();
    if (before != null) {
        final Date date = new Date(before);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.before(date);
            }
        });
    }
    final Long after = filter.getAfterTimestamp();
    if (after != null) {
        final Date date = new Date(after);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.after(date);
            }
        });
    }
    final String matchesText = filter.getMatchesText();
    if (matchesText != null && matchesText.length() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                if (contains(matchesText, event.getClassName(), event.getMessage(), event.getLogger(), event.getThread())) {
                    return true;
                }
                String[] throwableStrRep = event.getException();
                if (throwableStrRep != null && contains(matchesText, throwableStrRep)) {
                    return true;
                }
                Map properties = event.getProperties();
                if (properties != null && contains(matchesText, properties.toString())) {
                    return true;
                }
                return false;
            }
        });
    }
    if (predicates.size() == 0) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {
        return new Predicate<LogEvent>() {

            @Override
            public String toString() {
                return "AndPredicate" + predicates;
            }

            @Override
            public boolean matches(LogEvent event) {
                for (Predicate<LogEvent> predicate : predicates) {
                    if (!predicate.matches(event)) {
                        return false;
                    }
                }
                return true;
            }
        };
    }
}
Also used : LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList) Map(java.util.Map) Date(java.util.Date) Predicate(io.fabric8.insight.log.support.Predicate)

Example 3 with Predicate

use of io.fabric8.insight.log.support.Predicate in project strimzi by strimzi.

the class StatefulSetOperator method restartPod.

private Future<Void> restartPod(String namespace, String name, Predicate<String> isReady, String podName) {
    Future<Void> result = Future.future();
    log.info("Roll {}/{}: Rolling pod {}", namespace, name, podName);
    Future<Void> deleted = Future.future();
    Future<CompositeFuture> deleteFinished = Future.future();
    Watcher<Pod> watcher = new RollingUpdateWatcher(deleted);
    Watch watch = podOperations.watch(namespace, podName, watcher);
    // Delete the pod
    log.debug("Roll {}/{}: Waiting for pod {} to be deleted", namespace, name, podName);
    Future podReconcileFuture = podOperations.reconcile(namespace, podName, null);
    CompositeFuture.join(podReconcileFuture, deleted).setHandler(deleteResult -> {
        watch.close();
        if (deleteResult.succeeded()) {
            log.debug("Roll {}/{}: Pod {} was deleted", namespace, name, podName);
        }
        deleteFinished.handle(deleteResult);
    });
    deleteFinished.compose(ix -> {
        log.debug("Roll {}/{}: Waiting for new pod {} to get ready", namespace, name, podName);
        Future<Void> readyFuture = Future.future();
        vertx.setPeriodic(1_000, timerId -> {
            p(isReady, podName).setHandler(x -> {
                if (x.succeeded()) {
                    if (x.result()) {
                        vertx.cancelTimer(timerId);
                        readyFuture.complete();
                    }
                // else not ready
                } else {
                    vertx.cancelTimer(timerId);
                    readyFuture.fail(x.cause());
                }
            });
        });
        return readyFuture;
    }).setHandler(result);
    return result;
}
Also used : CompositeFuture(io.vertx.core.CompositeFuture) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) Logger(org.slf4j.Logger) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Predicate(java.util.function.Predicate) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) Pod(io.fabric8.kubernetes.api.model.Pod) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) Future(io.vertx.core.Future) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture)

Example 4 with Predicate

use of io.fabric8.insight.log.support.Predicate in project fabric8 by jboss-fuse.

the class LogQuery method getLogEventList.

public LogResults getLogEventList(int count, Predicate<PaxLoggingEvent> predicate) {
    LogResults answer = new LogResults();
    answer.setHost(getHostName());
    long from = Long.MAX_VALUE;
    long to = Long.MIN_VALUE;
    if (appender != null) {
        LruList events = appender.getEvents();
        if (events != null) {
            Iterable<PaxLoggingEvent> iterable = events.getElements();
            if (iterable != null) {
                int matched = 0;
                for (PaxLoggingEvent event : iterable) {
                    if (event != null) {
                        long timestamp = event.getTimeStamp();
                        if (timestamp > to) {
                            to = timestamp;
                        }
                        if (timestamp < from) {
                            from = timestamp;
                        }
                        if (predicate == null || predicate.matches(event)) {
                            answer.addEvent(Logs.newInstance(event));
                            matched += 1;
                            if (count > 0 && matched >= count) {
                                break;
                            }
                        }
                    }
                }
            }
        }
    } else {
        LOG.warn("No VmLogAppender available!");
    }
    answer.setFromTimestamp(from);
    answer.setToTimestamp(to);
    return answer;
}
Also used : PaxLoggingEvent(org.ops4j.pax.logging.spi.PaxLoggingEvent) LogResults(io.fabric8.insight.log.LogResults) LruList(org.apache.karaf.shell.log.LruList)

Example 5 with Predicate

use of io.fabric8.insight.log.support.Predicate in project strimzi by strimzi.

the class ControllerTest method configMapRemoved.

// TODO 3way reconcilation where kafka and kube agree
// TODO 3way reconcilation where all three agree
// TODO 3way reconcilation with conflict
// TODO reconciliation where only private state exists => delete the private state
// TODO tests for the other reconciliation cases
// + non-matching predicate
// + error cases
private void configMapRemoved(TestContext context, Exception deleteTopicException, Exception storeException) {
    Topic kubeTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "bar")).build();
    Topic kafkaTopic = kubeTopic;
    Topic privateTopic = kubeTopic;
    mockKafka.setCreateTopicResponse(topicName.toString(), null).createTopic(kafkaTopic, ar -> {
    });
    mockKafka.setTopicMetadataResponse(topicName, Utils.getTopicMetadata(kubeTopic), null);
    mockKafka.setDeleteTopicResponse(topicName, deleteTopicException);
    mockTopicStore.setCreateTopicResponse(topicName, null).create(privateTopic, ar -> {
    });
    mockTopicStore.setDeleteTopicResponse(topicName, storeException);
    ConfigMap cm = TopicSerialization.toConfigMap(kubeTopic, cmPredicate);
    Async async = context.async();
    controller.onConfigMapDeleted(cm, ar -> {
        if (deleteTopicException != null || storeException != null) {
            assertFailed(context, ar);
            if (deleteTopicException != null) {
                // should still exist
                mockKafka.assertExists(context, kafkaTopic.getTopicName());
            } else {
                mockKafka.assertNotExists(context, kafkaTopic.getTopicName());
            }
            mockTopicStore.assertExists(context, kafkaTopic.getTopicName());
        } else {
            assertSucceeded(context, ar);
            mockKafka.assertNotExists(context, kafkaTopic.getTopicName());
            mockTopicStore.assertNotExists(context, kafkaTopic.getTopicName());
        }
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async)

Aggregations

ArrayList (java.util.ArrayList)3 LogEvent (io.fabric8.insight.log.LogEvent)2 LogResults (io.fabric8.insight.log.LogResults)2 Predicate (io.fabric8.insight.log.support.Predicate)1 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 DoneableStatefulSet (io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet)1 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)1 StatefulSetList (io.fabric8.kubernetes.api.model.extensions.StatefulSetList)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 Watch (io.fabric8.kubernetes.client.Watch)1 Watcher (io.fabric8.kubernetes.client.Watcher)1 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)1 RollableScalableResource (io.fabric8.kubernetes.client.dsl.RollableScalableResource)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 Vertx (io.vertx.core.Vertx)1 Async (io.vertx.ext.unit.Async)1 Date (java.util.Date)1