-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
170 lines (142 loc) · 5.18 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
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package main
import (
"context"
"os"
"github.com/sirupsen/logrus"
"github.com/go-openapi/swag"
"github.com/davecgh/go-spew/spew"
"github.com/communitybridge/easycla/cla-backend-go/utils"
"github.com/aws/aws-lambda-go/lambda"
"github.com/communitybridge/easycla/cla-backend-go/token"
"github.com/communitybridge/easycla/cla-backend-go/config"
"github.com/LF-Engineering/lfx-models/models/stats"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
awsqs "github.com/aws/aws-sdk-go/service/sqs"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
"github.com/communitybridge/easycla/cla-backend-go/projects_cla_groups"
"github.com/communitybridge/easycla/cla-backend-go/v2/metrics"
v2ProjectService "github.com/communitybridge/easycla/cla-backend-go/v2/project-service"
)
var (
// version the application version
version string
// build/Commit the application build number
commit string
// branch the build branch
branch string
// build date
buildDate string
)
var awsSession = session.Must(session.NewSession(&aws.Config{}))
var metricsRepo metrics.Repository
var stage string
func init() {
stage = os.Getenv("STAGE")
if stage == "" {
log.Fatal("stage not set")
}
log.Infof("STAGE set to %s\n", stage)
configFile, err := config.LoadConfig("", awsSession, stage)
if err != nil {
log.Panicf("Unable to load config - Error: %v", err)
}
pcgRepo := projects_cla_groups.NewRepository(awsSession, stage)
metricsRepo = metrics.NewRepository(awsSession, stage, configFile.APIGatewayURL, pcgRepo)
token.Init(configFile.Auth0Platform.ClientID, configFile.Auth0Platform.ClientSecret, configFile.Auth0Platform.URL, configFile.Auth0Platform.Audience)
v2ProjectService.InitClient(configFile.APIGatewayURL)
}
func handler(ctx context.Context, event events.CloudWatchEvent) {
f := logrus.Fields{
"functionName": "handler",
"eventID": event.ID,
"eventVersion": event.Version,
}
totalCountMetrics, err := metricsRepo.GetTotalCountMetrics()
if err != nil {
log.WithFields(f).WithError(err).Fatal("unable to get totalCount metrics from dynamodb.")
}
req := stats.Request{
Products: &stats.Products{
EasyCLA: stats.Stats{
"cla_contributors": stats.Stat{
Action: swag.String(stats.StatActionReplace),
Frequency: swag.String(stats.StatFrequencyAllTime),
Type: swag.String(stats.StatTypeNumber),
Value: float64(totalCountMetrics.ContributorsCount),
},
"clas_signed": stats.Stat{
Action: swag.String(stats.StatActionReplace),
Frequency: swag.String(stats.StatFrequencyAllTime),
Type: swag.String(stats.StatTypeNumber),
Value: float64(totalCountMetrics.CLAsSignedCount),
},
"live_projects": stats.Stat{
Action: swag.String(stats.StatActionReplace),
Frequency: swag.String(stats.StatFrequencyAllTime),
Type: swag.String(stats.StatTypeNumber),
Value: float64(totalCountMetrics.ProjectsLiveCount),
},
// repositories = GitHub repositories + Gerrit Instances (not repos) <--- under-counting gerrit repos
"repositories_covered": stats.Stat{
Action: swag.String(stats.StatActionReplace),
Frequency: swag.String(stats.StatFrequencyAllTime),
Type: swag.String(stats.StatTypeNumber),
Value: float64(totalCountMetrics.GerritRepositoriesEnabledCount + totalCountMetrics.GithubRepositoriesEnabledCount),
},
},
},
Project: "",
}
dataInBytes, err := req.MarshalBinary()
if err != nil {
log.WithFields(f).WithError(err).Fatal("marshall the sqs event failed")
}
log.WithFields(f).Debugf("going to send total count metrics to sqs queue %s", spew.Sdump(req))
conf := config.GetConfig()
var region, queueURL string
if conf.MetricsReport.AwsSQSRegion == "" {
log.WithFields(f).Fatal("aws sqs region config is missing")
}
region = conf.MetricsReport.AwsSQSRegion
if conf.MetricsReport.AwsSQSQueueURL == "" {
log.WithFields(f).Fatal("aws sqs queue url config is missing")
}
queueURL = conf.MetricsReport.AwsSQSQueueURL
sqsSession := awsqs.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
})))
input := &awsqs.SendMessageInput{
MessageBody: aws.String(string(dataInBytes)),
QueueUrl: aws.String(queueURL),
}
if conf.MetricsReport.Enabled {
_, err = sqsSession.SendMessage(input)
if err != nil {
log.WithFields(f).WithError(err).Fatal("sending the message to sqs failed")
return
}
log.WithFields(f).Infof("metrics report sent successfully to queue %s", queueURL)
} else {
log.WithFields(f).Info("metrics report not sent - disabled in the configuration")
}
}
func printBuildInfo() {
log.Infof("Version : %s", version)
log.Infof("Git commit hash : %s", commit)
log.Infof("Branch : %s", branch)
log.Infof("Build date : %s", buildDate)
}
func main() {
log.Info("Lambda server starting...")
printBuildInfo()
if os.Getenv("LOCAL_MODE") == "true" {
handler(utils.NewContext(), events.CloudWatchEvent{})
} else {
lambda.Start(handler)
}
log.Infof("Lambda shutting down...")
}