Skip to content

Commit 537ad44

Browse files
author
Christian González Di Antonio
committed
improved, refactored
1 parent 6721dea commit 537ad44

File tree

7 files changed

+38
-47
lines changed

7 files changed

+38
-47
lines changed

.github/workflows/release.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ jobs:
9999
- name: List .build Files
100100
run: ls -R .build
101101

102+
- name: Set Env Var RELEASE_VERSION
103+
run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF#refs/*/}
104+
105+
- name: Show RELEASE_VERSION
106+
run: echo $RELEASE_VERSION
107+
102108
- name: Build Docker Images
103-
run: make docker DOCKER_REPO=docker.io/slashdevops
109+
run: make docker DOCKER_IMAGE_TAG=$RELEASE_VERSION DOCKER_REPO=docker.io/slashdevops
104110

105111
- name: Docker Tag Images Latest
106-
run: make docker-tag-latest DOCKER_REPO=docker.io/slashdevops
112+
run: make docker-tag-latest DOCKER_IMAGE_TAG=$RELEASE_VERSION DOCKER_REPO=docker.io/slashdevops
107113

108114
- name: Show Local Docker Images
109115
run: docker images
@@ -112,10 +118,10 @@ jobs:
112118
run: echo ${{ secrets.DOCKER_HUB_PASSWORD }} | docker login -u ${{ secrets.DOCKER_HUB_USER }} --password-stdin
113119

114120
- name: Publish Images in Docker Hub
115-
run: make docker-publish DOCKER_REPO=$DOCKER_HUB_REPO
121+
run: make docker-publish DOCKER_IMAGE_TAG=$RELEASE_VERSION DOCKER_REPO=$DOCKER_HUB_REPO
116122

117123
- name: Publish Manifest in Docker Hub
118-
run: make docker-manifest DOCKER_REPO=$DOCKER_HUB_REPO
124+
run: make docker-manifest DOCKER_IMAGE_TAG=$RELEASE_VERSION DOCKER_REPO=$DOCKER_HUB_REPO
119125

120126
create_release:
121127
name: Create Release

