77
88import static java .nio .charset .StandardCharsets .UTF_8 ;
99
10- import com .fasterxml .jackson .databind .ObjectMapper ;
11- import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
1210import io .opentelemetry .sdk .metrics .data .MetricData ;
1311import java .io .BufferedWriter ;
1412import java .io .IOException ;
1513import java .nio .file .FileAlreadyExistsException ;
1614import java .nio .file .Files ;
1715import java .nio .file .Path ;
1816import java .nio .file .Paths ;
19- import java .util .ArrayList ;
20- import java .util .LinkedHashMap ;
21- import java .util .List ;
2217import java .util .Map ;
2318import java .util .UUID ;
2419import java .util .regex .Matcher ;
@@ -38,9 +33,6 @@ public final class MetaDataCollector {
3833 private static final Pattern MODULE_PATTERN =
3934 Pattern .compile ("(.*?/instrumentation/.*?)(/javaagent/|/library/)" );
4035
41- // thread-safe after initialization
42- private static final ObjectMapper YAML_MAPPER = new ObjectMapper (new YAMLFactory ());
43-
4436 public static void writeTelemetryToFiles (String path , Map <String , MetricData > metrics )
4537 throws IOException {
4638
@@ -68,46 +60,40 @@ private static String extractInstrumentationPath(String path) {
6860 return instrumentationPath ;
6961 }
7062
71- private static void writeMetricData (String moduleRoot , Map <String , MetricData > metrics )
63+ private static void writeMetricData (String instrumentationPath , Map <String , MetricData > metrics )
7264 throws IOException {
65+
7366 if (metrics .isEmpty ()) {
7467 return ;
7568 }
7669
77- Path output = Paths .get (moduleRoot , TMP_DIR , "metrics-" + UUID .randomUUID () + ".yaml" );
78-
79- Map <String , Object > root = new LinkedHashMap <>();
80- List <Map <String , Object >> metricList = new ArrayList <>();
81-
82- for (MetricData metric : metrics .values ()) {
83- Map <String , Object > metricNode = new LinkedHashMap <>();
84- metricNode .put ("name" , metric .getName ());
85- metricNode .put ("description" , metric .getDescription ());
86- metricNode .put ("type" , metric .getType ().toString ());
87- metricNode .put ("unit" , sanitizeUnit (metric .getUnit ()));
88-
89- List <Map <String , String >> attributes = new ArrayList <>();
90- metric .getData ().getPoints ().stream ()
91- .findFirst ()
92- .ifPresent (
93- p ->
94- p .getAttributes ()
95- .forEach (
96- (key , value ) -> {
97- Map <String , String > attr = new LinkedHashMap <>();
98- attr .put ("name" , key .getKey ());
99- attr .put ("type" , key .getType ().toString ());
100- attributes .add (attr );
101- }));
102-
103- metricNode .put ("attributes" , attributes );
104- metricList .add (metricNode );
105- }
106-
107- root .put ("metrics" , metricList );
108-
109- try (BufferedWriter writer = Files .newBufferedWriter (output , UTF_8 )) {
110- YAML_MAPPER .writeValue (writer , root );
70+ Path metricsPath =
71+ Paths .get (instrumentationPath , TMP_DIR , "metrics-" + UUID .randomUUID () + ".yaml" );
72+ try (BufferedWriter writer = Files .newBufferedWriter (metricsPath .toFile ().toPath (), UTF_8 )) {
73+
74+ if (!metrics .isEmpty ()) {
75+ writer .write ("metrics:\n " );
76+ for (MetricData metric : metrics .values ()) {
77+ writer .write (" - name: " + metric .getName () + "\n " );
78+ writer .write (" description: " + metric .getDescription () + "\n " );
79+ writer .write (" type: " + metric .getType ().toString () + "\n " );
80+ writer .write (" unit: " + sanitizeUnit (metric .getUnit ()) + "\n " );
81+ writer .write (" attributes: \n " );
82+ metric .getData ().getPoints ().stream ()
83+ .findFirst ()
84+ .get ()
85+ .getAttributes ()
86+ .forEach (
87+ (key , value ) -> {
88+ try {
89+ writer .write (" - name: " + key .getKey () + "\n " );
90+ writer .write (" type: " + key .getType ().toString () + "\n " );
91+ } catch (IOException e ) {
92+ throw new IllegalStateException (e );
93+ }
94+ });
95+ }
96+ }
11197 }
11298 }
11399
0 commit comments