@@ -22,7 +22,10 @@ import (
22
22
"log"
23
23
"log/slog"
24
24
"net"
25
+ "os"
25
26
27
+ traceapi "cloud.google.com/go/trace/apiv2"
28
+ "cloud.google.com/go/trace/apiv2/tracepb"
26
29
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
27
30
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
28
31
"go.opentelemetry.io/contrib/detectors/gcp"
@@ -39,6 +42,7 @@ import (
39
42
"google.golang.org/grpc/credentials/insecure"
40
43
"google.golang.org/grpc/metadata"
41
44
"google.golang.org/grpc/reflection"
45
+ "google.golang.org/protobuf/types/known/timestamppb"
42
46
43
47
"github.com/openconfig/lemming/dataplane/dplaneopts"
44
48
"github.com/openconfig/lemming/dataplane/saiserver"
@@ -132,6 +136,29 @@ func setupOTelSDK(ctx context.Context) (func(context.Context) error, error) {
132
136
if err != nil {
133
137
return nil , err
134
138
}
139
+ // Create a test span and try exporting it to verify authentication is successful.
140
+ c , err := traceapi .NewClient (ctx )
141
+ if err != nil {
142
+ return nil , err
143
+ }
144
+ _ , span := tracer .Start (ctx , "ping" )
145
+ span .End ()
146
+
147
+ batchCtx := metadata .NewOutgoingContext (ctx , metadata .New (map [string ]string {"x-goog-user-project" : * gcpProject }))
148
+ err = c .BatchWriteSpans (batchCtx , & tracepb.BatchWriteSpansRequest {
149
+ Name : fmt .Sprintf ("projects/%s" , * gcpProject ),
150
+ Spans : []* tracepb.Span {{
151
+ Name : fmt .Sprintf ("projects/%s/traces/%s/spans/%s" , * gcpProject , span .SpanContext ().TraceID ().String (), span .SpanContext ().SpanID ().String ()),
152
+ SpanId : span .SpanContext ().SpanID ().String (),
153
+ DisplayName : & tracepb.TruncatableString {Value : "test" },
154
+ StartTime : timestamppb .Now (),
155
+ EndTime : timestamppb .Now (),
156
+ }},
157
+ })
158
+ if err != nil {
159
+ fmt .Fprintf (os .Stderr , "failed to export test trace, disabling exporter: %v\n " , err )
160
+ exporter = nil
161
+ }
135
162
cloudlog .SetGlobalLogger (ctx , * gcpProject , "lucius" )
136
163
}
137
164
0 commit comments