| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlpmetric/otest/collector.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package otest |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"compress/gzip" |
| 12 |
"context" |
| 13 |
"crypto/ecdsa" |
| 14 |
"crypto/elliptic" |
| 15 |
"crypto/rand" |
| 16 |
"crypto/tls" |
| 17 |
"crypto/x509" |
| 18 |
"crypto/x509/pkix" // nolint:depguard // This is for testing. |
| 19 |
"encoding/pem" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"math/big" |
| 24 |
"net" |
| 25 |
"net/http" |
| 26 |
"net/url" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
|
| 30 |
"google.golang.org/grpc" |
| 31 |
"google.golang.org/grpc/metadata" |
| 32 |
"google.golang.org/protobuf/proto" |
| 33 |
|
| 34 |
"{{ .oconfImportPath }}" |
| 35 |
collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" |
| 36 |
mpb "go.opentelemetry.io/proto/otlp/metrics/v1" |
| 37 |
) |
| 38 |
|
| 39 |
// Collector is the collection target a Client sends metric uploads to. |
| 40 |
type Collector interface { |
| 41 |
Collect() *Storage |
| 42 |
} |
| 43 |
|
| 44 |
type ExportResult struct { |
| 45 |
Response *collpb.ExportMetricsServiceResponse |
| 46 |
Err error |
| 47 |
} |
| 48 |
|
| 49 |
// Storage stores uploaded OTLP metric data in their proto form. |
| 50 |
type Storage struct { |
| 51 |
dataMu sync.Mutex |
| 52 |
data []*mpb.ResourceMetrics |
| 53 |
} |
| 54 |
|
| 55 |
// NewStorage returns a configure storage ready to store received requests. |
| 56 |
func NewStorage() *Storage { |
| 57 |
return &Storage{} |
| 58 |
} |
| 59 |
|
| 60 |
// Add adds the request to the Storage. |
| 61 |
func (s *Storage) Add(request *collpb.ExportMetricsServiceRequest) { |
| 62 |
s.dataMu.Lock() |
| 63 |
defer s.dataMu.Unlock() |
| 64 |
s.data = append(s.data, request.ResourceMetrics...) |
| 65 |
} |
| 66 |
|
| 67 |
// Dump returns all added ResourceMetrics and clears the storage. |
| 68 |
func (s *Storage) Dump() []*mpb.ResourceMetrics { |
| 69 |
s.dataMu.Lock() |
| 70 |
defer s.dataMu.Unlock() |
| 71 |
|
| 72 |
var data []*mpb.ResourceMetrics |
| 73 |
data, s.data = s.data, []*mpb.ResourceMetrics{} |
| 74 |
return data |
| 75 |
} |
| 76 |
|
| 77 |
// GRPCCollector is an OTLP gRPC server that collects all requests it receives. |
| 78 |
type GRPCCollector struct { |
| 79 |
collpb.UnimplementedMetricsServiceServer |
| 80 |
|
| 81 |
headersMu sync.Mutex |
| 82 |
headers metadata.MD |
| 83 |
storage *Storage |
| 84 |
|
| 85 |
resultCh <-chan ExportResult |
| 86 |
listener net.Listener |
| 87 |
srv *grpc.Server |
| 88 |
} |
| 89 |
|
| 90 |
// NewGRPCCollector returns a *GRPCCollector that is listening at the provided |
| 91 |
// endpoint. |
| 92 |
// |
| 93 |
// If endpoint is an empty string, the returned collector will be listening on |
| 94 |
// the localhost interface at an OS chosen port. |
| 95 |
// |
| 96 |
// If errCh is not nil, the collector will respond to Export calls with errors |
| 97 |
// sent on that channel. This means that if errCh is not nil Export calls will |
| 98 |
// block until an error is received. |
| 99 |
func NewGRPCCollector(endpoint string, resultCh <-chan ExportResult) (*GRPCCollector, error) { |
| 100 |
if endpoint == "" { |
| 101 |
endpoint = "localhost:0" |
| 102 |
} |
| 103 |
|
| 104 |
c := &GRPCCollector{ |
| 105 |
storage: NewStorage(), |
| 106 |
resultCh: resultCh, |
| 107 |
} |
| 108 |
|
| 109 |
var err error |
| 110 |
c.listener, err = net.Listen("tcp", endpoint) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
c.srv = grpc.NewServer() |
| 116 |
collpb.RegisterMetricsServiceServer(c.srv, c) |
| 117 |
go func() { _ = c.srv.Serve(c.listener) }() |
| 118 |
|
| 119 |
return c, nil |
| 120 |
} |
| 121 |
|
| 122 |
// Shutdown shuts down the gRPC server closing all open connections and |
| 123 |
// listeners immediately. |
| 124 |
func (c *GRPCCollector) Shutdown() { c.srv.Stop() } |
| 125 |
|
| 126 |
// Addr returns the net.Addr c is listening at. |
| 127 |
func (c *GRPCCollector) Addr() net.Addr { |
| 128 |
return c.listener.Addr() |
| 129 |
} |
| 130 |
|
| 131 |
// Collect returns the Storage holding all collected requests. |
| 132 |
func (c *GRPCCollector) Collect() *Storage { |
| 133 |
return c.storage |
| 134 |
} |
| 135 |
|
| 136 |
// Headers returns the headers received for all requests. |
| 137 |
func (c *GRPCCollector) Headers() map[string][]string { |
| 138 |
// Makes a copy. |
| 139 |
c.headersMu.Lock() |
| 140 |
defer c.headersMu.Unlock() |
| 141 |
return metadata.Join(c.headers) |
| 142 |
} |
| 143 |
|
| 144 |
// Export handles the export req. |
| 145 |
func (c *GRPCCollector) Export( |
| 146 |
ctx context.Context, |
| 147 |
req *collpb.ExportMetricsServiceRequest, |
| 148 |
) (*collpb.ExportMetricsServiceResponse, error) { |
| 149 |
c.storage.Add(req) |
| 150 |
|
| 151 |
if h, ok := metadata.FromIncomingContext(ctx); ok { |
| 152 |
c.headersMu.Lock() |
| 153 |
c.headers = metadata.Join(c.headers, h) |
| 154 |
c.headersMu.Unlock() |
| 155 |
} |
| 156 |
|
| 157 |
if c.resultCh != nil { |
| 158 |
r := <-c.resultCh |
| 159 |
if r.Response == nil { |
| 160 |
return &collpb.ExportMetricsServiceResponse{}, r.Err |
| 161 |
} |
| 162 |
return r.Response, r.Err |
| 163 |
} |
| 164 |
return &collpb.ExportMetricsServiceResponse{}, nil |
| 165 |
} |
| 166 |
|
| 167 |
var emptyExportMetricsServiceResponse = func() []byte { |
| 168 |
body := collpb.ExportMetricsServiceResponse{} |
| 169 |
r, err := proto.Marshal(&body) |
| 170 |
if err != nil { |
| 171 |
panic(err) |
| 172 |
} |
| 173 |
return r |
| 174 |
}() |
| 175 |
|
| 176 |
type HTTPResponseError struct { |
| 177 |
Err error |
| 178 |
Status int |
| 179 |
Header http.Header |
| 180 |
} |
| 181 |
|
| 182 |
func (e *HTTPResponseError) Error() string { |
| 183 |
return fmt.Sprintf("%d: %s", e.Status, e.Err) |
| 184 |
} |
| 185 |
|
| 186 |
func (e *HTTPResponseError) Unwrap() error { return e.Err } |
| 187 |
|
| 188 |
// HTTPCollector is an OTLP HTTP server that collects all requests it receives. |
| 189 |
type HTTPCollector struct { |
| 190 |
plainTextResponse bool |
| 191 |
|
| 192 |
headersMu sync.Mutex |
| 193 |
headers http.Header |
| 194 |
storage *Storage |
| 195 |
|
| 196 |
resultCh <-chan ExportResult |
| 197 |
listener net.Listener |
| 198 |
srv *http.Server |
| 199 |
} |
| 200 |
|
| 201 |
// NewHTTPCollector returns a *HTTPCollector that is listening at the provided |
| 202 |
// endpoint. |
| 203 |
// |
| 204 |
// If endpoint is an empty string, the returned collector will be listening on |
| 205 |
// the localhost interface at an OS chosen port, not use TLS, and listen at the |
| 206 |
// default OTLP metric endpoint path ("/v1/metrics"). If the endpoint contains |
| 207 |
// a prefix of "https" the server will generate weak self-signed TLS |
| 208 |
// certificates and use them to server data. If the endpoint contains a path, |
| 209 |
// that path will be used instead of the default OTLP metric endpoint path. |
| 210 |
// |
| 211 |
// If errCh is not nil, the collector will respond to HTTP requests with errors |
| 212 |
// sent on that channel. This means that if errCh is not nil Export calls will |
| 213 |
// block until an error is received. |
| 214 |
func NewHTTPCollector( |
| 215 |
endpoint string, |
| 216 |
resultCh <-chan ExportResult, |
| 217 |
opts ...func(*HTTPCollector), |
| 218 |
) (*HTTPCollector, error) { |
| 219 |
u, err := url.Parse(endpoint) |
| 220 |
if err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
if u.Host == "" { |
| 224 |
u.Host = "localhost:0" |
| 225 |
} |
| 226 |
if u.Path == "" { |
| 227 |
u.Path = oconf.DefaultMetricsPath |
| 228 |
} |
| 229 |
|
| 230 |
c := &HTTPCollector{ |
| 231 |
headers: http.Header{}, |
| 232 |
storage: NewStorage(), |
| 233 |
resultCh: resultCh, |
| 234 |
} |
| 235 |
for _, opt := range opts { |
| 236 |
opt(c) |
| 237 |
} |
| 238 |
|
| 239 |
c.listener, err = net.Listen("tcp", u.Host) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
|
| 244 |
mux := http.NewServeMux() |
| 245 |
mux.Handle(u.Path, http.HandlerFunc(c.handler)) |
| 246 |
c.srv = &http.Server{ |
| 247 |
Handler: mux, |
| 248 |
ReadTimeout: 10 * time.Second, |
| 249 |
WriteTimeout: 10 * time.Second, |
| 250 |
} |
| 251 |
if u.Scheme == "https" { |
| 252 |
cert, err := weakCertificate() |
| 253 |
if err != nil { |
| 254 |
return nil, err |
| 255 |
} |
| 256 |
c.srv.TLSConfig = &tls.Config{ |
| 257 |
Certificates: []tls.Certificate{cert}, |
| 258 |
} |
| 259 |
go func() { _ = c.srv.ServeTLS(c.listener, "", "") }() |
| 260 |
} else { |
| 261 |
go func() { _ = c.srv.Serve(c.listener) }() |
| 262 |
} |
| 263 |
return c, nil |
| 264 |
} |
| 265 |
|
| 266 |
// WithHTTPCollectorRespondingPlainText makes the HTTPCollector return |
| 267 |
// a plaintext, instead of protobuf, response. |
| 268 |
func WithHTTPCollectorRespondingPlainText() func(*HTTPCollector) { |
| 269 |
return func(s *HTTPCollector) { |
| 270 |
s.plainTextResponse = true |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// Shutdown shuts down the HTTP server closing all open connections and |
| 275 |
// listeners. |
| 276 |
func (c *HTTPCollector) Shutdown(ctx context.Context) error { |
| 277 |
return c.srv.Shutdown(ctx) |
| 278 |
} |
| 279 |
|
| 280 |
// Addr returns the net.Addr c is listening at. |
| 281 |
func (c *HTTPCollector) Addr() net.Addr { |
| 282 |
return c.listener.Addr() |
| 283 |
} |
| 284 |
|
| 285 |
// Collect returns the Storage holding all collected requests. |
| 286 |
func (c *HTTPCollector) Collect() *Storage { |
| 287 |
return c.storage |
| 288 |
} |
| 289 |
|
| 290 |
// Headers returns the headers received for all requests. |
| 291 |
func (c *HTTPCollector) Headers() map[string][]string { |
| 292 |
// Makes a copy. |
| 293 |
c.headersMu.Lock() |
| 294 |
defer c.headersMu.Unlock() |
| 295 |
return c.headers.Clone() |
| 296 |
} |
| 297 |
|
| 298 |
func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) { |
| 299 |
c.respond(w, c.record(r)) |
| 300 |
} |
| 301 |
|
| 302 |
func (c *HTTPCollector) record(r *http.Request) ExportResult { |
| 303 |
// Currently only supports protobuf. |
| 304 |
if v := r.Header.Get("Content-Type"); v != "application/x-protobuf" { |
| 305 |
err := fmt.Errorf("content-type not supported: %s", v) |
| 306 |
return ExportResult{Err: err} |
| 307 |
} |
| 308 |
|
| 309 |
body, err := c.readBody(r) |
| 310 |
if err != nil { |
| 311 |
return ExportResult{Err: err} |
| 312 |
} |
| 313 |
pbRequest := &collpb.ExportMetricsServiceRequest{} |
| 314 |
err = proto.Unmarshal(body, pbRequest) |
| 315 |
if err != nil { |
| 316 |
return ExportResult{ |
| 317 |
Err: &HTTPResponseError{ |
| 318 |
Err: err, |
| 319 |
Status: http.StatusInternalServerError, |
| 320 |
}, |
| 321 |
} |
| 322 |
} |
| 323 |
c.storage.Add(pbRequest) |
| 324 |
|
| 325 |
c.headersMu.Lock() |
| 326 |
for k, vals := range r.Header { |
| 327 |
for _, v := range vals { |
| 328 |
c.headers.Add(k, v) |
| 329 |
} |
| 330 |
} |
| 331 |
c.headersMu.Unlock() |
| 332 |
|
| 333 |
if c.resultCh != nil { |
| 334 |
return <-c.resultCh |
| 335 |
} |
| 336 |
return ExportResult{Err: err} |
| 337 |
} |
| 338 |
|
| 339 |
func (c *HTTPCollector) readBody(r *http.Request) (body []byte, err error) { |
| 340 |
var reader io.ReadCloser |
| 341 |
switch r.Header.Get("Content-Encoding") { |
| 342 |
case "gzip": |
| 343 |
reader, err = gzip.NewReader(r.Body) |
| 344 |
if err != nil { |
| 345 |
_ = reader.Close() |
| 346 |
return nil, &HTTPResponseError{ |
| 347 |
Err: err, |
| 348 |
Status: http.StatusInternalServerError, |
| 349 |
} |
| 350 |
} |
| 351 |
default: |
| 352 |
reader = r.Body |
| 353 |
} |
| 354 |
|
| 355 |
defer func() { |
| 356 |
cErr := reader.Close() |
| 357 |
if err == nil && cErr != nil { |
| 358 |
err = &HTTPResponseError{ |
| 359 |
Err: cErr, |
| 360 |
Status: http.StatusInternalServerError, |
| 361 |
} |
| 362 |
} |
| 363 |
}() |
| 364 |
body, err = io.ReadAll(reader) |
| 365 |
if err != nil { |
| 366 |
err = &HTTPResponseError{ |
| 367 |
Err: err, |
| 368 |
Status: http.StatusInternalServerError, |
| 369 |
} |
| 370 |
} |
| 371 |
return body, err |
| 372 |
} |
| 373 |
|
| 374 |
func (c *HTTPCollector) respond(w http.ResponseWriter, resp ExportResult) { |
| 375 |
if resp.Err != nil { |
| 376 |
w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 377 |
w.Header().Set("X-Content-Type-Options", "nosniff") |
| 378 |
var e *HTTPResponseError |
| 379 |
if errors.As(resp.Err, &e) { |
| 380 |
for k, vals := range e.Header { |
| 381 |
for _, v := range vals { |
| 382 |
w.Header().Add(k, v) |
| 383 |
} |
| 384 |
} |
| 385 |
w.WriteHeader(e.Status) |
| 386 |
fmt.Fprintln(w, e.Error()) |
| 387 |
} else { |
| 388 |
w.WriteHeader(http.StatusBadRequest) |
| 389 |
fmt.Fprintln(w, resp.Err.Error()) |
| 390 |
} |
| 391 |
return |
| 392 |
} |
| 393 |
|
| 394 |
if c.plainTextResponse { |
| 395 |
w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 396 |
w.WriteHeader(http.StatusOK) |
| 397 |
_, _ = w.Write([]byte("OK")) |
| 398 |
return |
| 399 |
} |
| 400 |
|
| 401 |
w.Header().Set("Content-Type", "application/x-protobuf") |
| 402 |
w.WriteHeader(http.StatusOK) |
| 403 |
if resp.Response == nil { |
| 404 |
_, _ = w.Write(emptyExportMetricsServiceResponse) |
| 405 |
} else { |
| 406 |
r, err := proto.Marshal(resp.Response) |
| 407 |
if err != nil { |
| 408 |
panic(err) |
| 409 |
} |
| 410 |
_, _ = w.Write(r) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// Based on https://golang.org/src/crypto/tls/generate_cert.go, |
| 415 |
// simplified and weakened. |
| 416 |
func weakCertificate() (tls.Certificate, error) { |
| 417 |
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 418 |
if err != nil { |
| 419 |
return tls.Certificate{}, err |
| 420 |
} |
| 421 |
notBefore := time.Now() |
| 422 |
notAfter := notBefore.Add(time.Hour) |
| 423 |
m := new(big.Int).Lsh(big.NewInt(1), 128) |
| 424 |
sn, err := rand.Int(rand.Reader, m) |
| 425 |
if err != nil { |
| 426 |
return tls.Certificate{}, err |
| 427 |
} |
| 428 |
tmpl := x509.Certificate{ |
| 429 |
SerialNumber: sn, |
| 430 |
Subject: pkix.Name{Organization: []string{"otel-go"}}, |
| 431 |
NotBefore: notBefore, |
| 432 |
NotAfter: notAfter, |
| 433 |
KeyUsage: x509.KeyUsageDigitalSignature, |
| 434 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 435 |
BasicConstraintsValid: true, |
| 436 |
DNSNames: []string{"localhost"}, |
| 437 |
IPAddresses: []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}, |
| 438 |
} |
| 439 |
derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &priv.PublicKey, priv) |
| 440 |
if err != nil { |
| 441 |
return tls.Certificate{}, err |
| 442 |
} |
| 443 |
var certBuf bytes.Buffer |
| 444 |
err = pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) |
| 445 |
if err != nil { |
| 446 |
return tls.Certificate{}, err |
| 447 |
} |
| 448 |
privBytes, err := x509.MarshalPKCS8PrivateKey(priv) |
| 449 |
if err != nil { |
| 450 |
return tls.Certificate{}, err |
| 451 |
} |
| 452 |
var privBuf bytes.Buffer |
| 453 |
err = pem.Encode(&privBuf, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) |
| 454 |
if err != nil { |
| 455 |
return tls.Certificate{}, err |
| 456 |
} |
| 457 |
return tls.X509KeyPair(certBuf.Bytes(), privBuf.Bytes()) |
| 458 |
} |