Search in sources :

Example 1 with Pair

use of com.wavefront.common.Pair in project java by wavefrontHQ.

the class JsonMetricsGeneratorTest method testTranslator.

@Test
public void testTranslator() throws IOException {
    Counter counter = testRegistry.newCounter(new MetricName("test", "foo", "bar"));
    counter.inc();
    counter.inc();
    String json = generate(false, false, false, metricNameMetricPair -> {
        assertThat(metricNameMetricPair._1).isEquivalentAccordingToCompareTo(new MetricName("test", "foo", "bar"));
        assertThat(metricNameMetricPair._2).isInstanceOf(Counter.class);
        assertThat(((Counter) metricNameMetricPair._2).count()).isEqualTo(2);
        return new Pair<>(new MetricName("test", "baz", "qux"), metricNameMetricPair._2);
    });
    assertThat(json).isEqualTo("{\"test.qux\":2}");
    json = generate(false, false, false, metricNameMetricPair -> null);
    assertThat(json).isEqualTo("{}");
}
Also used : Counter(com.yammer.metrics.core.Counter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Histogram(com.yammer.metrics.core.Histogram) Test(org.junit.Test) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) AtomicLong(java.util.concurrent.atomic.AtomicLong) ImmutableList(com.google.common.collect.ImmutableList) MetricsRegistry(com.yammer.metrics.core.MetricsRegistry) Pair(com.wavefront.common.Pair) Map(java.util.Map) MetricName(com.yammer.metrics.core.MetricName) WavefrontHistogram(com.yammer.metrics.core.WavefrontHistogram) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Before(org.junit.Before) MetricName(com.yammer.metrics.core.MetricName) Counter(com.yammer.metrics.core.Counter) Pair(com.wavefront.common.Pair) Test(org.junit.Test)

Example 2 with Pair

use of com.wavefront.common.Pair in project java by wavefrontHQ.

the class ConcurrentShardedQueueFileTest method testConcurrency.

@Test
public void testConcurrency() throws Exception {
    File file = new File(File.createTempFile("proxyConcurrencyTest", null).getPath() + ".spool");
    ConcurrentShardedQueueFile queueFile = new ConcurrentShardedQueueFile(file.getCanonicalPath(), ".spool", 1024 * 1024, s -> new TapeQueueFile(new QueueFile.Builder(new File(s)).build()));
    Queue<Pair<Integer, Byte>> taskCheatSheet = new ArrayDeque<>();
    System.out.println(queueFile.shards.size());
    AtomicLong tasksGenerated = new AtomicLong();
    AtomicLong nanosAdd = new AtomicLong();
    AtomicLong nanosGet = new AtomicLong();
    while (queueFile.shards.size() < 4) {
        byte[] task = randomTask();
        queueFile.add(task);
        taskCheatSheet.add(Pair.of(task.length, task[0]));
        tasksGenerated.incrementAndGet();
    }
    AtomicBoolean done = new AtomicBoolean(false);
    AtomicBoolean fail = new AtomicBoolean(false);
    Runnable addTask = () -> {
        int delay = 0;
        while (!done.get() && !fail.get()) {
            try {
                byte[] task = randomTask();
                long start = System.nanoTime();
                queueFile.add(task);
                nanosAdd.addAndGet(System.nanoTime() - start);
                taskCheatSheet.add(Pair.of(task.length, task[0]));
                tasksGenerated.incrementAndGet();
                Thread.sleep(delay / 1000);
                delay++;
            } catch (Exception e) {
                e.printStackTrace();
                fail.set(true);
            }
        }
    };
    Runnable getTask = () -> {
        int delay = 2000;
        while (!taskCheatSheet.isEmpty() && !fail.get()) {
            try {
                long start = System.nanoTime();
                Pair<Integer, Byte> taskData = taskCheatSheet.remove();
                byte[] task = queueFile.peek();
                queueFile.remove();
                nanosGet.addAndGet(System.nanoTime() - start);
                if (taskData._1 != task.length) {
                    System.out.println("Data integrity fail! Expected: " + taskData._1 + " bytes, got " + task.length + " bytes");
                    fail.set(true);
                }
                for (byte b : task) {
                    if (taskData._2 != b) {
                        System.out.println("Data integrity fail! Expected " + taskData._2 + ", got " + b);
                        fail.set(true);
                    }
                }
                Thread.sleep(delay / 500);
                if (delay > 0)
                    delay--;
            } catch (Exception e) {
                e.printStackTrace();
                fail.set(true);
            }
        }
        done.set(true);
    };
    ExecutorService executor = Executors.newFixedThreadPool(2);
    long start = System.nanoTime();
    Future<?> addFuture = executor.submit(addTask);
    Future<?> getFuture = executor.submit(getTask);
    addFuture.get();
    getFuture.get();
    assertFalse(fail.get());
    System.out.println("Tasks generated: " + tasksGenerated.get());
    System.out.println("Real time (ms) = " + (System.nanoTime() - start) / 1_000_000);
    System.out.println("Add + remove time (ms) = " + (nanosGet.get() + nanosAdd.get()) / 1_000_000);
    System.out.println("Add time (ms) = " + nanosAdd.get() / 1_000_000);
    System.out.println("Remove time (ms) = " + nanosGet.get() / 1_000_000);
}
Also used : ArrayDeque(java.util.ArrayDeque) QueueFile(com.squareup.tape2.QueueFile) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) QueueFile(com.squareup.tape2.QueueFile) Pair(com.wavefront.common.Pair) Test(org.junit.Test)

