forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.go
115 lines (98 loc) · 2.32 KB
/
sender.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
import (
"context"
"crypto/tls"
"fmt"
"net"
"strings"
"sync"
"go.opentelemetry.io/collector/config/confignet"
"go.uber.org/zap"
)
const defaultPriority = 165
const versionRFC5424 = 1
const protocolRFC5424Str = "rfc5424"
const protocolRFC3164Str = "rfc3164"
const priority = "priority"
const version = "version"
const hostname = "hostname"
const app = "appname"
const pid = "proc_id"
const msgID = "msg_id"
const structuredData = "structured_data"
const message = "message"
const emptyValue = "-"
const emptyMessage = ""
type sender struct {
network string
addr string
protocol string
tlsConfig *tls.Config
logger *zap.Logger
mu sync.Mutex
conn net.Conn
}
func connect(ctx context.Context, logger *zap.Logger, cfg *Config, tlsConfig *tls.Config) (*sender, error) {
s := &sender{
logger: logger,
network: cfg.Network,
addr: fmt.Sprintf("%s:%d", cfg.Endpoint, cfg.Port),
protocol: cfg.Protocol,
tlsConfig: tlsConfig,
}
s.mu.Lock()
defer s.mu.Unlock()
err := s.dial(ctx)
if err != nil {
return nil, err
}
return s, err
}
func (s *sender) close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.conn != nil {
err := s.conn.Close()
s.conn = nil
return err
}
return nil
}
func (s *sender) dial(ctx context.Context) error {
if s.conn != nil {
s.conn.Close()
s.conn = nil
}
var err error
if s.tlsConfig != nil && s.network == string(confignet.TransportTypeTCP) {
dialer := tls.Dialer{Config: s.tlsConfig}
s.conn, err = dialer.DialContext(ctx, s.network, s.addr)
} else {
dialer := new(net.Dialer)
s.conn, err = dialer.DialContext(ctx, s.network, s.addr)
}
return err
}
func (s *sender) Write(ctx context.Context, msgStr string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.conn != nil {
if err := s.write(msgStr); err == nil {
return nil
}
}
if err := s.dial(ctx); err != nil {
return err
}
return s.write(msgStr)
}
func (s *sender) write(msg string) error {
// check if logs contains new line character at the end, if not add it
if !strings.HasSuffix(msg, "\n") {
msg = fmt.Sprintf("%s%s", msg, "\n")
}
_, err := fmt.Fprint(s.conn, msg)
return err
}