-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add events and metrics systems
- Loading branch information
Showing
10 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ncarlier/reader/pkg/config" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
func metrics(conf *config.Config) http.Handler { | ||
return promhttp.Handler() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,4 +57,10 @@ var routes = Routes{ | |
true, | ||
varz, | ||
}, | ||
Route{ | ||
[]string{"GET"}, | ||
"/metrics", | ||
true, | ||
metrics, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package event | ||
|
||
import ( | ||
"github.com/ncarlier/reader/pkg/metric" | ||
"github.com/ncarlier/reader/pkg/model" | ||
) | ||
|
||
func init() { | ||
bus.Subscribe(CreateArticle, func(payload ...interface{}) { | ||
if article, ok := payload[0].(model.Article); ok { | ||
metric.IncNewArticlesCounter(article) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package event | ||
|
||
import ( | ||
"github.com/asaskevich/EventBus" | ||
) | ||
|
||
var bus = EventBus.New() | ||
|
||
const ( | ||
// CreateUser is the create event on an user | ||
CreateUser = "user:create" | ||
// UpdateUser is the update event on an user | ||
UpdateUser = "user:update" | ||
// DeleteUser is the delete event on an user | ||
DeleteUser = "user:delete" | ||
// CreateArticle is the create event on an article | ||
CreateArticle = "article:create" | ||
) | ||
|
||
// Emit trigger an event to events listenners | ||
func Emit(event string, payload ...interface{}) { | ||
bus.Publish(event, payload...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package metric | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ncarlier/reader/pkg/model" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var ( | ||
createdArticles = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "articles_created_total", | ||
Help: "The total number of created articles by user", | ||
}, []string{"uid"}) | ||
) | ||
|
||
// IncNewArticlesCounter increments the counter of new articles | ||
func IncNewArticlesCounter(article model.Article) { | ||
createdArticles.With(prometheus.Labels{"uid": fmt.Sprint(article.UserID)}).Inc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package metric | ||
|
||
import ( | ||
"expvar" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
var startTime = time.Now().UTC() | ||
|
||
func goroutines() interface{} { | ||
return runtime.NumGoroutine() | ||
} | ||
|
||
func uptime() interface{} { | ||
uptime := time.Since(startTime) | ||
return int64(uptime) | ||
} | ||
|
||
func init() { | ||
expvar.Publish("goroutines", expvar.Func(goroutines)) | ||
expvar.Publish("uptime", expvar.Func(uptime)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters