-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
250 lines (208 loc) · 8.29 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/cyverse-de/messaging/v9"
"github.com/cyverse-de/resource-usage-api/amqp"
"github.com/cyverse-de/resource-usage-api/cpuhours"
"github.com/cyverse-de/resource-usage-api/db"
"github.com/cyverse-de/resource-usage-api/internal"
"github.com/cyverse-de/resource-usage-api/logging"
"github.com/jmoiron/sqlx"
"github.com/knadh/koanf"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"github.com/cyverse-de/go-mod/cfg"
"github.com/cyverse-de/go-mod/gotelnats"
"github.com/cyverse-de/go-mod/otelutils"
"github.com/cyverse-de/go-mod/protobufjson"
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
"github.com/uptrace/opentelemetry-go-extra/otelsql"
"github.com/uptrace/opentelemetry-go-extra/otelsqlx"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
_ "expvar"
_ "github.com/lib/pq"
)
const serviceName = "resource-usage-api"
var log = logging.Log.WithFields(logrus.Fields{"package": "main"})
func getHandler(dbClient *sqlx.DB, nc *nats.EncodedConn) amqp.HandlerFn {
dedb := db.New(dbClient)
cpuhours := cpuhours.New(dedb, nc)
return func(context context.Context, externalID string, state messaging.JobState) {
var err error
log = log.WithFields(logrus.Fields{"externalID": externalID}).WithContext(context)
if state == messaging.FailedState || state == messaging.SucceededState {
log.Debug("calculating CPU hours for analysis")
if err = cpuhours.CalculateForAnalysis(context, externalID); err != nil {
log.Error(err)
}
log.Debug("done calculating CPU hours for analysis")
} else {
log.Debugf("received status is %s, ignoring", state)
}
}
}
func main() {
var (
err error
config *koanf.Koanf
dbconn *sqlx.DB
configPath = flag.String("config", cfg.DefaultConfigPath, "Full path to the configuration file")
dotEnvPath = flag.String("dotenv-path", cfg.DefaultDotEnvPath, "Path to the dotenv file")
noCreds = flag.Bool("no-creds", false, "Turn off NATS creds support")
noTLS = flag.Bool("no-tls", false, "Turn off TLS support in the NATS connection")
tlsCert = flag.String("tlscert", gotelnats.DefaultTLSCertPath, "Path to the NATS TLS cert file")
tlsKey = flag.String("tlskey", gotelnats.DefaultTLSKeyPath, "Path to the NATS TLS key file")
caCert = flag.String("tlsca", gotelnats.DefaultTLSCAPath, "Path to the NATS TLS CA file")
credsPath = flag.String("creds", gotelnats.DefaultCredsPath, "Path to the NATS creds file")
envPrefix = flag.String("env-prefix", cfg.DefaultEnvPrefix, "The prefix for environment variables")
maxReconnects = flag.Int("max-reconnects", gotelnats.DefaultMaxReconnects, "Maximum number of reconnection attempts to NATS")
reconnectWait = flag.Int("reconnect-wait", gotelnats.DefaultReconnectWait, "Seconds to wait between reconnection attempts to NATS")
listenPort = flag.Int("port", 60000, "The port the service listens on for requests")
queue = flag.String("queue", serviceName, "The AMQP queue name for this service")
reconnect = flag.Bool("reconnect", false, "Whether the AMQP client should reconnect on failure")
logLevel = flag.String("log-level", "info", "One of trace, debug, info, warn, error, fatal, or panic.")
usageRoutingKey = flag.String("usage-routing-key", "qms.usages", "The routing key to use when sending usage updates over AMQP")
dataUsageBase = flag.String("data-usage-base-url", "http://data-usage-api", "The base URL for contacting the data-usage-api service")
subscriptionsBase = flag.String("subscriptions-base-uri", "http://subscriptions", "The base URL for contacting the subscriptions service")
)
flag.Parse()
logrus.AddHook(otellogrus.NewHook())
logging.SetupLogging(*logLevel)
var tracerCtx, cancel = context.WithCancel(context.Background())
defer cancel()
shutdown := otelutils.TracerProviderFromEnv(tracerCtx, serviceName, func(e error) { log.Fatal(e) })
defer shutdown()
nats.RegisterEncoder("protojson", protobufjson.NewCodec(protobufjson.WithEmitUnpopulated()))
log.Infof("config path is %s", *configPath)
log.Infof("listen port is %d", listenPort)
log.Infof("NATS TLS cert file is %s", *tlsCert)
log.Infof("NATS TLS key file is %s", *tlsKey)
log.Infof("NATS CA cert file is %s", *caCert)
log.Infof("NATS creds file is %s", *credsPath)
log.Infof("dotenv file is %s", *dotEnvPath)
config, err = cfg.Init(&cfg.Settings{
EnvPrefix: *envPrefix,
ConfigPath: *configPath,
DotEnvPath: *dotEnvPath,
StrictMerge: false,
FileType: cfg.YAML,
})
if err != nil {
log.Fatal(err)
}
log.Infof("done reading configuration from %s", *configPath)
dbURI := config.String("db.uri")
if dbURI == "" {
log.Fatal("db.uri must be set in the configuration file")
}
amqpURI := config.String("amqp.uri")
if amqpURI == "" {
log.Fatal("amqp.uri must be set in the configuration file")
}
amqpExchange := config.String("amqp.exchange.name")
if amqpExchange == "" {
log.Fatal("amqp.exchange.name must be set in the configuration file")
}
amqpExchangeType := config.String("amqp.exchange.type")
if amqpExchangeType == "" {
log.Fatal("amqp.exchange.type must be set in the configuration file")
}
userSuffix := config.String("users.domain")
if userSuffix == "" {
log.Fatal("users.domain must be set in the configuration file")
}
qmsEnabled := config.Bool("qms.enabled")
qmsBaseURL := config.String("qms.base")
if qmsEnabled {
if qmsBaseURL == "" {
log.Fatal("qms.base must be set in the configuration file if qms.enabled is true")
}
}
natsCluster := config.String("nats.cluster")
if natsCluster == "" {
log.Fatalf("The %sNATS_CLUSTER environment variable or nats.cluster configuration value must be set", *envPrefix)
}
dbconn = otelsqlx.MustConnect("postgres", dbURI,
otelsql.WithAttributes(semconv.DBSystemPostgreSQL))
log.Info("done connecting to the database")
dbconn.SetMaxOpenConns(10)
dbconn.SetConnMaxIdleTime(time.Minute)
options := []nats.Option{
nats.RetryOnFailedConnect(true),
nats.MaxReconnects(*maxReconnects),
nats.ReconnectWait(time.Duration(*reconnectWait) * time.Second),
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
if err != nil {
log.Errorf("disconnected from nats: %s", err.Error())
}
}),
nats.ReconnectHandler(func(nc *nats.Conn) {
log.Infof("reconnected to %s", nc.ConnectedUrl())
}),
nats.ClosedHandler(func(nc *nats.Conn) {
log.Errorf("connection closed: %s", nc.LastError().Error())
}),
}
if !*noTLS {
options = append(options, nats.RootCAs(*caCert))
options = append(options, nats.ClientCert(*tlsCert, *tlsKey))
}
if !*noCreds {
options = append(options, nats.UserCredentials(*credsPath))
}
nc, err := nats.Connect(
natsCluster,
options...,
)
if err != nil {
log.Fatal(err)
}
log.Infof("configured servers: %s", strings.Join(nc.Servers(), " "))
log.Infof("connected to NATS host: %s", nc.ConnectedServerName())
natsClient, err := nats.NewEncodedConn(nc, "protojson")
if err != nil {
log.Fatal(err)
}
amqpConfig := amqp.Configuration{
URI: amqpURI,
Exchange: amqpExchange,
ExchangeType: amqpExchangeType,
Reconnect: *reconnect,
Queue: *queue,
PrefetchCount: 0,
}
log.Infof("AMQP exchange name: %s", amqpConfig.Exchange)
log.Infof("AMQP exchange type: %s", amqpConfig.ExchangeType)
log.Infof("AMQP reconnect: %v", amqpConfig.Reconnect)
log.Infof("AMQP queue name: %s", amqpConfig.Queue)
log.Infof("AMQP prefetch amount %d", amqpConfig.PrefetchCount)
amqpClient, err := amqp.New(&amqpConfig, getHandler(dbconn, natsClient))
if err != nil {
log.Fatal(err)
}
defer amqpClient.Close()
log.Debug("after close")
log.Info("done connecting to the AMQP broker")
appConfig := &internal.AppConfiguration{
UserSuffix: userSuffix,
DataUsageBaseURL: *dataUsageBase,
AMQPClient: amqpClient,
NATSClient: natsClient,
AMQPUsageRoutingKey: *usageRoutingKey,
QMSEnabled: qmsEnabled,
QMSBaseURL: qmsBaseURL,
SubscriptionsBaseURI: *subscriptionsBase,
}
app, err := internal.New(dbconn, appConfig)
if err != nil {
log.Fatal(err)
}
log.Infof("listening on port %d", *listenPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", strconv.Itoa(*listenPort)), app.Router()))
}