Example 3 with Pair

use of com.wavefront.common.Pair in project java by wavefrontHQ.

the class JsonMetricsPortUnificationHandler method handleHttpMessage.

@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) throws URISyntaxException {
    StringBuilder output = new StringBuilder();
    try {
        URI uri = new URI(request.uri());
        Map<String, String> params = Arrays.stream(uri.getRawQuery().split("&")).map(x -> new Pair<>(x.split("=")[0].trim().toLowerCase(), x.split("=")[1])).collect(Collectors.toMap(k -> k._1, v -> v._2));
        String requestBody = request.content().toString(CharsetUtil.UTF_8);
        Map<String, String> tags = Maps.newHashMap();
        params.entrySet().stream().filter(x -> !STANDARD_PARAMS.contains(x.getKey()) && x.getValue().length() > 0).forEach(x -> tags.put(x.getKey(), x.getValue()));
        List<ReportPoint> points = new ArrayList<>();
        long timestamp;
        if (params.get("d") == null) {
            timestamp = Clock.now();
        } else {
            try {
                timestamp = Long.parseLong(params.get("d"));
            } catch (NumberFormatException e) {
                timestamp = Clock.now();
            }
        }
        String prefix = this.prefix == null ? params.get("p") : params.get("p") == null ? this.prefix : this.prefix + "." + params.get("p");
        String host = params.get("h") == null ? defaultHost : params.get("h");
        JsonNode metrics = jsonParser.readTree(requestBody);
        ReportableEntityPreprocessor preprocessor = preprocessorSupplier == null ? null : preprocessorSupplier.get();
        String[] messageHolder = new String[1];
        JsonMetricsParser.report("dummy", prefix, metrics, points, host, timestamp);
        for (ReportPoint point : points) {
            if (point.getAnnotations().isEmpty()) {
                point.setAnnotations(tags);
            } else {
                Map<String, String> newAnnotations = Maps.newHashMap(tags);
                newAnnotations.putAll(point.getAnnotations());
                point.setAnnotations(newAnnotations);
            }
            if (preprocessor != null) {
                preprocessor.forReportPoint().transform(point);
                if (!preprocessor.forReportPoint().filter(point, messageHolder)) {
                    if (messageHolder[0] != null) {
                        pointHandler.reject(point, messageHolder[0]);
                    } else {
                        pointHandler.block(point);
                    }
                    continue;
                }
            }
            pointHandler.report(point);
        }
        writeHttpResponse(ctx, HttpResponseStatus.OK, output, request);
    } catch (IOException e) {
        logWarning("WF-300: Error processing incoming JSON request", e, ctx);
        writeHttpResponse(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, output, request);
    }
}
Also used : Arrays(java.util.Arrays) HealthCheckManager(com.wavefront.agent.channel.HealthCheckManager) URISyntaxException(java.net.URISyntaxException) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TokenAuthenticator(com.wavefront.agent.auth.TokenAuthenticator) ReportableEntityHandler(com.wavefront.agent.handlers.ReportableEntityHandler) ChannelUtils.writeHttpResponse(com.wavefront.agent.channel.ChannelUtils.writeHttpResponse) Map(java.util.Map) HandlerKey(com.wavefront.agent.handlers.HandlerKey) CharsetUtil(io.netty.util.CharsetUtil) ReportPoint(wavefront.report.ReportPoint) JsonNode(com.fasterxml.jackson.databind.JsonNode) Clock(com.wavefront.common.Clock) URI(java.net.URI) JsonMetricsParser(com.wavefront.metrics.JsonMetricsParser) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) Set(java.util.Set) IOException(java.io.IOException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ReportableEntityType(com.wavefront.data.ReportableEntityType) List(java.util.List) ReportableEntityHandlerFactory(com.wavefront.agent.handlers.ReportableEntityHandlerFactory) Pair(com.wavefront.common.Pair) ChannelHandler(io.netty.channel.ChannelHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URI(java.net.URI) ReportPoint(wavefront.report.ReportPoint) Pair(com.wavefront.common.Pair)

Aggregations

Pair (com.wavefront.common.Pair)3 IOException (java.io.IOException)2 Map (java.util.Map)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 QueueFile (com.squareup.tape2.QueueFile)1 TokenAuthenticator (com.wavefront.agent.auth.TokenAuthenticator)1 ChannelUtils.writeHttpResponse (com.wavefront.agent.channel.ChannelUtils.writeHttpResponse)1 HealthCheckManager (com.wavefront.agent.channel.HealthCheckManager)1 HandlerKey (com.wavefront.agent.handlers.HandlerKey)1 ReportableEntityHandler (com.wavefront.agent.handlers.ReportableEntityHandler)1 ReportableEntityHandlerFactory (com.wavefront.agent.handlers.ReportableEntityHandlerFactory)1 ReportableEntityPreprocessor (com.wavefront.agent.preprocessor.ReportableEntityPreprocessor)1