Skip to content

Commit fcc2d96

Browse files
committed
Add sample for calling Stackdriver Trace API
1 parent ed9ebd5 commit fcc2d96

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>samples</artifactId>
7+
<groupId>com.google.cloud.trace</groupId>
8+
<version>0.2.3-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stackdriver-trace-api</artifactId>
13+
<name>Google Cloud Trace Java SDK Stackdriver Trace API example</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>trace-grpc-api-service</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>${project.groupId}</groupId>
23+
<artifactId>core</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.google.auth</groupId>
28+
<artifactId>google-auth-library-oauth2-http</artifactId>
29+
<version>0.4.0</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.grpc</groupId>
33+
<artifactId>grpc-netty</artifactId>
34+
<version>1.0.1</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.netty</groupId>
38+
<artifactId>netty-tcnative-boringssl-static</artifactId>
39+
<version>1.1.33.Fork23</version>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.google.cloud.trace.samples.stackdriver.api;
2+
3+
import com.google.auth.oauth2.GoogleCredentials;
4+
import com.google.cloud.trace.Trace;
5+
import com.google.cloud.trace.Tracer;
6+
import com.google.cloud.trace.core.TraceContext;
7+
import com.google.cloud.trace.service.TraceGrpcApiService;
8+
import com.google.cloud.trace.service.TraceService;
9+
import java.io.FileInputStream;
10+
import java.io.IOException;
11+
12+
public class StackdriverTraceApi {
13+
public static void main(String[] args) throws IOException {
14+
// Initialize the Tracer.
15+
TraceService traceService = TraceGrpcApiService.builder()
16+
// Set the projectId.
17+
.setProjectId("my-project-id")
18+
// Uncomment this if you want to provide your own credentials for the Stackdriver Trace API.
19+
// On GCE, this is optional because the Application Default Credentials are used by default.
20+
//.setCredentials(
21+
// GoogleCredentials.fromStream(new FileInputStream("/path/to/my/credentials.json")))
22+
// Use a short delay of 1 second for this example. In production, you may want to use a
23+
// higher value so that more trace events are batched together in a single request to the
24+
// Stackdriver Trace API. By default, the delay is 15 seconds.
25+
.setScheduledDelay(1)
26+
.build();
27+
Trace.init(traceService);
28+
29+
Tracer tracer = Trace.getTracer();
30+
TraceContext context = tracer.startSpan("test-span");
31+
tracer.endSpan(context);
32+
}
33+
}

0 commit comments

Comments
 (0)