Skip to content

Commit cc148bc

Browse files
authored
Merge pull request #74 from BattleL/time-format
Time format
2 parents 6ea2e80 + 89029a0 commit cc148bc

File tree

5 files changed

+616
-17
lines changed

5 files changed

+616
-17
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
FROM golang:alpine as build-env
22

3-
RUN apk add git
4-
53
# Copy source + vendor
64
COPY . /go/src/github.com/tencentyun/qcloud-exporter
75
WORKDIR /go/src/github.com/tencentyun/qcloud-exporter
@@ -12,4 +10,8 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -v -a -ldflags
1210

1311
FROM alpine
1412
COPY --from=build-env /go/bin/qcloud_exporter /usr/bin/qcloud_exporter
13+
RUN apk update
14+
#RUN apk add git
15+
RUN apk add curl
16+
RUN apk add tcpdump
1517
ENTRYPOINT ["qcloud_exporter"]

pkg/client/client.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,20 @@ func NewCosClient(cred common.CredentialIface, conf *config.TencentConfig) (*cos
258258
// 用于Get Service 查询, service域名暂时只支持外网
259259
su, _ := url.Parse("http://cos." + conf.Credential.Region + ".myqcloud.com")
260260
b := &cos.BaseURL{BucketURL: nil, ServiceURL: su}
261-
// client := cos.NewClient(b, &http.Client{
262-
// Transport: &cos.AuthorizationTransport{
263-
// SecretID: conf.Credential.AccessKey,
264-
// SecretKey: conf.Credential.SecretKey,
265-
// },
266-
// })
267-
client := cos.NewClient(b, &http.Client{
268-
Transport: common.NewCredentialTransport(cred.GetRole()),
269-
})
261+
client := &cos.Client{}
262+
if conf.Credential.Role == "" {
263+
client = cos.NewClient(b, &http.Client{
264+
Transport: &cos.AuthorizationTransport{
265+
SecretID: conf.Credential.AccessKey,
266+
SecretKey: conf.Credential.SecretKey,
267+
},
268+
})
269+
} else {
270+
client = cos.NewClient(b, &http.Client{
271+
Transport: common.NewCredentialTransport(cred.GetRole()),
272+
})
273+
}
274+
270275
return client, nil
271276
}
272277

pkg/metric/repository.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
9+
810
"github.com/tencentyun/tencentcloud-exporter/pkg/common"
911

1012
"github.com/go-kit/log"
@@ -121,11 +123,11 @@ func (repo *TcmMetricRepositoryImpl) GetSamples(s *TcmSeries, st int64, et int64
121123
}
122124
request.Instances = []*monitor.Instance{instanceFilters}
123125

124-
stStr := time.Unix(st, 0).Format(timeStampFormat)
126+
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
125127
request.StartTime = &stStr
126128
if et != 0 {
127-
etStr := time.Unix(et, 0).Format(timeStampFormat)
128-
request.StartTime = &etStr
129+
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
130+
request.EndTime = &etStr
129131
}
130132

131133
response, err := repo.monitorClient.GetMonitorData(request)
@@ -173,6 +175,7 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch(
173175
return nil, err
174176
}
175177

178+
//level.Info(repo.logger).Log("st", st, "et", et)
176179
request := repo.buildGetMonitorDataRequest(m, seriesList, st, et)
177180
response, err := repo.monitorClient.GetMonitorData(request)
178181
if err != nil {
@@ -217,11 +220,11 @@ func (repo *TcmMetricRepositoryImpl) buildGetMonitorDataRequest(
217220
request.Instances = append(request.Instances, ifilters)
218221
}
219222

220-
stStr := time.Unix(st, 0).Format(timeStampFormat)
223+
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
221224
request.StartTime = &stStr
222225
if et != 0 {
223-
etStr := time.Unix(et, 0).Format(timeStampFormat)
224-
request.StartTime = &etStr
226+
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
227+
request.EndTime = &etStr
225228
}
226229
return request
227230
}

pkg/util/time.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util
2+
3+
import "time"
4+
5+
// FormatTime formats time by zone.
6+
func FormatTime(t time.Time, format string) string {
7+
var local time.Time
8+
_, offset := t.Zone()
9+
if offset == 0 {
10+
local = t.Add(8 * time.Hour)
11+
} else {
12+
local = t
13+
}
14+
return local.Format(format)
15+
}

0 commit comments

Comments
 (0)