cmd/metrics.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ func collectCmd(cmd *cobra.Command, args []string) {
173173

174174
m := metrics.New(&conf)
175175
sess := awshelper.NewSession(&conf.AWS)
176+
cwc := cloudwatch.New(sess)
177+
178+
c := collector.New(&conf, m, cwc)
176179

177-
c := collector.New(&conf, m, sess)
178180
prometheus.MustRegister(c)
179181
http.Handle("/metrics", promhttp.Handler())
180182

cmd/server.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http/pprof"
2121
"time"
2222

23+
"github.com/aws/aws-sdk-go/service/cloudwatch"
2324
"github.com/prometheus/client_golang/prometheus"
2425
"github.com/prometheus/client_golang/prometheus/promhttp"
2526
"github.com/slashdevops/aws_cloudwatch_exporter/internal/awshelper"
@@ -122,9 +123,11 @@ func startCmd(cmd *cobra.Command, args []string) {
122123

123124
m := metrics.New(&conf)
124125
sess := awshelper.NewSession(&conf.AWS)
126+
cwc := cloudwatch.New(sess)
125127

126-
collector := collector.New(&conf, m, sess)
127-
prometheus.MustRegister(collector)
128+
c := collector.New(&conf, m, cwc)
129+
130+
prometheus.MustRegister(c)
128131

129132
handlers := web.NewHandlers(&conf)
130133

@@ -134,20 +137,18 @@ func startCmd(cmd *cobra.Command, args []string) {
134137
mux.Handle(conf.Application.MetricsPath, promhttp.Handler())
135138

136139
// Debug & Profiling
137-
if conf.Server.Debug {
138-
mux.HandleFunc("/debug/pprof/", pprof.Index)
139-
mux.HandleFunc("/debug/pprof/heap", pprof.Index)
140-
mux.HandleFunc("/debug/pprof/mutex", pprof.Index)
141-
mux.HandleFunc("/debug/pprof/goroutine", pprof.Index)
142-
mux.HandleFunc("/debug/pprof/threadcreate", pprof.Index)
143-
mux.HandleFunc("/debug/pprof/block", pprof.Index)
144-
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
145-
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
146-
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
147-
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
148-
}
149-
150-
// this channel is to wait routines before finis this
140+
mux.HandleFunc("/debug/pprof/", pprof.Index)
141+
mux.HandleFunc("/debug/pprof/heap", pprof.Index)
142+
mux.HandleFunc("/debug/pprof/mutex", pprof.Index)
143+
mux.HandleFunc("/debug/pprof/goroutine", pprof.Index)
144+
mux.HandleFunc("/debug/pprof/threadcreate", pprof.Index)
145+
mux.HandleFunc("/debug/pprof/block", pprof.Index)
146+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
147+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
148+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
149+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
150+
151+
// this channel is to wait routines
151152
done := make(chan bool, 1)
152153
s := server.New(mux, &conf)
153154

docs/credentials.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ This is the credentials' configuration file options
44

55
```yaml
66
aws:
7-
session_token: ""
87
region: ""
98
profile: ""
109
role_arn: ""
11-
role_session_name: ""
12-
web_identity_token_file: ""
13-
external_id: ""
1410
```
1511
1612
## Help links

internal/awshelper/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestNewSessionWithFiles(t *testing.T) {
133133
Expected: map[string]string{
134134
"AWS_ACCESS_KEY_ID": "AKIAIOSFODNN7EXAMPLE",
135135
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
136-
"AWS_REGION": "us-west-1",
136+
"AWS_REGION": "eu-west-1",
137137
"AWS_SHARED_CREDENTIALS_FILE": "SharedConfigCredentials: testdata/case1/credentials",
138138
},
139139
},

internal/collector/collector.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"sync"
2222
"time"
2323

24-
"github.com/aws/aws-sdk-go/aws/session"
2524
"github.com/aws/aws-sdk-go/service/cloudwatch"
2625
"github.com/prometheus/client_golang/prometheus"
2726
"github.com/prometheus/common/log"
@@ -51,16 +50,16 @@ type OwnMetrics struct {
5150

5251
type Collector struct {
5352
conf *config.All
54-
sess *session.Session
53+
svc *cloudwatch.CloudWatch
5554
metrics metrics.Metrics
5655
mutex sync.RWMutex
5756
ownMetrics *OwnMetrics
5857
}
5958

60-
func New(c *config.All, m metrics.Metrics, s *session.Session) *Collector {
59+
func New(c *config.All, m metrics.Metrics, cwc *cloudwatch.CloudWatch) *Collector {
6160
return &Collector{
6261
conf: c,
63-
sess: s,
62+
svc: cwc,
6463
metrics: m,
6564
ownMetrics: &OwnMetrics{
6665
Up: prometheus.NewGauge(prometheus.GaugeOpts{
@@ -209,14 +208,8 @@ func (c *Collector) scrape(ch chan<- prometheus.Metric) {
209208
// number of metrics to be scrape and defined in yaml files
210209
c.ownMetrics.MetricsTotal.Set(float64(len(mdi.MetricDataQueries)))
211210

212-
// TODO: Remove it from here and implement a new interface and package to collect metrics
213-
// but it is here because every time the exporter going to scrape metrics the
214-
// cloudwatch session is refreshed, I need to be sure that credential never expired
215-
// when move it from here
216-
svc := cloudwatch.New(c.sess)
217-
218211
// Scrape CloudWatch Metrics
219-
mdo, err := svc.GetMetricData(mdi)
212+
mdo, err := c.svc.GetMetricData(mdi)
220213
if err != nil {
221214
c.ownMetrics.Up.Set(0)
222215
c.ownMetrics.ScrapesErrors.Inc()

internal/web/handlers.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ func (h *Handlers) Home(w http.ResponseWriter, r *http.Request) {
5555
<li>{{.BuildInfo}}</li>
5656
</ul>
5757
58-
{{ if .Debug }}
59-
<h2>Debug is enabled</h2>
60-
<h3>Links:</h3>
58+
<h2>Go profile is enabled</h2>
6159
<ul>
62-
{{range .DebugLinks}}
60+
{{range .ProfileLinks}}
6361
<li><a href="{{.}}">{{.}}</a></li>
6462
{{ end }}
6563
</ul>
66-
{{ else }}
67-
<h2>Debug is disabled, you cannot see application performance profile</h2>
68-
{{ end }}
6964
7065
<h3><a href="https://prometheus.io/">https://prometheus.io</a></h3>
7166
</body>
@@ -80,8 +75,7 @@ func (h *Handlers) Home(w http.ResponseWriter, r *http.Request) {
8075
HealthPath string
8176
VersionInfo string
8277
BuildInfo string
83-
Debug bool
84-
DebugLinks []string
78+
ProfileLinks []string
8579
}{
8680
h.conf.Application.Name,
8781
h.conf.Application.Name,
@@ -91,7 +85,6 @@ func (h *Handlers) Home(w http.ResponseWriter, r *http.Request) {
9185
h.conf.Application.HealthPath,
9286
h.conf.Application.VersionInfo,
9387
h.conf.Application.BuildInfo,
94-
h.conf.Server.Debug,
9588
[]string{
9689
"/debug/pprof/",
9790
"/debug/pprof/heap",

0 commit comments

Comments
 (0)