Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit d5a1d20

Browse files
committed
Fix dependencies, clean up sample
1 parent 0e3c6cf commit d5a1d20

4 files changed

Lines changed: 63 additions & 71 deletions

File tree

pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@
6767
<scope>import</scope>
6868
</dependency>
6969

70-
<dependency>
71-
<groupId>io.opentelemetry</groupId>
72-
<artifactId>opentelemetry-bom</artifactId>
73-
<version>1.48.0</version>
74-
<type>pom</type>
75-
<scope>import</scope>
76-
</dependency>
77-
7870
<!-- Used for BQ Storage APIs -->
7971
<dependency>
8072
<groupId>com.google.cloud</groupId>

samples/snippets/pom.xml

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,6 @@
5151
<type>pom</type>
5252
<scope>import</scope>
5353
</dependency>
54-
55-
<dependency>
56-
<groupId>io.opentelemetry</groupId>
57-
<artifactId>opentelemetry-api</artifactId>
58-
<version>1.46.0</version>
59-
</dependency>
60-
<dependency>
61-
<groupId>io.opentelemetry</groupId>
62-
<artifactId>opentelemetry-context</artifactId>
63-
<version>1.46.0</version>
64-
</dependency>
65-
<dependency>
66-
<groupId>io.opentelemetry</groupId>
67-
<artifactId>opentelemetry-sdk</artifactId>
68-
<version>1.46.0</version>
69-
</dependency>
70-
<dependency>
71-
<groupId>io.opentelemetry</groupId>
72-
<artifactId>opentelemetry-sdk-common</artifactId>
73-
<version>1.46.0</version>
74-
</dependency>
75-
<dependency>
76-
<groupId>io.opentelemetry</groupId>
77-
<artifactId>opentelemetry-sdk-trace</artifactId>
78-
<version>1.46.0</version>
79-
</dependency>
80-
8154
</dependencies>
8255
</dependencyManagement>
8356

@@ -86,6 +59,31 @@
8659
<groupId>com.google.cloud</groupId>
8760
<artifactId>google-cloud-bigquery</artifactId>
8861
</dependency>
62+
<dependency>
63+
<groupId>io.opentelemetry</groupId>
64+
<artifactId>opentelemetry-api</artifactId>
65+
<version>1.46.0</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>io.opentelemetry</groupId>
69+
<artifactId>opentelemetry-context</artifactId>
70+
<version>1.46.0</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>io.opentelemetry</groupId>
74+
<artifactId>opentelemetry-sdk</artifactId>
75+
<version>1.46.0</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>io.opentelemetry</groupId>
79+
<artifactId>opentelemetry-sdk-common</artifactId>
80+
<version>1.46.0</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>io.opentelemetry</groupId>
84+
<artifactId>opentelemetry-sdk-trace</artifactId>
85+
<version>1.46.0</version>
86+
</dependency>
8987
<!-- [END bigquery_java_dependencies] -->
9088
<!-- [END bigquery_install_with_bom] -->
9189

samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpan.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
import java.util.Map;
3838

3939
public 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")

samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpanIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void testCreateJob() {
5454
"Sample Test Tracer");
5555

5656
assertThat(bout.toString()).contains("Parent Span was captured!");
57-
assertThat(bout.toString()).contains("Child Span was captured!");
58-
assertThat(bout.toString()).contains("Sample Child Span is the child of Sample Parent Span!");
57+
assertThat(bout.toString()).contains("createDataset is the child of Sample Parent Span!");
5958
}
6059
}

0 commit comments

Comments
 (0)