|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.applicationinsights.diagnostics.jfr; |
| 5 | + |
| 6 | +import com.azure.json.JsonReader; |
| 7 | +import com.azure.json.JsonSerializable; |
| 8 | +import com.azure.json.JsonToken; |
| 9 | +import com.azure.json.JsonWriter; |
| 10 | +import java.io.IOException; |
| 11 | +import jdk.jfr.Category; |
| 12 | +import jdk.jfr.Description; |
| 13 | +import jdk.jfr.Event; |
| 14 | +import jdk.jfr.Label; |
| 15 | +import jdk.jfr.Name; |
| 16 | +import jdk.jfr.Period; |
| 17 | +import jdk.jfr.StackTrace; |
| 18 | + |
| 19 | +/** |
| 20 | + * Machine-level diagnostic event. |
| 21 | + * |
| 22 | + * <p><b>Compatibility contract.</b> The JFR wire event name ({@link #NAME}) is an explicitly |
| 23 | + * versioned identifier. It was renamed from {@link #LEGACY_NAME} ("MachineStats") to "MachineInfo". |
| 24 | + * Because the event name is the key by which a reader locates the event, changing it is a |
| 25 | + * compatibility boundary that {@link #SCHEMA_VERSION} (payload versioning) cannot bridge: |
| 26 | + * |
| 27 | + * <ul> |
| 28 | + * <li><b>Read side (backward compatibility):</b> readers must look up {@link #NAME} and fall back |
| 29 | + * to {@link #LEGACY_NAME} so that recordings produced by older agents (which carry a |
| 30 | + * "MachineStats" event) can still be discovered and scored. |
| 31 | + * <li><b>Write side (forward compatibility):</b> agents at this version and later emit only |
| 32 | + * {@link #NAME}. Consumers/backends and any custom JFC files must therefore be updated to |
| 33 | + * recognize "MachineInfo"; older consumers that only know "MachineStats" will not find the |
| 34 | + * event. This is a deliberate, documented minimum-consumer-version requirement rather than a |
| 35 | + * silent change. |
| 36 | + * </ul> |
| 37 | + * |
| 38 | + * <p>Once the write side no longer needs to interoperate with the old name, {@link #LEGACY_NAME} |
| 39 | + * and its read-side fallback can be removed. |
| 40 | + */ |
| 41 | +@SuppressWarnings("Java8ApiChecker") // JFR APIs require Java 11+, but agent targets Java 8 bytecode |
| 42 | +@Name("com.microsoft.applicationinsights.diagnostics.jfr.MachineInfo") |
| 43 | +@Label("MachineInfo") |
| 44 | +@Category("Diagnostic") |
| 45 | +@Description("MachineInfo") |
| 46 | +@StackTrace(false) |
| 47 | +@Period("beginChunk") |
| 48 | +public class MachineInfo extends Event implements JsonSerializable<MachineInfo> { |
| 49 | + public static final String NAME = "com.microsoft.applicationinsights.diagnostics.jfr.MachineInfo"; |
| 50 | + |
| 51 | + /** |
| 52 | + * Event name emitted by agents prior to the MachineStats->MachineInfo rename. Readers fall |
| 53 | + * back to this so previously-recorded recordings (which carry a "MachineStats" event) can still |
| 54 | + * be located and scored. |
| 55 | + */ |
| 56 | + public static final String LEGACY_NAME = |
| 57 | + "com.microsoft.applicationinsights.diagnostics.jfr.MachineStats"; |
| 58 | + |
| 59 | + /** |
| 60 | + * Current schema version. Version 2 drops the legacy {@code contextSwitchesPerMs} field; |
| 61 | + * recordings produced before the schemaVersion field was added carry schemaVersion 1 (the |
| 62 | + * implicit legacy version) and still serialize {@code contextSwitchesPerMs}. |
| 63 | + */ |
| 64 | + public static final int SCHEMA_VERSION = 2; |
| 65 | + |
| 66 | + private int coreCount; |
| 67 | + |
| 68 | + private int schemaVersion; |
| 69 | + |
| 70 | + public int getCoreCount() { |
| 71 | + return coreCount; |
| 72 | + } |
| 73 | + |
| 74 | + public MachineInfo setCoreCount(int coreCount) { |
| 75 | + this.coreCount = coreCount; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public int getSchemaVersion() { |
| 80 | + return schemaVersion; |
| 81 | + } |
| 82 | + |
| 83 | + public MachineInfo setSchemaVersion(int schemaVersion) { |
| 84 | + this.schemaVersion = schemaVersion; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { |
| 90 | + jsonWriter.writeStartObject(); |
| 91 | + return jsonWriter |
| 92 | + .writeIntField("coreCount", coreCount) |
| 93 | + .writeIntField("schemaVersion", schemaVersion) |
| 94 | + .writeEndObject(); |
| 95 | + } |
| 96 | + |
| 97 | + public static MachineInfo fromJson(JsonReader jsonReader) throws IOException { |
| 98 | + return jsonReader.readObject( |
| 99 | + reader -> { |
| 100 | + MachineInfo deserializedValue = new MachineInfo(); |
| 101 | + |
| 102 | + while (reader.nextToken() != JsonToken.END_OBJECT) { |
| 103 | + String fieldName = reader.getFieldName(); |
| 104 | + reader.nextToken(); |
| 105 | + if ("coreCount".equals(fieldName)) { |
| 106 | + deserializedValue.setCoreCount(reader.getInt()); |
| 107 | + } else if ("schemaVersion".equals(fieldName)) { |
| 108 | + deserializedValue.setSchemaVersion(reader.getInt()); |
| 109 | + } else { |
| 110 | + reader.skipChildren(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return deserializedValue; |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments