Created
July 9, 2020 02:12
-
-
Save codefromthecrypt/4383d8689addc2980ae969c41a29c28f to your computer and use it in GitHub Desktop.
snarking on otel's tracer v0.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.opentelemetry.common.AttributeValue; | |
import io.opentelemetry.exporters.logging.LoggingSpanExporter; | |
import io.opentelemetry.sdk.OpenTelemetrySdk; | |
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | |
import io.opentelemetry.trace.Span; | |
import io.opentelemetry.trace.Tracer; | |
// This shows how you can add attributes to a span and print it to console using Java's logger. | |
// | |
// $ java -cp snark.jar Snark | |
// Jul 09, 2020 10:09:38 AM io.opentelemetry.exporters.logging.LoggingSpanExporter export | |
// INFO: span: SpanWrapper{delegate=io.opentelemetry.sdk.trace.RecordEventsReadableSpan@42110406, resolvedLinks=[], resolvedEvents=[], attributes={test questions?=AttributeValueBooleanArray{booleanArrayValue=[false, false, true]}}, totalAttributeCount=2, totalRecordedEvents=0, status=Status{canonicalCode=OK, description=null}} | |
// | |
// If you bundle this with maven-shade-library, you'll get all 3MiB of dependencies needed to print | |
// the above, like I did. Here's what you'll get when you depend on opentelemetry-exporters-logging | |
// | |
// \- io.opentelemetry:opentelemetry-exporters-logging:jar:0.6.0:compile | |
// \- io.opentelemetry:opentelemetry-sdk:jar:0.6.0:compile | |
// +- io.opentelemetry:opentelemetry-api:jar:0.6.0:compile | |
// | \- io.opentelemetry:opentelemetry-context-prop:jar:0.6.0:compile | |
// | \- io.grpc:grpc-context:jar:1.28.0:compile | |
// \- com.google.guava:guava:jar:28.2-android:runtime | |
// +- com.google.guava:failureaccess:jar:1.0.1:runtime | |
// +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:runtime | |
// +- com.google.code.findbugs:jsr305:jar:3.0.2:runtime | |
// +- org.checkerframework:checker-compat-qual:jar:2.5.5:runtime | |
// +- com.google.errorprone:error_prone_annotations:jar:2.3.4:runtime | |
// \- com.google.j2objc:j2objc-annotations:jar:1.3:runtime | |
// | |
// Here goes.. | |
public class Snark { | |
// Some jokes are about "sdk" types and some will say .. oh just use api.. | |
// | |
// I dare you to find any project that exclusively depends on the "api" and can do anything. | |
// I double-dare you to find a second impl of the api | |
// | |
// meaning the SDK classes can be assumed present | |
public static void main(String[] args) { | |
// hmm static registries.. can't find an example of a not-static registry. | |
OpenTelemetrySdk.getTracerProvider() | |
// I assume this will just change whatever global there is. Handy this is exposed as I was | |
// thinking I can add a processor per-request? | |
.addSpanProcessor(SimpleSpanProcessor.newBuilder( | |
// this is for when you want to see hashCodes of java types in log files... | |
// even if this stopped being lazy and used a non-generated toString, something helpful | |
// like rendering in json might be thwarted due to the intentional inclusion of various | |
// 64-bit numeric types (like both long and double and.. heh.. arrays of them!) | |
new LoggingSpanExporter() | |
).build()); | |
// addSpanProcessor isn't chained, which is ok I guess as it shouldn't be an api anyway | |
// odd that I had to change this from TracerSdk to Tracer. Why would you covariantly return | |
// a package private type? annoying.. | |
Tracer tracer = | |
OpenTelemetrySdk.getTracerProvider().get("we have to name tracers.. ok"); | |
// something normalish whew.. | |
Span span = tracer.spanBuilder("snark attack").startSpan(); | |
span.setAttribute("string", "sheesh"); // not chained so we can practice semi-colons | |
// null allowed because we like to pretend you can win a race. Also allows us to force a cast | |
span.setAttribute("string", (String) null); | |
// this is one of the more hilarious api choices. what ... tag.. would .. be ..? | |
span.setAttribute("test questions?", AttributeValue.arrayAttributeValue(false, false, true)); | |
span.end(); // start/finish begin/end.. false dichotomy! start/end | |
// because shutting down things we didn't start is good | |
OpenTelemetrySdk.getTracerProvider().shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment