|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +package example1; |
| 26 | + |
| 27 | +import jdk.jfr.AnnotationElement; |
| 28 | +import jdk.jfr.ValueDescriptor; |
| 29 | +import jdk.jfr.EventFactory; |
| 30 | +import jdk.jfr.Event; |
| 31 | +import jdk.jfr.Name; |
| 32 | +import jdk.jfr.Label; |
| 33 | +import jdk.jfr.Description; |
| 34 | +import jdk.jfr.Category; |
| 35 | +import jdk.jfr.Recording; |
| 36 | +import jdk.jfr.MetadataDefinition; |
| 37 | +import jdk.jfr.Relational; |
| 38 | +import jdk.jfr.consumer.RecordingFile; |
| 39 | +import jdk.jfr.Configuration; |
| 40 | +import jdk.jfr.SettingDefinition; |
| 41 | +import jdk.jfr.SettingControl; |
| 42 | +import jdk.jfr.FlightRecorder; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.nio.file.Files; |
| 46 | +import java.nio.file.Path; |
| 47 | +import java.nio.file.Paths; |
| 48 | +import java.time.Duration; |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.Collections; |
| 51 | +import java.util.List; |
| 52 | +import java.util.Set; |
| 53 | +import java.util.regex.Pattern; |
| 54 | +import java.util.stream.Collectors; |
| 55 | +import java.lang.annotation.Retention; |
| 56 | +import java.lang.annotation.RetentionPolicy; |
| 57 | +import java.lang.annotation.Target; |
| 58 | +import java.lang.annotation.ElementType; |
| 59 | + |
| 60 | +public class Snippets { |
| 61 | + |
| 62 | + void AnnotationElementOverview() { |
| 63 | + // @start region="AnnotationElementOverview" |
| 64 | + List<AnnotationElement> typeAnnotations = new ArrayList<>(); |
| 65 | + typeAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld")); |
| 66 | + typeAnnotations.add(new AnnotationElement(Label.class, "Hello World")); |
| 67 | + typeAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started")); |
| 68 | + |
| 69 | + List<AnnotationElement> fieldAnnotations = new ArrayList<>(); |
| 70 | + fieldAnnotations.add(new AnnotationElement(Label.class, "Message")); |
| 71 | + |
| 72 | + List<ValueDescriptor> fields = new ArrayList<>(); |
| 73 | + fields.add(new ValueDescriptor(String.class, "message", fieldAnnotations)); |
| 74 | + |
| 75 | + EventFactory f = EventFactory.create(typeAnnotations, fields); |
| 76 | + Event event = f.newEvent(); |
| 77 | + event.commit(); |
| 78 | + // @end |
| 79 | + } |
| 80 | + |
| 81 | + // @start region="EventOverview" |
| 82 | + public class Example { |
| 83 | + |
| 84 | + @Label("Hello World") |
| 85 | + @Description("Helps programmer getting started") |
| 86 | + static class HelloWorld extends Event { |
| 87 | + @Label("Message") |
| 88 | + String message; |
| 89 | + } |
| 90 | + |
| 91 | + public static void main(String... args) { |
| 92 | + HelloWorld event = new HelloWorld(); |
| 93 | + event.message = "hello, world!"; |
| 94 | + event.commit(); |
| 95 | + } |
| 96 | + } |
| 97 | + // @end |
| 98 | + |
| 99 | + void EventFactoryOverview() { |
| 100 | + // @start region="EventFactoryOverview" |
| 101 | + List<ValueDescriptor> fields = new ArrayList<>(); |
| 102 | + List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message")); |
| 103 | + fields.add(new ValueDescriptor(String.class, "message", messageAnnotations)); |
| 104 | + List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number")); |
| 105 | + fields.add(new ValueDescriptor(int.class, "number", numberAnnotations)); |
| 106 | + |
| 107 | + String[] category = { "Example", "Getting Started" }; |
| 108 | + List<AnnotationElement> eventAnnotations = new ArrayList<>(); |
| 109 | + eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld")); |
| 110 | + eventAnnotations.add(new AnnotationElement(Label.class, "Hello World")); |
| 111 | + eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started")); |
| 112 | + eventAnnotations.add(new AnnotationElement(Category.class, category)); |
| 113 | + |
| 114 | + EventFactory f = EventFactory.create(eventAnnotations, fields); |
| 115 | + |
| 116 | + Event event = f.newEvent(); |
| 117 | + event.set(0, "hello, world!"); |
| 118 | + event.set(1, 4711); |
| 119 | + event.commit(); |
| 120 | + // @end |
| 121 | + } |
| 122 | + |
| 123 | + void EventSettingOverview() throws Exception { |
| 124 | + // @start region="EventSettingOverview" |
| 125 | + Recording r = new Recording(); |
| 126 | + r.enable("jdk.CPULoad") |
| 127 | + .withPeriod(Duration.ofSeconds(1)); |
| 128 | + r.enable("jdk.FileWrite") |
| 129 | + .withoutStackTrace() |
| 130 | + .withThreshold(Duration.ofNanos(10)); |
| 131 | + r.start(); |
| 132 | + Thread.sleep(10_000); |
| 133 | + r.stop(); |
| 134 | + r.dump(Files.createTempFile("recording", ".jfr")); |
| 135 | + // @end |
| 136 | + } |
| 137 | + |
| 138 | + void FlightRecorderTakeSnapshot() throws Exception { |
| 139 | + // @start region="FlightRecorderTakeSnapshot" |
| 140 | + try (Recording snapshot = FlightRecorder.getFlightRecorder().takeSnapshot()) { |
| 141 | + if (snapshot.getSize() > 0) { |
| 142 | + snapshot.setMaxSize(100_000_000); |
| 143 | + snapshot.setMaxAge(Duration.ofMinutes(5)); |
| 144 | + snapshot.dump(Paths.get("snapshot.jfr")); |
| 145 | + } |
| 146 | + } |
| 147 | + // @end |
| 148 | + } |
| 149 | + |
| 150 | + // @start region="MetadataDefinitionOverview" |
| 151 | + @MetadataDefinition |
| 152 | + @Label("Severity") |
| 153 | + @Description("Value between 0 and 100 that indicates severity. 100 is most severe.") |
| 154 | + @Retention(RetentionPolicy.RUNTIME) |
| 155 | + @Target({ ElementType.TYPE }) |
| 156 | + public @interface Severity { |
| 157 | + int value() default 50; |
| 158 | + } |
| 159 | + |
| 160 | + @MetadataDefinition |
| 161 | + @Label("Transaction Id") |
| 162 | + @Relational |
| 163 | + @Retention(RetentionPolicy.RUNTIME) |
| 164 | + @Target({ ElementType.FIELD }) |
| 165 | + public @interface TransactionId { |
| 166 | + } |
| 167 | + |
| 168 | + @Severity(80) |
| 169 | + @Label("Transaction Blocked") |
| 170 | + class TransactionBlocked extends Event { |
| 171 | + @TransactionId |
| 172 | + @Label("Transaction") |
| 173 | + long transactionId1; |
| 174 | + |
| 175 | + @TransactionId |
| 176 | + @Label("Transaction Blocker") |
| 177 | + long transactionId2; |
| 178 | + } |
| 179 | + // @end |
| 180 | + |
| 181 | + void RecordingnOverview() throws Exception { |
| 182 | + // @start region="RecordingOverview" |
| 183 | + Configuration c = Configuration.getConfiguration("default"); |
| 184 | + Recording r = new Recording(c); |
| 185 | + r.start(); |
| 186 | + System.gc(); |
| 187 | + Thread.sleep(5000); |
| 188 | + r.stop(); |
| 189 | + r.dump(Files.createTempFile("my-recording", ".jfr")); |
| 190 | + // @end |
| 191 | + } |
| 192 | + |
| 193 | + // @start region="SettingControlOverview1" |
| 194 | + final class RegExpControl extends SettingControl { |
| 195 | + private Pattern pattern = Pattern.compile(".*"); |
| 196 | + |
| 197 | + @Override |
| 198 | + public void setValue(String value) { |
| 199 | + this.pattern = Pattern.compile(value); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public String combine(Set<String> values) { |
| 204 | + return String.join("|", values); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public String getValue() { |
| 209 | + return pattern.toString(); |
| 210 | + } |
| 211 | + |
| 212 | + public boolean matches(String s) { |
| 213 | + return pattern.matcher(s).find(); |
| 214 | + } |
| 215 | + } |
| 216 | + // @end |
| 217 | + |
| 218 | + class HttpServlet { |
| 219 | + } |
| 220 | + |
| 221 | + class HttpServletRequest { |
| 222 | + public String getRequestURI() { |
| 223 | + return null; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + class HttpServletResponse { |
| 228 | + } |
| 229 | + |
| 230 | + // @start region="SettingControlOverview2" |
| 231 | + abstract class HTTPRequest extends Event { |
| 232 | + @Label("Request URI") |
| 233 | + protected String uri; |
| 234 | + |
| 235 | + @Label("Servlet URI Filter") |
| 236 | + @SettingDefinition |
| 237 | + protected boolean uriFilter(RegExpControl regExp) { |
| 238 | + return regExp.matches(uri); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + @Label("HTTP Get Request") |
| 243 | + class HTTPGetRequest extends HTTPRequest { |
| 244 | + } |
| 245 | + |
| 246 | + @Label("HTTP Post Request") |
| 247 | + class HTTPPostRequest extends HTTPRequest { |
| 248 | + } |
| 249 | + |
| 250 | + class ExampleServlet extends HttpServlet { |
| 251 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) { |
| 252 | + HTTPGetRequest request = new HTTPGetRequest(); |
| 253 | + request.begin(); |
| 254 | + request.uri = req.getRequestURI(); |
| 255 | + code: // @replace regex='code:' replacement="..." |
| 256 | + request.commit(); |
| 257 | + } |
| 258 | + |
| 259 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) { |
| 260 | + HTTPPostRequest request = new HTTPPostRequest(); |
| 261 | + request.begin(); |
| 262 | + request.uri = req.getRequestURI(); |
| 263 | + code: // @replace regex='code:' replacement="..." |
| 264 | + request.commit(); |
| 265 | + } |
| 266 | + } |
| 267 | + // @end |
| 268 | + |
| 269 | + void SettingControlOverview3() { |
| 270 | + // @start region="SettingControlOverview3" |
| 271 | + Recording r = new Recording(); |
| 272 | + r.enable("HTTPGetRequest").with("uriFilter", "https://www.example.com/list/.*"); |
| 273 | + r.enable("HTTPPostRequest").with("uriFilter", "https://www.example.com/login/.*"); |
| 274 | + r.start(); |
| 275 | + // @end |
| 276 | + } |
| 277 | + |
| 278 | + // @start region="SettingDefinitionOverview" |
| 279 | + class HelloWorld extends Event { |
| 280 | + |
| 281 | + @Label("Message") |
| 282 | + String message; |
| 283 | + |
| 284 | + @SettingDefinition |
| 285 | + @Label("Message Filter") |
| 286 | + public boolean filter(RegExpControl regExp) { |
| 287 | + return regExp.matches(message); |
| 288 | + } |
| 289 | + } |
| 290 | + // @end |
| 291 | +} |
0 commit comments