This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
/
main.go
191 lines (155 loc) · 5.01 KB
/
main.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
187
188
189
190
191
// Copyright © 2020 Zach Leslie <code@zleslie.info>
//
// 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://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 main
import (
"context"
"flag"
"fmt"
"io"
"os"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/flagext"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"google.golang.org/grpc"
yaml "gopkg.in/yaml.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
"github.com/xaque208/znet/pkg/util"
"github.com/xaque208/znet/znet"
)
const appName = "znet"
// Version is set via build flag -ldflags -X main.Version
var (
Version string
Branch string
Revision string
)
func init() {
version.Version = Version
version.Branch = Branch
version.Revision = Revision
prometheus.MustRegister(version.NewCollector(appName))
}
func main() {
logger := util.NewLogger()
cfg, err := loadConfig()
if err != nil {
_ = level.Error(logger).Log("msg", "failed to load config file", "err", err)
os.Exit(1)
}
shutdownTracer, err := installOpenTelemetryTracer(cfg, logger)
if err != nil {
_ = level.Error(logger).Log("msg", "error initialising tracer", "err", err)
os.Exit(1)
}
defer shutdownTracer()
z, err := znet.New(*cfg)
if err != nil {
_ = level.Error(logger).Log("msg", "failed to create Znet", "err", err)
os.Exit(1)
}
if err := z.Run(); err != nil {
_ = level.Error(logger).Log("msg", "error running zNet", "err", err)
os.Exit(1)
}
}
func loadConfig() (*znet.Config, error) {
const (
configFileOption = "config.file"
)
var (
configFile string
)
args := os.Args[1:]
config := &znet.Config{}
// first get the config file
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(io.Discard)
fs.StringVar(&configFile, configFileOption, "", "")
// Try to find -config.file & -config.expand-env flags. As Parsing stops on the first error, eg. unknown flag,
// we simply try remaining parameters until we find config flag, or there are no params left.
// (ContinueOnError just means that flag.Parse doesn't call panic or os.Exit, but it returns error, which we ignore)
for len(args) > 0 {
_ = fs.Parse(args)
args = args[1:]
}
// load config defaults and register flags
config.RegisterFlagsAndApplyDefaults("", flag.CommandLine)
// overlay with config file if provided
if configFile != "" {
buff, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to read configFile %s: %w", configFile, err)
}
err = yaml.UnmarshalStrict(buff, config)
if err != nil {
return nil, fmt.Errorf("failed to parse configFile %s: %w", configFile, err)
}
}
// overlay with cli
flagext.IgnoredFlag(flag.CommandLine, configFileOption, "Configuration file to load")
flag.Parse()
return config, nil
}
func installOpenTelemetryTracer(config *znet.Config, logger log.Logger) (func(), error) {
if config.OtelEndpoint == "" {
return func() {}, nil
}
_ = level.Info(logger).Log("msg", "initialising OpenTelemetry tracer")
ctx := context.Background()
res, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(fmt.Sprintf("%s-%s", appName, config.Target)),
semconv.ServiceVersionKey.String(Version),
),
resource.WithHost(),
)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize trace resuorce")
}
conn, err := grpc.DialContext(ctx, config.OtelEndpoint, grpc.WithInsecure())
if err != nil {
return nil, errors.Wrap(err, "failed to dial otel grpc")
}
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return nil, errors.Wrap(err, "failed to creat trace exporter")
}
bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tracerProvider)
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
shutdown := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := tracerProvider.Shutdown(ctx); err != nil {
_ = level.Error(logger).Log("msg", "OpenTelemetry trace provider failed to shutdown", "err", err)
os.Exit(1)
}
}
return shutdown, nil
}