|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.exporter.otlp.profiles.jfr; |
| 7 | + |
| 8 | +import io.opentelemetry.exporter.otlp.internal.data.ImmutableFunctionData; |
| 9 | +import io.opentelemetry.exporter.otlp.internal.data.ImmutableLineData; |
| 10 | +import io.opentelemetry.exporter.otlp.internal.data.ImmutableLocationData; |
| 11 | +import io.opentelemetry.exporter.otlp.internal.data.ImmutableStackData; |
| 12 | +import io.opentelemetry.exporter.otlp.profiles.FunctionData; |
| 13 | +import io.opentelemetry.exporter.otlp.profiles.LineData; |
| 14 | +import io.opentelemetry.exporter.otlp.profiles.LocationData; |
| 15 | +import io.opentelemetry.exporter.otlp.profiles.ProfilesDictionaryCompositor; |
| 16 | +import io.opentelemetry.exporter.otlp.profiles.StackData; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import jdk.jfr.consumer.RecordedFrame; |
| 20 | + |
| 21 | +/** |
| 22 | + * Allows for the conversion and storage of JFR thread stacks in the dictionary encoding structure |
| 23 | + * used by OTLP profile signal exporters. |
| 24 | + * |
| 25 | + * <p>The compositor resembles a builder, though without the fluent API. Instead, mutation methods |
| 26 | + * return the index of the offered element, this information being required to construct any element |
| 27 | + * that references into the tables. |
| 28 | + * |
| 29 | + * <p>This class is not threadsafe and must be externally synchronized. |
| 30 | + */ |
| 31 | +public class JfrLocationDataCompositor { |
| 32 | + |
| 33 | + private final ProfilesDictionaryCompositor profilesDictionaryCompositor; |
| 34 | + |
| 35 | + /** |
| 36 | + * Wrap the given dictionary with additional JFR-specific stack data handling functionality. |
| 37 | + * |
| 38 | + * @param profilesDictionaryCompositor the underlying storage. |
| 39 | + */ |
| 40 | + public JfrLocationDataCompositor(ProfilesDictionaryCompositor profilesDictionaryCompositor) { |
| 41 | + this.profilesDictionaryCompositor = profilesDictionaryCompositor; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Stores the provided list of frames as a StackData element in the dictionary if an equivalent is |
| 46 | + * not already present, and returns its index. |
| 47 | + * |
| 48 | + * @param frameList the JFR stack data. |
| 49 | + * @return the index of the added or existing StackData element. |
| 50 | + */ |
| 51 | + public int putIfAbsent(List<RecordedFrame> frameList) { |
| 52 | + |
| 53 | + List<Integer> locationIndices = frameList.stream().map(this::frameToLocation).toList(); |
| 54 | + |
| 55 | + StackData stackData = ImmutableStackData.create(locationIndices); |
| 56 | + int stackIndex = profilesDictionaryCompositor.putIfAbsent(stackData); |
| 57 | + return stackIndex; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Convert a single frame of a stack to a LocationData, store it and its components in the |
| 62 | + * dictionary and return its index. |
| 63 | + * |
| 64 | + * @param frame the source data |
| 65 | + * @return the LocationData storage index in the dictionary |
| 66 | + */ |
| 67 | + protected int frameToLocation(RecordedFrame frame) { |
| 68 | + |
| 69 | + // the LocationData references several components which need creating and placing in their |
| 70 | + // respective dictionary tables |
| 71 | + |
| 72 | + String name = nameFrom(frame); |
| 73 | + int nameStringIndex = profilesDictionaryCompositor.putIfAbsent(name); |
| 74 | + |
| 75 | + FunctionData functionData = ImmutableFunctionData.create(nameStringIndex, 0, 0, 0); |
| 76 | + int functionIndex = profilesDictionaryCompositor.putIfAbsent(functionData); |
| 77 | + |
| 78 | + int lineNumber = frame.getLineNumber() != -1 ? frame.getLineNumber() : 0; |
| 79 | + LineData lineData = ImmutableLineData.create(functionIndex, lineNumber, 0); |
| 80 | + |
| 81 | + LocationData locationData = |
| 82 | + ImmutableLocationData.create(0, 0, List.of(lineData), Collections.emptyList()); |
| 83 | + |
| 84 | + int locationIndex = profilesDictionaryCompositor.putIfAbsent(locationData); |
| 85 | + return locationIndex; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Construct a name String from the frame. Note that the wire spec and semantic conventions don't |
| 90 | + * define a specific string format. Override this method to customize the conversion. |
| 91 | + * |
| 92 | + * @param frame the JFR frame data. |
| 93 | + * @return the name as a String. |
| 94 | + */ |
| 95 | + protected String nameFrom(RecordedFrame frame) { |
| 96 | + String name = frame.getMethod().getType() != null ? frame.getMethod().getType().getName() : ""; |
| 97 | + name += "."; |
| 98 | + name += frame.getMethod().getName() != null ? frame.getMethod().getName() : ""; |
| 99 | + return name; |
| 100 | + } |
| 101 | +} |
0 commit comments