-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathserver.go
255 lines (227 loc) · 8.29 KB
/
server.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package endtoendserver
import (
"context"
"fmt"
"log"
"strconv"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/pubsub"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"google.golang.org/genproto/googleapis/rpc/code"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
)
// Server is an end-to-end test service.
type Server struct {
pubsubClient *pubsub.Client
// traceProvider *sdktrace.TracerProvider
}
// New instantiates a new end-to-end test service.
func New() (*Server, error) {
if subscriptionMode != "pull" {
return nil, fmt.Errorf("server does not support subscription mode %v", subscriptionMode)
}
pubsubClient, err := pubsub.NewClient(context.Background(), projectID)
if err != nil {
return nil, err
}
return &Server{
pubsubClient: pubsubClient,
// traceProvider: traceProvider,
}, nil
}
// Run the end-to-end test service. This method will block until the context is
// cancel, or an unrecoverable error is encountered.
func (s *Server) Run(ctx context.Context) error {
sub := s.pubsubClient.Subscription(requestSubscriptionName)
log.Printf("End-to-end test service listening on %s", sub)
return sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) { s.onReceive(ctx, m) })
}
// Shutdown gracefully shuts down the service, flushing and closing resources as
// appropriate.
func (s *Server) Shutdown(ctx context.Context) {
if err := s.pubsubClient.Close(); err != nil {
log.Printf("pubsubClient.Close(): %v", err)
}
}
// onReceive executes a scenario based on the incoming message from the test runner.
func (s *Server) onReceive(ctx context.Context, m *pubsub.Message) {
defer m.Ack()
testID := m.Attributes[testIDKey]
scenario := m.Attributes[scenarioKey]
if scenario == "" {
log.Printf("could not find required attribute %q in message %+v", scenarioKey, m)
err := s.respond(ctx, testID, &response{
statusCode: code.Code_INVALID_ARGUMENT,
data: []byte(fmt.Sprintf("required %q is missing", scenarioKey)),
})
if err != nil {
log.Printf("could not publish response: %v", err)
}
return
}
handler := scenarioHandlers[scenario]
if handler == nil {
handler = &unimplementedHandler{}
}
tracerProvider, err := handler.tracerProvider()
if err != nil {
log.Printf("could not initialize a tracer-provider: %v", err)
return
}
req := request{
scenario: scenario,
testID: testID,
}
res := handler.handle(ctx, req, tracerProvider)
if err := shutdownTraceProvider(ctx, tracerProvider); err != nil {
log.Printf("could not shutdown tracer-provider: %v", err)
s.respond(ctx, testID, &response{
statusCode: code.Code_INTERNAL,
data: []byte(fmt.Sprintf("could not shutdown tracer-provider: %v", err)),
})
return
}
if err := s.respond(ctx, testID, res); err != nil {
log.Printf("could not publish response: %v", err)
}
}
// respond to the test runner that we finished executing the scenario by sending
// a message to the response pubsub topic.
func (s *Server) respond(ctx context.Context, testID string, res *response) error {
m := &pubsub.Message{
Data: res.data,
Attributes: map[string]string{
testIDKey: testID,
statusCodeKey: strconv.Itoa(int(res.statusCode)),
traceIDKey: res.traceID.String(),
},
}
publishResult := s.pubsubClient.Topic(responseTopicName).Publish(ctx, m)
_, err := publishResult.Get(ctx)
return err
}
func newTracerProvider(res *resource.Resource) (*sdktrace.TracerProvider, error) {
exporter, err := texporter.New(texporter.WithProjectID(projectID))
if err != nil {
return nil, err
}
traceProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter, sdktrace.WithBatchTimeout(traceBatchTimeout)),
sdktrace.WithResource(res))
return traceProvider, nil
}
// TODO: replace with upstream resource detector
type testDetector struct{}
func (d *testDetector) Detect(ctx context.Context) (*resource.Resource, error) {
if !metadata.OnGCE() {
return nil, nil
}
detector := gcp.NewDetector()
projectID, err := detector.ProjectID()
if err != nil {
return nil, err
}
attributes := []attribute.KeyValue{semconv.CloudProviderGCP, semconv.CloudAccountIDKey.String(projectID)}
switch detector.CloudPlatform() {
case gcp.GKE:
attributes = append(attributes, semconv.CloudPlatformGCPKubernetesEngine)
v, locType, err := detector.GKEAvailabilityZoneOrRegion()
if err != nil {
return nil, err
}
switch locType {
case gcp.Zone:
attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(v))
case gcp.Region:
attributes = append(attributes, semconv.CloudRegionKey.String(v))
default:
return nil, fmt.Errorf("location must be zone or region. Got %v", locType)
}
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.K8SClusterNameKey: detector.GKEClusterName,
semconv.HostIDKey: detector.GKEHostID,
semconv.HostNameKey: detector.GKEHostName,
})
case gcp.CloudRun:
attributes = append(attributes, semconv.CloudPlatformGCPCloudRun)
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.FaaSNameKey: detector.FaaSName,
semconv.FaaSVersionKey: detector.FaaSVersion,
semconv.FaaSIDKey: detector.FaaSID,
semconv.CloudRegionKey: detector.FaaSCloudRegion,
})
case gcp.CloudFunctions:
attributes = append(attributes, semconv.CloudPlatformGCPCloudFunctions)
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.FaaSNameKey: detector.FaaSName,
semconv.FaaSVersionKey: detector.FaaSVersion,
semconv.FaaSIDKey: detector.FaaSID,
semconv.CloudRegionKey: detector.FaaSCloudRegion,
})
case gcp.AppEngine:
attributes = append(attributes, semconv.CloudPlatformGCPAppEngine)
zone, region, err := detector.AppEngineAvailabilityZoneAndRegion()
if err != nil {
return nil, err
}
attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone))
attributes = append(attributes, semconv.CloudRegionKey.String(region))
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.FaaSNameKey: detector.AppEngineServiceName,
semconv.FaaSVersionKey: detector.AppEngineServiceVersion,
semconv.FaaSIDKey: detector.AppEngineServiceInstance,
})
case gcp.GCE:
attributes = append(attributes, semconv.CloudPlatformGCPComputeEngine)
zone, region, err := detector.GCEAvailabilityZoneAndRegion()
if err != nil {
return nil, err
}
attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone))
attributes = append(attributes, semconv.CloudRegionKey.String(region))
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.HostTypeKey: detector.GCEHostType,
semconv.HostIDKey: detector.GCEHostID,
semconv.HostNameKey: detector.GCEHostName,
})
default:
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil
}
}
type detectionFunc func() (string, error)
func detectWithFuncs(attributes []attribute.KeyValue, funcs map[attribute.Key]detectionFunc) (*resource.Resource, error) {
for key, detect := range funcs {
v, err := detect()
if err != nil {
return nil, err
}
attributes = append(attributes, key.String(v))
}
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil
}
func shutdownTraceProvider(ctx context.Context, tracerProvider *sdktrace.TracerProvider) error {
if err := tracerProvider.ForceFlush(ctx); err != nil {
return fmt.Errorf("traceProvider.ForceFlush(): %v", err)
}
if err := tracerProvider.Shutdown(ctx); err != nil {
return fmt.Errorf("traceProvider.Shutdown(): %v", err)
}
return nil
}