Skip to content

Commit

Permalink
Merge pull request #74 from BattleL/time-format
Browse files Browse the repository at this point in the history
Time format
  • Loading branch information
zero3233 authored Sep 7, 2022
2 parents 6ea2e80 + 89029a0 commit cc148bc
Show file tree
Hide file tree
Showing 5 changed files with 616 additions and 17 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM golang:alpine as build-env

RUN apk add git

# Copy source + vendor
COPY . /go/src/github.com/tencentyun/qcloud-exporter
WORKDIR /go/src/github.com/tencentyun/qcloud-exporter
Expand All @@ -12,4 +10,8 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -v -a -ldflags

FROM alpine
COPY --from=build-env /go/bin/qcloud_exporter /usr/bin/qcloud_exporter
RUN apk update
#RUN apk add git
RUN apk add curl
RUN apk add tcpdump
ENTRYPOINT ["qcloud_exporter"]
23 changes: 14 additions & 9 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,20 @@ func NewCosClient(cred common.CredentialIface, conf *config.TencentConfig) (*cos
// 用于Get Service 查询, service域名暂时只支持外网
su, _ := url.Parse("http://cos." + conf.Credential.Region + ".myqcloud.com")
b := &cos.BaseURL{BucketURL: nil, ServiceURL: su}
// client := cos.NewClient(b, &http.Client{
// Transport: &cos.AuthorizationTransport{
// SecretID: conf.Credential.AccessKey,
// SecretKey: conf.Credential.SecretKey,
// },
// })
client := cos.NewClient(b, &http.Client{
Transport: common.NewCredentialTransport(cred.GetRole()),
})
client := &cos.Client{}
if conf.Credential.Role == "" {
client = cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: conf.Credential.AccessKey,
SecretKey: conf.Credential.SecretKey,
},
})
} else {
client = cos.NewClient(b, &http.Client{
Transport: common.NewCredentialTransport(cred.GetRole()),
})
}

return client, nil
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/metric/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/tencentyun/tencentcloud-exporter/pkg/util"

"github.com/tencentyun/tencentcloud-exporter/pkg/common"

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

stStr := time.Unix(st, 0).Format(timeStampFormat)
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
request.StartTime = &stStr
if et != 0 {
etStr := time.Unix(et, 0).Format(timeStampFormat)
request.StartTime = &etStr
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
request.EndTime = &etStr
}

response, err := repo.monitorClient.GetMonitorData(request)
Expand Down Expand Up @@ -173,6 +175,7 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch(
return nil, err
}

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

stStr := time.Unix(st, 0).Format(timeStampFormat)
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
request.StartTime = &stStr
if et != 0 {
etStr := time.Unix(et, 0).Format(timeStampFormat)
request.StartTime = &etStr
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
request.EndTime = &etStr
}
return request
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/util/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

import "time"

// FormatTime formats time by zone.
func FormatTime(t time.Time, format string) string {
var local time.Time
_, offset := t.Zone()
if offset == 0 {
local = t.Add(8 * time.Hour)
} else {
local = t
}
return local.Format(format)
}
Loading

0 comments on commit cc148bc

Please sign in to comment.