1 /* 2 * Copyright (c) 2018, 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.test.lib.jfr.Events; 34 35 import static jdk.test.lib.Asserts.assertEquals; 36 37 /** 38 * @test 39 * @summary The test uses SettingControl 40 * @key jfr 41 * @requires vm.hasJFR 42 * @library /test/lib /test/jdk 43 * @run main/othervm jdk.jfr.api.settings.TestFilterEvents 44 */ 45 public class TestFilterEvents { 46 47 private static class AbstractHTTPEvent extends Event { 48 @Label("HTTP URI") 49 protected String uri; 50 51 @Label("URI Filter") 52 @SettingDefinition 53 protected boolean uriFilter(RegExpControl control) { 54 return control.matches(uri); 55 } 56 } 57 58 private static final class HTTPGetEvent extends AbstractHTTPEvent { 59 @Label("Thread Names") 60 @Description("List of thread names to accept, such as \"main\" or \"workerThread1\", \"taskThread\"") 61 @SettingDefinition 62 private boolean threadNames(StringListSetting setting) { 63 return setting.accept(Thread.currentThread().getName()); 64 } 65 66 } 67 private static final class HTTPPostEvent extends AbstractHTTPEvent { 68 } 69 70 public static void main(String[] args) throws Exception { 71 Recording continuous = new Recording(); 72 continuous.enable(HTTPGetEvent.class).with("threadNames", "\"unused-threadname-1\""); 73 assertEquals(0, makeProfilingRecording("\"unused-threadname-2\"")); 74 assertEquals(1, makeProfilingRecording("\"" + Thread.currentThread().getName() + "\"")); 75 continuous.close(); 76 } 77 78 private static int makeProfilingRecording(String threadNames) throws Exception { 79 try (Recording recording = new Recording()) { 80 recording.enable(HTTPGetEvent.class).with("threadNames", threadNames); 81 recording.enable(HTTPGetEvent.class).with("uriFilter", "https://www.example.com/list/.*"); 82 recording.enable(HTTPPostEvent.class).with("uriFilter", "https://www.example.com/list/.*"); 83 recording.start(); 84 85 HTTPGetEvent getEvent = new HTTPGetEvent(); 86 getEvent.uri = "https://www.example.com/list/item?id=4"; 87 getEvent.commit(); 88 89 HTTPPostEvent postEvent = new HTTPPostEvent(); 90 postEvent.uri = "https://www.example.com/admin/login?name=john"; 91 postEvent.commit(); 92 93 recording.stop(); 94 95 return Events.fromRecording(recording).size(); 96 } 97 } 98 99 }