* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package metric
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/globals"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const (
|
|
proxyWebsocketSubsystem = "worker_proxy_websocket"
|
|
)
|
|
|
|
var (
|
|
activeConnCount = prometheus.NewGauge(
|
|
prometheus.GaugeOpts{
|
|
Namespace: globals.MetricNamespace,
|
|
Subsystem: proxyWebsocketSubsystem,
|
|
Name: "active_connections",
|
|
Help: "Count of open websocket proxy connections (to Boundary workers).",
|
|
},
|
|
)
|
|
|
|
bytesReceived = prometheus.NewCounter(
|
|
prometheus.CounterOpts{
|
|
Namespace: globals.MetricNamespace,
|
|
Subsystem: proxyWebsocketSubsystem,
|
|
Name: "received_bytes_total",
|
|
Help: "Count of received bytes for Worker proxy websocket connections.",
|
|
},
|
|
)
|
|
|
|
bytesSent = prometheus.NewCounter(
|
|
prometheus.CounterOpts{
|
|
Namespace: globals.MetricNamespace,
|
|
Subsystem: proxyWebsocketSubsystem,
|
|
Name: "sent_bytes_total",
|
|
Help: "Count of sent bytes for Worker proxy websocket connections.",
|
|
},
|
|
)
|
|
)
|
|
|
|
// InitializeWebsocketCollectors registers the websocket collectors onto `r`.
|
|
// It panics upon the first registration that causes an error.
|
|
func InitializeWebsocketCollectors(r prometheus.Registerer) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
r.MustRegister(activeConnCount, bytesReceived, bytesSent)
|
|
}
|
|
|
|
// InstrumentWebsocketWrapper expects an http.Handler where websockets are used
|
|
// and wraps it to instrument that websocket with metrics (bytes received, sent, etc).
|
|
func InstrumentWebsocketWrapper(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
nextRw := rw
|
|
if rwh, ok := rw.(http.Hijacker); ok {
|
|
nextRw = wrapProxyHandler(&hijackerWrapper{ResponseWriter: rw, Hijacker: rwh}, rw)
|
|
}
|
|
next.ServeHTTP(nextRw, req)
|
|
})
|
|
}
|
|
|
|
// wrapProxyHandler wraps the incoming `http.ResponseWriter` in `hijackerWrapper` such
|
|
// that we don't lose access to the other possible implementations that could be present
|
|
// in the original object.
|
|
//
|
|
// See: https://medium.com/@cep21/interface-wrapping-method-erasure-c523b3549912
|
|
func wrapProxyHandler(with *hijackerWrapper, wrap http.ResponseWriter) http.ResponseWriter {
|
|
const op = "metric.wrapProxyHandler"
|
|
flusher, _ := wrap.(http.Flusher)
|
|
pusher, _ := wrap.(http.Pusher)
|
|
|
|
// We don't check for http.Hijacker because hijackerWrapper is always an http.Hijacker.
|
|
switch {
|
|
case flusher == nil && pusher == nil:
|
|
return with
|
|
case flusher != nil && pusher == nil:
|
|
return struct {
|
|
*hijackerWrapper
|
|
http.Flusher
|
|
}{with, flusher}
|
|
case flusher == nil && pusher != nil:
|
|
return struct {
|
|
*hijackerWrapper
|
|
http.Pusher
|
|
}{with, pusher}
|
|
default:
|
|
return struct {
|
|
*hijackerWrapper
|
|
http.Flusher
|
|
http.Pusher
|
|
}{with, flusher, pusher}
|
|
}
|
|
}
|
|
|
|
type wsConn struct {
|
|
*sync.Once
|
|
net.Conn
|
|
}
|
|
|
|
func (wc *wsConn) Read(b []byte) (int, error) {
|
|
n, err := wc.Conn.Read(b)
|
|
if err != nil && err != io.EOF {
|
|
return n, err
|
|
}
|
|
bytesReceived.Add(float64(n))
|
|
return n, err
|
|
}
|
|
|
|
func (wc *wsConn) Write(b []byte) (int, error) {
|
|
n, err := wc.Conn.Write(b)
|
|
if err != nil {
|
|
return n, err
|
|
}
|
|
bytesSent.Add(float64(n))
|
|
return n, nil
|
|
}
|
|
|
|
func (wc *wsConn) Close() error {
|
|
wc.Once.Do(func() { activeConnCount.Dec() })
|
|
return wc.Conn.Close()
|
|
}
|
|
|
|
type hijackerWrapper struct {
|
|
// The `Once` objects constrain incrementing/decrementing active connections.
|
|
// Only a precautionary measure as:
|
|
// 1. http.(Hijacker).Hijack() cannot be called more than once (2nd call is an error).
|
|
// 2. websocket.(*Conn).Close() cannot be called more than once (2nd call is no-op).
|
|
inc sync.Once
|
|
dec sync.Once
|
|
|
|
// `ServeHTTP` takes an `http.ResponseWriter`, so `metricsWrapper`
|
|
// needs to implement it. Other than that, `ResponseWriter` is unused.
|
|
http.ResponseWriter
|
|
|
|
http.Hijacker
|
|
}
|
|
|
|
func (hw *hijackerWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
c, rw, err := hw.Hijacker.Hijack()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
c = &wsConn{Conn: c, Once: &hw.dec}
|
|
|
|
// Our underlying `Hijack` call returns a `bufio.ReadWriter` object with the
|
|
// `Reader` and `Writer` set to the `net.Conn` object *before* we wrap it.
|
|
// The websocket library uses this buffer to Read and Write. This means
|
|
// our Conn implementation would never be called if we didn't replace the
|
|
// `Reader` and `Writer`.
|
|
buffered, _ := rw.Reader.Peek(rw.Reader.Buffered())
|
|
rw.Reader.Reset(io.MultiReader(bytes.NewReader(buffered), c))
|
|
rw = bufio.NewReadWriter(rw.Reader, bufio.NewWriter(c))
|
|
|
|
hw.inc.Do(func() { activeConnCount.Inc() })
|
|
return c, rw, err
|
|
}
|