wyhaines / opentelemetry-api.cr

The core of open telemetry instrumentation is the OpenTelemetry API/SDK. The initial aim of this shard is to implement the OpenTelemetry specification for metrics, traces, and logs.
Apache License 2.0
12 stars 1 forks source link

Json format for STDOUT output #13

Closed miry closed 2 years ago

miry commented 2 years ago

Here is some small idention problem.

Click to expand! ```json { "type":"trace", "traceId":"10ea1bd0000eda23489452a25cf1343f", "resource":{ "service.name":"it_is_not_used", "service.version":"0.1.0", "service.instance.id":"000eda23-4894-10e6-a900-8fb10dfa68c4", "telemetry.sdk.name":"opentelemetry", "telemetry.sdk.language":"crystal", "telemetry.sdk.version":"0.3.0" }, "spans":[ { "type":"span", "traceId":"10ea1bd0000eda23489452a25cf1343f", "spanId":"0eda234895000001", "parentSpanId":"", "kind":1, "name":"request", "status":{ "code": 0, "message": "" }, "startTime":1653690773366256128, "endTime":1653690773366308096, "attributes":{ "verb":"GET", "url":"http://example.com/foo" }, "events":[ { "type": "event", "name": "dispatching to handler", "timestamp": 1653690773366297088 } ] }, { "type":"span", "traceId":"10ea1bd0000eda23489452a25cf1343f", "spanId":"0eda234895000002", "parentSpanId":"0eda234895000001", "kind":1, "name":"handler", "status":{ "code": 0, "message": "" }, "startTime":1653690773366298112, "endTime":1653690773366308096, "attributes":{ }, "events":[ { "type": "event", "name": "handling request", "timestamp": 1653690773366304000 } ] }, { "type":"span", "traceId":"10ea1bd0000eda23489452a25cf1343f", "spanId":"0eda234895000003", "parentSpanId":"0eda234895000002", "kind":1, "name":"db", "status":{ "code": 0, "message": "" }, "startTime":1653690773366305024, "endTime":1653690773366307072, "attributes":{ }, "events":[ { "type": "event", "name": "querying database", "timestamp": 1653690773366307072 } ] } ] } ```

Changed to

Click to expand propose output! ```json { "type":"trace", "traceId":"02443728000eda234bb12b1c181a0169", "resource":{ "service.name":"it_is_not_used", "service.version":"0.1.0", "service.instance.id":"000eda23-4bb1-0240-b8a0-c4e5df553628", "telemetry.sdk.name":"opentelemetry", "telemetry.sdk.language":"crystal", "telemetry.sdk.version":"0.3.0" }, "spans":[ { "type":"span", "traceId":"02443728000eda234bb12b1c181a0169", "spanId":"0eda234bb2000001", "parentSpanId":"", "kind":1, "name":"request", "status":{"code":0,"message":""}, "startTime":1653691570165433088, "endTime":1653691570165487104, "attributes":{ "verb":"GET", "url":"http://example.com/foo" }, "events":[ { "type": "event", "name": "dispatching to handler", "timestamp": 1653691570165476096 } ] }, { "type":"span", "traceId":"02443728000eda234bb12b1c181a0169", "spanId":"0eda234bb2000002", "parentSpanId":"0eda234bb2000001", "kind":1, "name":"handler", "status":{"code":0,"message":""}, "startTime":1653691570165477120, "endTime":1653691570165487104, "events":[ { "type": "event", "name": "handling request", "timestamp": 1653691570165483008 } ] }, { "type":"span", "traceId":"02443728000eda234bb12b1c181a0169", "spanId":"0eda234bb2000003", "parentSpanId":"0eda234bb2000002", "kind":1, "name":"db", "status":{"code":0,"message":""}, "startTime":1653691570165484032, "endTime":1653691570165486080, "events":[ { "type": "event", "name": "querying database", "timestamp": 1653691570165486080 } ] } ] } ```
wyhaines commented 2 years ago

Thank you for those corrections. The original code examples were actually written before the library worked -- they were a sort of written specification about how I wanted it to work, and I guess I never noticed that I deviated a little from it. I appreciate that you caught that!

miry commented 2 years ago

@wyhaines I have not rewriten, as it is valid JSON, and works fine. There is also the way to use JSON.build in future: https://crystal-lang.org/api/1.4.1/JSON.html#build%28indent%3Dnil%2C%26%29-class-method

wyhaines commented 2 years ago

I have refactored the code to use JSON::Builder. The output is prettier.