Skip to content

Commit

Permalink
[rk-common] Fix time string issues for duration [commit:dongxuny]
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxuny committed Nov 8, 2020
1 parent 602b3db commit a62aa86
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
13 changes: 11 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
// Global application context
GlobalAppCtx = &appContext{
application: "unknown-application",
startTime: time.Unix(0, 0),
startTime: time.Now(),
loggers: make(map[string]*LoggerPair, 0),
viperConfigs: make(map[string]*viper.Viper, 0),
rkConfigs: make(map[string]*rk_config.RkConfig, 0),
Expand All @@ -31,6 +31,15 @@ var (
}
)

func init() {
signal.Notify(GlobalAppCtx.shutdownSig,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGKILL,
syscall.SIGTERM,
syscall.SIGQUIT)
}

// Contains zap.logger and zap.config
// Make sure zap.logger was built from zap.config in order to
// dynamically change zap.logger attributes
Expand Down Expand Up @@ -102,7 +111,7 @@ func WithEventFactory(fac *rk_query.EventFactory) appContextOption {
func NewAppContext(opts ...appContextOption) *appContext {
ctx := &appContext{
application: "unknown-application",
startTime: time.Unix(0, 0),
startTime: time.Now(),
loggers: make(map[string]*LoggerPair, 0),
viperConfigs: make(map[string]*viper.Viper, 0),
rkConfigs: make(map[string]*rk_config.RkConfig, 0),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 h1:BpJ2o0OR5FV7vrkDYfXYVJQeMNWa8RhklZOpW2ITAIQ=
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
7 changes: 5 additions & 2 deletions info/basic_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package rk_info

import (
"github.com/hako/durafmt"
"github.com/rookie-ninja/rk-common/context"
"go.uber.org/zap"
"os"
Expand All @@ -18,7 +19,8 @@ type BasicInfo struct {
GID string `json:"gid"`
Username string `json:"username"`
StartTime string `json:"start_time"`
UpTime string `json:"up_time"`
UpTimeSec int64 `json:"up_time_sec"`
UpTimeStr string `json:"up_time_str"`
Application string `json:"application"`
Region string `json:"region"`
AZ string `json:"az"`
Expand All @@ -33,7 +35,8 @@ func BasicInfoToStruct() *BasicInfo {
UID: u.Uid,
GID: u.Gid,
StartTime: rk_ctx.GlobalAppCtx.GetStartTime().Format(time.RFC3339),
UpTime: rk_ctx.GlobalAppCtx.GetUpTime().String(),
UpTimeSec: int64(rk_ctx.GlobalAppCtx.GetUpTime().Seconds()),
UpTimeStr: durafmt.ParseShort(rk_ctx.GlobalAppCtx.GetUpTime()).String(),
Application: rk_ctx.GlobalAppCtx.GetApplication(),
Realm: os.Getenv("REALM"),
Region: os.Getenv("REGION"),
Expand Down
5 changes: 3 additions & 2 deletions info/mem_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MemStats struct {
SysAllocMB uint64 `json:"sys_alloc_byte"`
MemPercentage float64`json:"mem_usage_percentage"`
LastGCTimestamp string `json:"last_gc_timestamp"`
GCCount uint32 `json:"gc_count"`
GCCount uint32 `json:"gc_count_total"`
ForceGCCount uint32 `json:"force_gc_count"`
}

Expand All @@ -28,7 +28,8 @@ func MemStatsToStruct() *MemStats {
return &MemStats{
MemAllocMB: stats.Alloc,
SysAllocMB: stats.Sys,
LastGCTimestamp: time.Unix(int64(stats.LastGC), 0).Format(time.RFC3339),
MemPercentage: float64(stats.Alloc)/float64(stats.Sys),
LastGCTimestamp: time.Unix(0, int64(stats.LastGC)).Format(time.RFC3339),
GCCount: stats.NumGC,
ForceGCCount: stats.NumForcedGC,
}
Expand Down

0 comments on commit a62aa86

Please sign in to comment.