Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added telemetry middleware for routing.
Signed-off-by: Andrew Neudegg <andrew.neudegg@finbourne.com>
- Loading branch information
1 parent
871d448
commit db1fa3f
Showing
2 changed files
with
53 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/andrewneudegg/delta/pkg/events" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
// EventMiddlewareFunc is a function that will be called on each event. | ||
type EventMiddlewareFunc func(events.Event) | ||
|
||
// Inject wraps the given channel with another channel that actions the middleware on each item in a separate goroutine. | ||
func Inject(ch chan events.Event, middleware EventMiddlewareFunc) chan events.Event { | ||
newOutPipe := make(chan events.Event) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case e := <-ch: | ||
go middleware(e) | ||
newOutPipe <- e | ||
} | ||
} | ||
}() | ||
|
||
return newOutPipe | ||
} | ||
|
||
// NoopEventMiddleware does not do anything, but matches the prometheus definition. | ||
func NoopEventMiddleware(counterName string) func(events.Event) { | ||
return func(e events.Event) {} | ||
} | ||
|
||
// PrometheusTelemetryEventMiddleware will increment a counter with the given name. | ||
func PrometheusTelemetryEventMiddleware(counterName string) func(events.Event) { | ||
|
||
counter := promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: counterName, | ||
Help: fmt.Sprintf("autogenerated counter for '%s'", counterName), | ||
}) | ||
|
||
return func(e events.Event) { | ||
counter.Add(1) | ||
} | ||
} |
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