api/internal/endpoint/handler/tls/interceptor/register.go
Peter Kurfer d70ba748f5 Introduce Lifecycle for every endpoint and manage listeners in the renamed Orchestrator
- merge packages to get a more concise layout because plugins are no more and therefore there's not a lot to be exported
- fix test logger
- rework config parsing to be easier and more transparent
- remove unnecessary APIs because dynamic endpoint handling is rather a won't implement
2021-02-10 20:26:45 +00:00

49 lines
1.3 KiB
Go

package interceptor
import (
"sync"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/inetmock/inetmock/internal/endpoint"
"gitlab.com/inetmock/inetmock/pkg/logging"
"gitlab.com/inetmock/inetmock/pkg/metrics"
"go.uber.org/zap"
)
var (
labelNames = []string{"handler_name"}
handledRequestCounter *prometheus.CounterVec
openConnectionsGauge *prometheus.GaugeVec
requestDurationHistogram *prometheus.HistogramVec
)
func AddTLSInterceptor(registry endpoint.HandlerRegistry) (err error) {
var logger logging.Logger
if logger, err = logging.CreateLogger(); err != nil {
panic(err)
}
logger = logger.With(
zap.String("protocol_handler", name),
)
if handledRequestCounter, err = metrics.Counter(name, "handled_requests", "", labelNames...); err != nil {
return
}
if openConnectionsGauge, err = metrics.Gauge(name, "open_connections", "", labelNames...); err != nil {
return
}
if requestDurationHistogram, err = metrics.Histogram(name, "request_duration", "", nil, labelNames...); err != nil {
}
registry.RegisterHandler(name, func() endpoint.ProtocolHandler {
return &tlsInterceptor{
logger: logger,
currentConnectionsCount: new(sync.WaitGroup),
currentConnections: make(map[uuid.UUID]*proxyConn),
connectionsMutex: &sync.Mutex{},
}
})
return
}