Skip to content

Commit

Permalink
try log (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 authored Dec 19, 2024
1 parent 13afec2 commit 1857c60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dataplane/standalone/lucius/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ go_library(
"//proto/forwarding",
"@com_github_googlecloudplatform_opentelemetry_operations_go_exporter_trace//:trace",
"@com_github_grpc_ecosystem_go_grpc_middleware_v2//interceptors/logging",
"@com_google_cloud_go_trace//apiv2",
"@com_google_cloud_go_trace//apiv2/tracepb",
"@io_opentelemetry_go_contrib_detectors_gcp//:gcp",
"@io_opentelemetry_go_contrib_instrumentation_google_golang_org_grpc_otelgrpc//:otelgrpc",
"@io_opentelemetry_go_otel//:otel",
Expand All @@ -30,6 +32,7 @@ go_library(
"@org_golang_google_grpc//credentials/insecure",
"@org_golang_google_grpc//metadata",
"@org_golang_google_grpc//reflection",
"@org_golang_google_protobuf//types/known/timestamppb",
],
)

Expand Down
27 changes: 27 additions & 0 deletions dataplane/standalone/lucius/lucius.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import (
"log"
"log/slog"
"net"
"os"

traceapi "cloud.google.com/go/trace/apiv2"
"cloud.google.com/go/trace/apiv2/tracepb"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"go.opentelemetry.io/contrib/detectors/gcp"
Expand All @@ -39,6 +42,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/openconfig/lemming/dataplane/dplaneopts"
"github.com/openconfig/lemming/dataplane/saiserver"
Expand Down Expand Up @@ -132,6 +136,29 @@ func setupOTelSDK(ctx context.Context) (func(context.Context) error, error) {
if err != nil {
return nil, err
}
// Create a test span and try exporting it to verify authentication is successful.
c, err := traceapi.NewClient(ctx)
if err != nil {
return nil, err
}
_, span := tracer.Start(ctx, "ping")
span.End()

batchCtx := metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{"x-goog-user-project": *gcpProject}))
err = c.BatchWriteSpans(batchCtx, &tracepb.BatchWriteSpansRequest{
Name: fmt.Sprintf("projects/%s", *gcpProject),
Spans: []*tracepb.Span{{
Name: fmt.Sprintf("projects/%s/traces/%s/spans/%s", *gcpProject, span.SpanContext().TraceID().String(), span.SpanContext().SpanID().String()),
SpanId: span.SpanContext().SpanID().String(),
DisplayName: &tracepb.TruncatableString{Value: "test"},
StartTime: timestamppb.Now(),
EndTime: timestamppb.Now(),
}},
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to export test trace, disabling exporter: %v\n", err)
exporter = nil
}
cloudlog.SetGlobalLogger(ctx, *gcpProject, "lucius")
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23
require (
cloud.google.com/go/cloudbuild v1.16.3
cloud.google.com/go/logging v1.10.0
cloud.google.com/go/trace v1.10.10
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.24.1
github.com/fatih/color v1.15.0
github.com/go-logr/logr v1.4.2
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
cloud.google.com/go/iam v1.1.10 // indirect
cloud.google.com/go/longrunning v0.5.9 // indirect
cloud.google.com/go/pubsub v1.40.0 // indirect
cloud.google.com/go/trace v1.10.10 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions internal/cloudlog/cloudlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func SetGlobalLogger(ctx context.Context, project, logName string) error {
if err != nil {
return err
}
if err := cl.Ping(ctx); err != nil {
fmt.Fprintf(os.Stderr, "failed to ping cloud logging, not exporting logs: %v\n", err)
return err
}

h := &cloudLogHandle{
Handler: slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{}),
Expand All @@ -52,6 +56,7 @@ func SetGlobalLogger(ctx context.Context, project, logName string) error {
select {
case <-sigs:
h.l.Flush()
os.Exit(1)
case <-t.C:
if err := h.l.Flush(); err != nil {
fmt.Fprintf(os.Stderr, "log flush err: %v", err)
Expand Down

0 comments on commit 1857c60

Please sign in to comment.