-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathmain.go
159 lines (138 loc) · 5.1 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"strings"
pb_struct "github.com/envoyproxy/go-control-plane/envoy/extensions/common/ratelimit/v3"
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v3"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/envoyproxy/ratelimit/src/utils"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)
type descriptorsValue struct {
descriptors []*pb_struct.RateLimitDescriptor
}
func (this *descriptorsValue) Set(arg string) error {
pairs := strings.Split(arg, ",")
entries := make([]*pb_struct.RateLimitDescriptor_Entry, len(pairs))
for i, pair := range pairs {
parts := strings.Split(pair, "=")
if len(parts) != 2 {
return errors.New("invalid descriptor list")
}
entries[i] = &pb_struct.RateLimitDescriptor_Entry{Key: parts[0], Value: parts[1]}
}
this.descriptors = append(this.descriptors, &pb_struct.RateLimitDescriptor{Entries: entries})
return nil
}
func (this *descriptorsValue) String() string {
ret := ""
for _, descriptor := range this.descriptors {
tmp := ""
for _, entry := range descriptor.Entries {
tmp += fmt.Sprintf(" <key=%s, value=%s> ", entry.Key, entry.Value)
}
ret += fmt.Sprintf("[%s] ", tmp)
}
return ret
}
func main() {
dialString := flag.String(
"dial_string", "localhost:8081", "url of ratelimit server in <host>:<port> form")
domain := flag.String("domain", "", "rate limit configuration domain to query")
descriptorsValue := descriptorsValue{[]*pb_struct.RateLimitDescriptor{}}
flag.Var(
&descriptorsValue, "descriptors",
"descriptor list to query in <key>=<value>,<key>=<value>,... form")
oltpProtocol := flag.String("oltp-protocol", "", "protocol to use when exporting tracing span, accept http, grpc or empty (disable tracing) as value, please use OLTP environment variables to set endpoint (refer to README.MD)")
grpcServerTlsCACert := flag.String("grpc-server-ca-file", "", "path to the server CA file for TLS connection")
grpcUseTLS := flag.Bool("grpc-use-tls", false, "Use TLS for connection to server")
grpcTlsCertFile := flag.String("grpc-cert-file", "", "path to the client cert file for TLS connection")
grpcTlsKeyFile := flag.String("grpc-key-file", "", "path to the client key for TLS connection")
flag.Parse()
flag.VisitAll(func(f *flag.Flag) {
fmt.Printf("Flag: --%s=%q\n", f.Name, f.Value)
})
if *oltpProtocol != "" {
tp := InitTracerProvider(*oltpProtocol)
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
}
dialOptions := []grpc.DialOption{
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
}
if *grpcUseTLS {
tlsConfig := utils.TlsConfigFromFiles(*grpcTlsCertFile, *grpcTlsKeyFile, *grpcServerTlsCACert, utils.ServerCA, false)
dialOptions = append(dialOptions, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.Dial(*dialString, dialOptions...)
if err != nil {
fmt.Printf("error connecting: %s\n", err.Error())
os.Exit(1)
}
defer conn.Close()
c := pb.NewRateLimitServiceClient(conn)
desc := make([]*pb_struct.RateLimitDescriptor, len(descriptorsValue.descriptors))
copy(desc, descriptorsValue.descriptors)
response, err := c.ShouldRateLimit(
context.Background(),
&pb.RateLimitRequest{
Domain: *domain,
Descriptors: desc,
HitsAddend: 1,
})
if err != nil {
fmt.Printf("request error: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("response: %s\n", response.String())
}
// using a simpler setup in this trace provider for simplicity
func InitTracerProvider(protocol string) *sdktrace.TracerProvider {
var client otlptrace.Client
switch protocol {
case "http":
client = otlptracehttp.NewClient()
case "grpc":
client = otlptracegrpc.NewClient()
default:
fmt.Printf("Invalid otlptrace client protocol: %s", protocol)
panic("Invalid otlptrace client protocol")
}
exporter, err := otlptrace.New(context.Background(), client)
if err != nil {
log.Fatalf("creating OTLP trace exporter: %v", err)
}
resource := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("RateLimitClient"),
)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resource),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}