forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: switch metrics to Service pattern
- Loading branch information
1 parent
d7a2952
commit e1c717b
Showing
8 changed files
with
81 additions
and
29 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
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,48 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/0xERR0R/blocky/config" | ||
"github.com/0xERR0R/blocky/service" | ||
"github.com/0xERR0R/blocky/util" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
// Service implements service.HTTPService. | ||
type Service struct { | ||
service.HTTPInfo | ||
} | ||
|
||
func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service { | ||
endpoints := util.ConcatSlices( | ||
service.EndpointsFromAddrs(service.HTTPProtocol, cfg.Addrs.HTTP), | ||
service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Addrs.HTTPS), | ||
) | ||
|
||
if !metricsCfg.Enable || len(endpoints) == 0 { | ||
// Avoid setting up collectors and listeners | ||
return new(Service) | ||
} | ||
|
||
s := &Service{ | ||
HTTPInfo: service.HTTPInfo{ | ||
Info: service.Info{ | ||
Name: "Metrics", | ||
Endpoints: endpoints, | ||
}, | ||
|
||
Mux: chi.NewMux(), | ||
}, | ||
} | ||
|
||
s.Mux.Handle( | ||
metricsCfg.Path, | ||
promhttp.InstrumentMetricHandler(reg, promhttp.HandlerFor(reg, promhttp.HandlerOpts{})), | ||
) | ||
|
||
return s | ||
} | ||
|
||
func (s *Service) Merge(other service.Service) (service.Merger, error) { | ||
return service.MergeHTTP(s, other) | ||
} |
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