forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
186 lines (163 loc) · 5.45 KB
/
exporter.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
// Copyright 2019, OpenTelemetry Authors
//
// 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:https://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 carbonexporter
import (
"context"
"fmt"
"net"
"sync"
"time"
agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/translator/internaldata"
)
// newCarbonExporter returns a new Carbon exporter.
func newCarbonExporter(cfg *Config, params component.ExporterCreateSettings) (component.MetricsExporter, error) {
// Resolve TCP address just to ensure that it is a valid one. It is better
// to fail here than at when the exporter is started.
if _, err := net.ResolveTCPAddr("tcp", cfg.Endpoint); err != nil {
return nil, fmt.Errorf("%v exporter has an invalid TCP endpoint: %w", cfg.ID(), err)
}
// Negative timeouts are not acceptable, since all sends will fail.
if cfg.Timeout < 0 {
return nil, fmt.Errorf("%v exporter requires a positive timeout", cfg.ID())
}
sender := carbonSender{
connPool: newTCPConnPool(cfg.Endpoint, cfg.Timeout),
}
return exporterhelper.NewMetricsExporter(
cfg,
params.Logger,
sender.pushMetricsData,
exporterhelper.WithShutdown(sender.Shutdown))
}
// carbonSender is the struct tying the translation function and the TCP
// connections into an implementations of exporterhelper.PushMetricsData so
// the exporter can leverage the helper and get consistent observability.
type carbonSender struct {
connPool *connPool
}
func (cs *carbonSender) pushMetricsData(_ context.Context, md pdata.Metrics) error {
rms := md.ResourceMetrics()
mds := make([]*agentmetricspb.ExportMetricsServiceRequest, 0, rms.Len())
for i := 0; i < rms.Len(); i++ {
emsr := &agentmetricspb.ExportMetricsServiceRequest{}
emsr.Node, emsr.Resource, emsr.Metrics = internaldata.ResourceMetricsToOC(rms.At(i))
mds = append(mds, emsr)
}
lines, _, _ := metricDataToPlaintext(mds)
if _, err := cs.connPool.Write([]byte(lines)); err != nil {
// Use the sum of converted and dropped since the write failed for all.
return err
}
return nil
}
func (cs *carbonSender) Shutdown(context.Context) error {
cs.connPool.Close()
return nil
}
// connPool is a very simple implementation of a pool of net.TCPConn instances.
// The implementation hides the pool and exposes a Write and Close methods.
// It leverages the prior art from SignalFx Gateway (see
// https://github.com/signalfx/gateway/blob/master/protocol/carbon/conn_pool.go
// but not its implementation).
//
// It keeps a unbounded "stack" of TCPConn instances always "popping" the most
// recently returned to the pool. There is no accounting to terminating old
// unused connections as that was the case on the prior art mentioned above.
type connPool struct {
mtx sync.Mutex
conns []*net.TCPConn
endpoint string
timeout time.Duration
}
func newTCPConnPool(
endpoint string,
timeout time.Duration,
) *connPool {
return &connPool{
endpoint: endpoint,
timeout: timeout,
}
}
func (cp *connPool) Write(bytes []byte) (int, error) {
var conn *net.TCPConn
var err error
// The deferred function below is what puts back connections on the pool.
defer func() {
if err == nil {
cp.mtx.Lock()
cp.conns = append(cp.conns, conn)
cp.mtx.Unlock()
} else {
if conn != nil {
conn.Close()
}
}
}()
start := time.Now()
cp.mtx.Lock()
lastIdx := len(cp.conns) - 1
if lastIdx >= 0 {
conn = cp.conns[lastIdx]
cp.conns = cp.conns[0:lastIdx]
}
cp.mtx.Unlock()
if conn == nil {
if conn, err = cp.createTCPConn(); err != nil {
return 0, err
}
}
// There is no way to do a call equivalent to recvfrom with an empty buffer
// to check if the connection was terminated (if the size of the buffer is
// 0 the Read call doesn't call lower level). So due to buffer sizes it is
// possible that a write will succeed on a connection that was already
// closed by the server.
//
// At least on Darwin it is possible to work around this by configuring the
// buffer on each call, ie.:
//
// if err = conn.SetWriteBuffer(len(bytes)-1); err != nil {
// return 0, err
// }
//
// However, this causes a performance penalty of ~10% cpu and it is not
// present in various implementations of Carbon clients. Considering these
// facts this "workaround" is not being added at this moment. If it is
// needed in some scenarios the workaround should be validated on other
// platforms and offered as a configuration setting.
if err = conn.SetWriteDeadline(start.Add(cp.timeout)); err != nil {
return 0, err
}
var n int
n, err = conn.Write(bytes)
return n, err
}
func (cp *connPool) Close() {
cp.mtx.Lock()
defer cp.mtx.Unlock()
for _, conn := range cp.conns {
conn.Close()
}
cp.conns = nil
}
func (cp *connPool) createTCPConn() (*net.TCPConn, error) {
c, err := net.DialTimeout("tcp", cp.endpoint, cp.timeout)
if err != nil {
return nil, err
}
return c.(*net.TCPConn), err
}