-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
216 lines (185 loc) · 5.83 KB
/
config.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright © 2024 Ingka Holding B.V. All Rights Reserved.
//
// 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 config
import (
"context"
"fmt"
"time"
"contrib.go.opencensus.io/exporter/ocagent"
"github.com/blendle/zapdriver"
"github.com/kelseyhightower/envconfig"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"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.22.0"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/ingka-group/iam-proxy/internal/logger"
"github.com/ingka-group/iam-proxy/internal/version"
)
const (
// ServiceName is the name of the service
ServiceName = "iam-proxy"
)
// Config for iam-proxy service.
type Config struct {
Host string
Port int
LogLevel string
IAM IAM
Environment Environment
HTTPTimeout time.Duration
ShutdownTimeout time.Duration
Metric Metric
// Internal
Logger *zap.SugaredLogger `ignored:"true"`
}
// Environment of kubernetes deployment.
type Environment string
// Valid environments
const (
EnvDev Environment = "development"
EnvTest Environment = "test"
EnvStage Environment = "stage"
EnvProd Environment = "production"
)
// IAM defines the configuration for the iam auth2 functionalities
type IAM struct {
Users string
Secret string
}
// Metric for OpenCensus trace and metric collection
type Metric struct {
Enabled bool
OtelAgentAddr string
OpencensusAddr string
// OpencensusReconnInterval represents the time interval, in seconds that
// the OC Agent will attempt to reconnect.
OpencensusReconnInterval int
ReportingInterval time.Duration
}
// New populates Config from environment variables and performs
// initializations in Config struct
func New() (*Config, error) {
c := NewDefault()
err := envconfig.Process("", &c)
if err != nil {
return nil, fmt.Errorf("parse application config %w", err)
}
// Initialize logger
if err := c.initLogger(); err != nil {
return nil, fmt.Errorf("initialize logger %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), c.HTTPTimeout)
defer cancel()
// Initialize tracer
c.initTracer(ctx)
return &c, nil
}
// NewDefault returns a Config with default values only
func NewDefault() Config {
return Config{
Port: 8080,
LogLevel: "info",
HTTPTimeout: 5 * time.Second,
ShutdownTimeout: 30 * time.Second,
}
}
// newLogger initializes logger. In case of errors, it terminates the application
func (c *Config) initLogger() error {
lc := zapdriver.NewProductionConfig()
lc.InitialFields = map[string]interface{}{
"version": version.Version,
}
switch c.LogLevel {
case "debug":
lc.Level.SetLevel(zap.DebugLevel)
case "info":
fallthrough
default:
lc.Level.SetLevel(zap.InfoLevel)
}
var err error
logger.Logger, err = lc.Build()
if err != nil {
return err
}
c.Logger = logger.Logger.Sugar()
return nil
}
// initTracer initializes OpenTelemetry tracer and OpenCensus metric collector
func (c *Config) initTracer(ctx context.Context) {
if !c.Metric.Enabled {
c.Logger.With("config", c.Metric).Info("Disabled opentelemetry tracer & opencensus metrics")
return
}
c.Logger.With("config", c.Metric).Info("Enabled opentelemetry tracer & opencensus metrics")
driver := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(c.Metric.OtelAgentAddr),
otlptracegrpc.WithDialOption(grpc.WithBlock()), // useful for testing
)
exp, err := otlptrace.New(ctx, driver)
if err != nil {
c.Logger.Fatalw("Cannot connect to OpenTelemetry Exporter", err)
}
res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceNameKey.String(ServiceName),
),
)
if err != nil {
c.Logger.Fatalw("Cannot create otel resource", zap.Error(err))
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
// set global propagator to trace context (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
// TODO: Needs to be migrated to the latest OTEL version
//cont := controller.basic.New(
// processor.basic.New(
// selector.simple.NewWithExactDistribution(),
// exp,
// ),
// controller.basic.WithCollectPeriod(7*time.Second),
// controller.basic.WithExporter(exp),
//)
//global.SetMeterProvider(cont.MeterProvider())
// Config the OpenCensus exporter.
oc, err := ocagent.NewExporter(
ocagent.WithInsecure(),
ocagent.WithAddress(c.Metric.OpencensusAddr),
ocagent.WithServiceName(ServiceName),
ocagent.WithReconnectionPeriod(time.Second*time.Duration(c.Metric.OpencensusReconnInterval)),
)
if err != nil {
c.Logger.Fatalw("Could not init OpenCensus exporter", zap.Error(err))
}
// This allows it to receive <stats> emitted by
// OpenCensus-Go and upload them to the agent.
view.RegisterExporter(oc)
view.SetReportingPeriod(c.Metric.ReportingInterval)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}