-
Notifications
You must be signed in to change notification settings - Fork 14
/
connection.go
132 lines (107 loc) · 3.14 KB
/
connection.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
package transport
import (
"context"
"crypto/tls"
"net"
"sync"
"time"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/telemetry/instrumentation"
"github.com/avenga/couper/telemetry/provider"
)
const (
eventOpen = "open"
eventClose = "close"
)
// OriginConn wraps the original net.Conn created by net.DialContext or transport.DialTLS for debug purposes.
type OriginConn struct {
net.Conn
connClosedMu sync.Mutex
connClosed bool
conf *Config
createdAt time.Time
initialReqID string
labels []attribute.KeyValue
log *logrus.Entry
tlsState *tls.ConnectionState
}
// NewOriginConn creates a new wrapper with logging context.
func NewOriginConn(ctx context.Context, conn net.Conn, conf *Config, entry *logrus.Entry) *OriginConn {
var reqID string
if uid, ok := ctx.Value(request.UID).(string); ok {
reqID = uid
}
o := &OriginConn{
Conn: conn,
conf: conf,
createdAt: time.Now(),
initialReqID: reqID,
labels: []attribute.KeyValue{
attribute.String("origin", conf.Origin),
attribute.String("host", conf.Hostname),
attribute.String("backend", conf.BackendName),
},
log: entry,
tlsState: nil,
}
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
o.tlsState = &state
}
entry.WithFields(o.logFields(eventOpen)).Debug()
counter, gauge := newMeterCounter()
counter.Add(ctx, 1, o.labels...)
gauge.Add(ctx, 1, o.labels...)
return o
}
func (o *OriginConn) logFields(event string) logrus.Fields {
fields := logrus.Fields{
"event": event,
"initial_uid": o.initialReqID,
"localAddr": o.LocalAddr().String(),
"origin": o.conf.Origin,
"remoteAddr": o.RemoteAddr().String(),
}
if event == eventClose {
since := time.Since(o.createdAt)
meter := provider.Meter("couper/connection")
duration, _ := meter.SyncFloat64().Histogram(
instrumentation.BackendConnectionsLifetime,
instrument.WithDescription(string(unit.Dimensionless)),
)
duration.Record(context.Background(), since.Seconds(), o.labels...)
fields["lifetime"] = since.Milliseconds()
}
return logrus.Fields{
"connection": fields,
}
}
func (o *OriginConn) Close() error {
o.connClosedMu.Lock()
if o.connClosed {
o.connClosedMu.Unlock()
return nil
}
o.connClosed = true
o.connClosedMu.Unlock()
o.log.WithFields(o.logFields(eventClose)).Debug()
_, gauge := newMeterCounter()
gauge.Add(context.Background(), -1, o.labels...)
return o.Conn.Close()
}
func newMeterCounter() (syncint64.Counter, syncfloat64.UpDownCounter) {
meter := provider.Meter("couper/connection")
counter, _ := meter.SyncInt64().
Counter(instrumentation.BackendConnectionsTotal, instrument.WithDescription(string(unit.Dimensionless)))
gauge, _ := meter.SyncFloat64().UpDownCounter(
instrumentation.BackendConnections,
instrument.WithDescription(string(unit.Dimensionless)),
)
return counter, gauge
}