Skip to content

Commit

Permalink
Merge pull request #7 from isaqueveras/release/export-fields
Browse files Browse the repository at this point in the history
feat: add tag to export fields
  • Loading branch information
isaqueveras authored May 25, 2024
2 parents 47d412a + 3839f5f commit ac53be4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 73 deletions.
39 changes: 20 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
type Script func(*Context)

type Context struct {
Id ID
RoutineID ID
Name string
Desc string
StartHour uint
EndHour uint
Interval time.Duration
LoadInterval time.Duration
Path string
StartedAt time.Time
Id ID `json:"id,omitempty"`
RoutineID ID `json:"routine_id,omitempty"`
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
Start uint `json:"start,omitempty"`
End uint `json:"end,omitempty"`
Interval time.Duration `json:"interval,omitempty"`
LoadInterval time.Duration `json:"load_interval,omitempty"`
Path string `json:"path,omitempty"`
RunAt time.Time `json:"run_at,omitempty"`
Watcher Watch `json:"-"`

script Script
metadata Metadata
Expand All @@ -28,7 +29,7 @@ type Context struct {
indicator []*indicator

// L define the log layer interface
L Logger
L Logger `json:"-"`
}

func (ctx *Context) Error(message string, args ...interface{}) {
Expand Down Expand Up @@ -99,15 +100,15 @@ func (ctx *Context) metrics(w *Watch, now time.Time) {
Indicator: ctx.indicator,
Histogram: ctx.histrogram,
Watcher: WatcherMetric{
ID: w.id.ToString(),
Name: w.name,
StartedAt: w.startedAt,
ID: w.Id.ToString(),
Name: w.Name,
StartedAt: w.RunAt,
},
Routine: RoutineMetric{
ID: ctx.RoutineID.ToString(),
Name: ctx.Name,
Path: ctx.Path,
StartedAt: ctx.StartedAt,
StartedAt: ctx.RunAt,
},
})

Expand All @@ -116,15 +117,15 @@ func (ctx *Context) metrics(w *Watch, now time.Time) {
}

func (ctx *Context) isTime(hour int) bool {
if ctx.StartHour == 0 && ctx.EndHour == 0 {
if ctx.Start == 0 && ctx.End == 0 {
return true
}

if ctx.StartHour <= ctx.EndHour {
return (hour >= int(ctx.StartHour) && hour <= int(ctx.EndHour))
if ctx.Start <= ctx.End {
return (hour >= int(ctx.Start) && hour <= int(ctx.End))
}

return (hour >= int(ctx.StartHour) || hour <= int(ctx.EndHour))
return (hour >= int(ctx.Start) || hour <= int(ctx.End))
}

func (ctx *Context) validate() error {
Expand Down
71 changes: 38 additions & 33 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

```json
{
"id": "3463095254098643911",
"latency": 48514700,
"started_at": "2024-05-22T19:34:01.072143938-03:00",
"finished_at": "2024-05-22T19:34:01.120658802-03:00",
"id": "5388751619798277776",
"latency": 574336154,
"started_at": "2024-05-24T22:19:40.390569286-03:00",
"finished_at": "2024-05-24T22:19:40.96490561-03:00",
"watcher": {
"id": "8b1d6a18-5f3d-4482-a574-35d3965c8783",
"name": "v1/example",
"started_at": "2024-05-22T19:33:58.178673456-03:00"
"id": "79e4b3cb-f280-4511-bf95-d4157584baba",
"name": "/v1/example/outis",
"started_at": "2024-05-24T22:19:39.389977699-03:00"
},
"routine": {
"id": "0b2d07ca-e3db-478a-9455-d5f476ac8d37",
"name": "Example routine",
"path": "/outis/example/main.go:23",
"started_at": "2024-05-22T19:33:58.17873695-03:00"
"routine_id": "f92f61a9-2ddc-4025-af64-212bfda3e151",
"name": "Teste da minha rotina",
"path": "/outis/example/main.go:24",
"started_at": "2024-05-24T22:19:39.390018896-03:00"
},
"metadata": {
"client": {
Expand Down Expand Up @@ -45,68 +45,73 @@
{
"key": "customers_notified",
"value": 13.21,
"created_at": "2024-05-22T19:34:01.108226823-03:00"
"created_at": "2024-05-24T22:19:40.782066655-03:00"
}
],
"histograms": [
{
"key": "problems_resolved",
"values": [
{
"value": 10,
"created_at": "2024-05-22T19:34:01.082507408-03:00"
"value": 48,
"created_at": "2024-05-24T22:19:40.438977176-03:00"
},
{
"value": 3,
"created_at": "2024-05-22T19:34:01.085635542-03:00"
"value": 107,
"created_at": "2024-05-24T22:19:40.546351975-03:00"
},
{
"value": 10,
"created_at": "2024-05-22T19:34:01.095840753-03:00"
"value": 101,
"created_at": "2024-05-24T22:19:40.647606016-03:00"
},
{
"value": 2,
"created_at": "2024-05-22T19:34:01.098013473-03:00"
"value": 100,
"created_at": "2024-05-24T22:19:40.747834238-03:00"
},
{
"value": 10,
"created_at": "2024-05-22T19:34:01.108188919-03:00"
"value": 34,
"created_at": "2024-05-24T22:19:40.782038289-03:00"
}
]
},
{
"key": "canceled_processes",
"values": [
{
"value": 8,
"created_at": "2024-05-22T19:34:01.116398926-03:00"
"value": 16,
"created_at": "2024-05-24T22:19:40.798237357-03:00"
},
{
"value": 4,
"created_at": "2024-05-22T19:34:01.120625851-03:00"
"value": 61,
"created_at": "2024-05-24T22:19:40.859567254-03:00"
},
{
"value": 0,
"created_at": "2024-05-22T19:34:01.120638405-03:00"
"value": 105,
"created_at": "2024-05-24T22:19:40.964847216-03:00"
}
]
}
],
"logs": [
{
"level": "INFO",
"message": "script 'Example routine' (rid: 0b2d07ca-e3db-478a-9455-d5f476ac8d37, id: 3463095254098643911) initialized",
"timestamp": "2024-05-22T19:34:01.072212527-03:00"
"message": "script 'Teste da minha rotina' (rid: f92f61a9-2ddc-4025-af64-212bfda3e151) initialized",
"timestamp": "2024-05-24T22:19:39.390109993-03:00"
},
{
"level": "INFO",
"message": "script 'Teste da minha rotina' (rid: f92f61a9-2ddc-4025-af64-212bfda3e151, id: 5388751619798277776) initialized",
"timestamp": "2024-05-24T22:19:40.390630463-03:00"
},
{
"level": "DEBUG",
"message": "Hello 02",
"timestamp": "2024-05-22T19:34:01.108226308-03:00"
"timestamp": "2024-05-24T22:19:40.782066143-03:00"
},
{
"level": "INFO",
"message": "script 'Example routine' (rid: 0b2d07ca-e3db-478a-9455-d5f476ac8d37, id: 3463095254098643911) finished",
"timestamp": "2024-05-22T19:34:01.120658155-03:00"
"message": "script 'Teste da minha rotina' (rid: f92f61a9-2ddc-4025-af64-212bfda3e151, id: 5388751619798277776) finished",
"timestamp": "2024-05-24T22:19:40.96490507-03:00"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type EventMetric struct {
}

type RoutineMetric struct {
ID string `json:"id"`
ID string `json:"routine_id"`
Name string `json:"name"`
Path string `json:"path"`
StartedAt time.Time `json:"started_at"`
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func WithScript(routine Script) Option {

func WithHours(start, end uint) Option {
return func(r *Context) {
r.StartHour, r.EndHour = start, end
r.Start, r.End = start, end
}
}

Expand Down
39 changes: 20 additions & 19 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ func (id ID) ToString() string {
}

type Watch struct {
id ID
name string
signal chan os.Signal
startedAt time.Time
Id ID `json:"id"`
Name string `json:"name"`
RunAt time.Time `json:"run_at"`

outis Outis
log Logger
signal chan os.Signal
outis Outis
log Logger
}

func Watcher(id, name string, opts ...WatcherOption) *Watch {
watch := &Watch{
id: ID(id),
name: name,
signal: make(chan os.Signal, 1),
log: setupLogger(),
outis: newOutis(),
startedAt: time.Now(),
Id: ID(id),
Name: name,
signal: make(chan os.Signal, 1),
log: setupLogger(),
outis: newOutis(),
RunAt: time.Now(),
}

for _, opt := range opts {
Expand All @@ -50,19 +50,20 @@ func Watcher(id, name string, opts ...WatcherOption) *Watch {
func (w *Watch) Wait() {
for range w.signal {
w.log.Infof("closing signal received")
break
return
}
}

func (w *Watch) Go(opts ...Option) {
ctx := &Context{
Watcher: *w,
indicator: make([]*indicator, 0),
metadata: make(Metadata),
logs: make([]Log, 0),
LoadInterval: 0,
L: w.log,
Interval: time.Minute,
StartedAt: time.Now(),
RunAt: time.Now(),
}

for _, opt := range opts {
Expand All @@ -77,11 +78,6 @@ func (w *Watch) Go(opts ...Option) {
return
}

if err := w.outis.Init(ctx); err != nil {
ctx.Error("%v", err)
return
}

info := runtime.FuncForPC(reflect.ValueOf(ctx.script).Pointer())
file, line := info.FileLine(info.Entry())
ctx.Path = fmt.Sprintf("%s:%v", file, line)
Expand All @@ -90,6 +86,11 @@ func (w *Watch) Go(opts ...Option) {
go ctx.reload(w.outis)
}

if err := w.outis.Init(ctx); err != nil {
ctx.Error("%v", err)
return
}

defer func() {
if r := recover(); r != nil {
ctx.Error("%v", r)
Expand Down

0 comments on commit ac53be4

Please sign in to comment.