-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
cmd_push_traces.go
87 lines (77 loc) · 1.97 KB
/
cmd_push_traces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cli
import (
"compress/gzip"
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"github.com/datawire/dlib/dlog"
"github.com/telepresenceio/telepresence/v2/pkg/tracing"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
)
func PushTracesCommand() *cobra.Command {
return &cobra.Command{
Use: "upload-traces <zipFile> <jaeger target>",
Args: cobra.ExactArgs(2),
Short: "Upload Traces",
Long: "Upload Traces to a Jaeger instance",
RunE: pushTraces,
SilenceUsage: true,
SilenceErrors: true,
}
}
func traceClient(url string) otlptrace.Client {
client := otlptracegrpc.NewClient(
otlptracegrpc.WithEndpoint(url),
otlptracegrpc.WithInsecure(),
)
return client
}
func pushTraces(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
zipFile, err := filepath.Abs(args[0])
if err != nil {
return err
}
jaegerTarget := args[1]
f, err := os.Open(zipFile)
if err != nil {
return fmt.Errorf("failed to open %s: %w", zipFile, err)
}
defer f.Close()
zipR, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("failed to unzip %s: %w", zipFile, err)
}
defer zipR.Close()
client := traceClient(jaegerTarget)
pr := tracing.NewProtoReader(zipR, func() *tracepb.ResourceSpans { return &tracepb.ResourceSpans{} })
spans, err := pr.ReadAll(ctx)
if err != nil {
return err
}
msg := &tracepb.TracesData{
ResourceSpans: spans,
}
dlog.Debugf(ctx, "Starting upload of %d traces", len(msg.ResourceSpans))
err = client.Start(ctx)
if err != nil {
return err
}
err = client.UploadTraces(ctx, msg.ResourceSpans)
if err != nil {
return err
}
err = client.Stop(ctx)
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStderr(), "Trace file %s uploaded to %s\n", zipFile, jaegerTarget)
return nil
}