3737import java .util .Map ;
3838
3939public class EnableOpenTelemetryTracingWithParentSpan {
40-
41- // Data structures for captured Span data
40+ // Maps Span names to their attribute maps.
4241 private static final Map <String , Map <AttributeKey <?>, Object >> OTEL_ATTRIBUTES =
4342 new HashMap <String , Map <AttributeKey <?>, Object >>();
43+ // Maps Span names to their parent Span IDs.
4444 private static final Map <String , String > OTEL_PARENT_SPAN_IDS = new HashMap <>();
45+ // Maps Span IDs to their Span names.
4546 private static final Map <String , String > OTEL_SPAN_IDS_TO_NAMES = new HashMap <>();
4647
4748 // Create a SpanExporter to determine how to handle captured Span data.
@@ -51,7 +52,7 @@ private static class SampleSpanExporter
5152 @ Override
5253 public CompletableResultCode export (Collection <SpanData > collection ) {
5354 // Export data. This data can be sent out of process via netowork calls, though
54- // for this example a local map is used.
55+ // for this example local data structures are used.
5556 if (collection .isEmpty ()) {
5657 // No span data was collected.
5758 return CompletableResultCode .ofFailure ();
@@ -80,7 +81,8 @@ public CompletableResultCode shutdown() {
8081 }
8182
8283 public static void main (String [] args ) {
83- enableOpenTelemetryWithParentSpan ("Sample Tracer" );
84+ final String tracerName = "Sample Tracer" ;
85+ enableOpenTelemetryWithParentSpan (tracerName );
8486 }
8587
8688 public static void enableOpenTelemetryWithParentSpan (String tracerName ) {
@@ -109,56 +111,57 @@ public static void enableOpenTelemetryWithParentSpan(String tracerName) {
109111 .build ();
110112 BigQuery bigquery = otelOptions .getService ();
111113
112- // Create the root parent Span. setNoParent() ensures that it is a parent Span.
113114 // TODO(developer): Replace Span and attribute names.
115+ final String parentSpanName = "Sample Parent Span" ;
116+ final String attributeKey = "sample-parent-attribute" ;
117+ final String attributeValue = "sample-parent-value" ;
118+ final String datasetId = "sampleDatasetId" ;
119+
120+ // Create the root parent Span. setNoParent() ensures that it is a parent Span with a Span ID
121+ // of 0.
114122 Span parentSpan =
115123 tracer
116- .spanBuilder ("Sample Parent Span" )
124+ .spanBuilder (parentSpanName )
117125 .setNoParent ()
118- .setAttribute ("sample-parent-attribute" , "sample-parent-value" )
126+ .setAttribute (attributeKey , attributeValue )
119127 .startSpan ();
120128
121- // Wrap nested functions in try-catch-finally block to pass on the Span Context.
129+ // The Span Context is automatically passed on to any functions called within the scope of the
130+ // try block. parentSpan.makeCurrent() sets parentSpan to be the parent of any Spans created in
131+ // this scope, or the scope of any functions called within this scope.
122132 try (Scope parentScope = parentSpan .makeCurrent ()) {
123- createDataset (bigquery , tracer , "sample-dataset-id" );
133+ DatasetInfo info = DatasetInfo .newBuilder (datasetId ).build ();
134+ Dataset dataset = bigquery .create (info );
124135 } finally {
125136 // finally block ensures that Spans are cleaned up properly.
126137 parentSpan .end ();
127138
128- if (OTEL_ATTRIBUTES
129- .get ("Sample Parent Span" )
130- .get (AttributeKey .stringKey ("sample-parent-attribute" ))
131- == "sample-parent-value" ) {
139+ // Unpack attribute maps to get attribute keys and values.
140+ Map <AttributeKey <?>, Object > parentSpanAttributes = OTEL_ATTRIBUTES .get (parentSpanName );
141+ Object parentSpanAttributeValue =
142+ parentSpanAttributes .get (AttributeKey .stringKey (attributeKey ));
143+ if (parentSpanAttributeValue == attributeValue ) {
132144 System .out .println ("Parent Span was captured!" );
133145 } else {
134146 System .out .println ("Parent Span was not captured!" );
135147 }
136- if (OTEL_ATTRIBUTES
137- .get ("Sample Child Span" )
138- .get (AttributeKey .stringKey ("sample-child-attribute" ))
139- == "sample-child-value" ) {
140- System .out .println ("Child Span was captured!" );
141- } else {
142- System .out .println ("Child Span was not captured!" );
143- }
144- if (OTEL_ATTRIBUTES
145- .get ("Sample Child Span" )
146- .get (AttributeKey .stringKey ("sample-child-attribute" ))
147- == "sample-child-value" ) {
148- System .out .println ("Child Span was captured!" );
149- } else {
150- System .out .println ("Child Span was not captured!" );
151- }
152- String childSpanParentId = OTEL_PARENT_SPAN_IDS .get ("Sample Child Span" );
148+
149+ String childSpanParentId =
150+ OTEL_PARENT_SPAN_IDS .get ("com.google.cloud.bigquery.BigQuery.createDataset" );
153151 String parentSpanId = OTEL_SPAN_IDS_TO_NAMES .get (childSpanParentId );
154- if (parentSpanId == "Sample Parent Span" ) {
155- System .out .println ("Sample Child Span is the child of Sample Parent Span!" );
152+ if (OTEL_SPAN_IDS_TO_NAMES .get (childSpanParentId ) == parentSpanName ) {
153+ System .out .println ("createDataset is the child of Sample Parent Span!" );
154+ } else {
155+ System .out .println ("createDataset is not the child of Parent!" );
156156 }
157+
158+ bigquery .delete (datasetId );
157159 }
158160 }
159161
160- public static void createDataset (BigQuery bigquery , Tracer tracer , String datasetId ) {
161- // Parent Span Context is passed on here.
162+ public static void createDataset (BigQuery bigquery , String datasetId , Tracer tracer ) {
163+ // Parent Span Context is automatically passed on here. childSpan's parent Span is set to be
164+ // parentSpan.
162165 Span childSpan =
163166 tracer
164167 .spanBuilder ("Sample Child Span" )
0 commit comments