1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.settings;
  27 
  28 import jdk.jfr.Description;
  29 import jdk.jfr.Event;
  30 import jdk.jfr.Label;
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.SettingDefinition;
  33 import jdk.testlibrary.jfr.Events;
  34 
  35 import static jdk.testlibrary.Asserts.assertEquals;
  36 
  37 /*
  38  * @test
  39  * @summary The test uses SettingControl
  40  * @key jfr
  41  * @library /lib/testlibrary
  42  * @build jdk.jfr.api.settings.RegExpControl
  43  * @build jdk.jfr.api.settings.StringListSetting
  44  * @run main/othervm jdk.jfr.api.settings.TestFilterEvents
  45  */
  46 public class TestFilterEvents {
  47 
  48     private static class AbstractHTTPEvent extends Event {
  49         @Label("HTTP URI")
  50         protected String uri;
  51 
  52         @Label("URI Filter")
  53         @SettingDefinition
  54         protected boolean uriFilter(RegExpControl control) {
  55             return control.matches(uri);
  56         }
  57     }
  58 
  59     private static final class HTTPGetEvent extends AbstractHTTPEvent {
  60         @Label("Thread Names")
  61         @Description("List of thread names to accept, such as \"main\" or \"workerThread1\", \"taskThread\"")
  62         @SettingDefinition
  63         private boolean threadNames(StringListSetting setting) {
  64             return setting.accept(Thread.currentThread().getName());
  65         }
  66 
  67     }
  68     private static final class HTTPPostEvent extends AbstractHTTPEvent {
  69     }
  70 
  71     public static void main(String[] args) throws Exception {
  72         Recording continuous = new Recording();
  73         continuous.enable(HTTPGetEvent.class).with("threadNames", "\"unused-threadname-1\"");
  74         assertEquals(0, makeProfilingRecording("\"unused-threadname-2\""));
  75         assertEquals(1, makeProfilingRecording("\"" + Thread.currentThread().getName() + "\""));
  76         continuous.close();
  77     }
  78 
  79     private static int makeProfilingRecording(String threadNames) throws Exception {
  80         try (Recording recording = new Recording()) {
  81             recording.enable(HTTPGetEvent.class).with("threadNames", threadNames);
  82             recording.enable(HTTPGetEvent.class).with("uriFilter", "https://www.example.com/list/.*");
  83             recording.enable(HTTPPostEvent.class).with("uriFilter", "https://www.example.com/list/.*");
  84             recording.start();
  85 
  86             HTTPGetEvent getEvent = new HTTPGetEvent();
  87             getEvent.uri = "https://www.example.com/list/item?id=4";
  88             getEvent.commit();
  89 
  90             HTTPPostEvent postEvent = new HTTPPostEvent();
  91             postEvent.uri = "https://www.example.com/admin/login?name=john";
  92             postEvent.commit();
  93 
  94             recording.stop();
  95 
  96             return Events.fromRecording(recording).size();
  97         }
  98     }
  99 
 100 }