-
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.
Merge pull request #43 from cloudnativedaysjp/issue/15
Support some metrics
- Loading branch information
Showing
13 changed files
with
580 additions
and
12 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
bindAddr: :20080 | ||
wsProxy: | ||
bindAddr: :20080 | ||
dreamkast: | ||
eventAbbr: cnsec2022 | ||
endpointUrl: ${DK_ENDPOINT_URL} | ||
|
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,56 @@ | ||
package lib | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
type DreamkastClientWrapper struct { | ||
c DreamkastClient | ||
nowFunc func() time.Time | ||
dkEndpointUrl string | ||
} | ||
|
||
func NewDreamkastClientWrapper(dkEndpointUrl string) (DreamkastClient, error) { | ||
c, err := NewClient(dkEndpointUrl) | ||
if err != nil { | ||
return nil, xerrors.Errorf("message: %w", err) | ||
} | ||
return &DreamkastClientWrapper{ | ||
c, | ||
func() time.Time { return time.Now() }, | ||
dkEndpointUrl, | ||
}, nil | ||
} | ||
|
||
func (w DreamkastClientWrapper) GenerateAuth0Token(ctx context.Context, auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience string) error { | ||
return w.c.GenerateAuth0Token(ctx, auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience) | ||
} | ||
|
||
func (w DreamkastClientWrapper) ListTracks(ctx context.Context, eventAbbr string) (ListTracksResp, error) { | ||
metricsDao := metrics.DreamkastMetricsFromCtx(ctx) | ||
now := w.nowFunc() | ||
result, err := w.c.ListTracks(ctx, eventAbbr) | ||
metricsDao.ListTracks(w.nowFunc().Sub(now)) | ||
return result, err | ||
} | ||
|
||
func (w DreamkastClientWrapper) ListTalks(ctx context.Context, eventAbbr string, trackId int32) (ListTalksResp, error) { | ||
metricsDao := metrics.DreamkastMetricsFromCtx(ctx) | ||
now := w.nowFunc() | ||
result, err := w.c.ListTalks(ctx, eventAbbr, trackId) | ||
metricsDao.ListTalks(w.nowFunc().Sub(now)) | ||
return result, err | ||
} | ||
|
||
func (w DreamkastClientWrapper) UpdateTalk(ctx context.Context, talkId int32, onAir bool) error { | ||
metricsDao := metrics.DreamkastMetricsFromCtx(ctx) | ||
now := w.nowFunc() | ||
err := w.c.UpdateTalk(ctx, talkId, onAir) | ||
metricsDao.UpdateTalk(w.nowFunc().Sub(now)) | ||
return err | ||
|
||
} |
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,25 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func RunCndOperationServer(addr string) error { | ||
m := echo.New() | ||
reg := prometheus.NewRegistry() | ||
RegisterCndOperationServer(reg) | ||
m.GET("/metrics", | ||
echo.WrapHandler(promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))) | ||
if err := m.Start(addr); err != nil { | ||
return xerrors.Errorf("message: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func RegisterCndOperationServer(registry prometheus.Registerer) { | ||
const subsystem = "server" | ||
registerDreamkast(registry, subsystem) | ||
} |
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,89 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// | ||
// Metrics | ||
// | ||
|
||
var ( | ||
dreamkastRequestSummaryVec prometheus.SummaryVec | ||
) | ||
|
||
func registerDreamkast(registry prometheus.Registerer, subsystem string) { | ||
dreamkastRequestSummaryVec = *prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "request", | ||
}, []string{"endpointUrl", "kind"}) | ||
} | ||
|
||
// | ||
// Interface | ||
// | ||
|
||
type DreamkastMetricsIface interface { | ||
ListTracks(time.Duration) | ||
ListTalks(time.Duration) | ||
UpdateTalk(time.Duration) | ||
} | ||
|
||
// | ||
// Data Access Object | ||
// | ||
|
||
type DreamkastMetricsDao struct { | ||
endpointUrl string | ||
} | ||
|
||
func NewDreamkastMetricsDao(endpointUrl string) *DreamkastMetricsDao { | ||
return &DreamkastMetricsDao{endpointUrl} | ||
} | ||
|
||
func (dao DreamkastMetricsDao) ListTracks(d time.Duration) { | ||
dreamkastRequestSummaryVec. | ||
WithLabelValues(dao.endpointUrl, "listTracks").Observe(float64(d)) | ||
} | ||
|
||
func (dao DreamkastMetricsDao) ListTalks(d time.Duration) { | ||
dreamkastRequestSummaryVec. | ||
WithLabelValues(dao.endpointUrl, "listTalks").Observe(float64(d)) | ||
} | ||
|
||
func (dao DreamkastMetricsDao) UpdateTalk(d time.Duration) { | ||
dreamkastRequestSummaryVec. | ||
WithLabelValues(dao.endpointUrl, "updateTalk").Observe(float64(d)) | ||
} | ||
|
||
// | ||
// Fake Object | ||
// | ||
|
||
type DreamkastMetricsFake struct{} | ||
|
||
func (DreamkastMetricsFake) ListTracks(time.Duration) {} | ||
func (DreamkastMetricsFake) ListTalks(time.Duration) {} | ||
func (DreamkastMetricsFake) UpdateTalk(time.Duration) {} | ||
|
||
// | ||
// Utilities | ||
// | ||
|
||
var ctxKeyDreamkastMetrics = contextKey{} | ||
|
||
func SetDreamkastMetricsToCtx(ctx context.Context, m DreamkastMetricsIface) context.Context { | ||
return context.WithValue(ctx, ctxKeyDreamkastMetrics, m) | ||
} | ||
|
||
func DreamkastMetricsFromCtx(ctx context.Context) DreamkastMetricsIface { | ||
dao, ok := ctx.Value(ctxKeyDreamkastMetrics).(DreamkastMetricsDao) | ||
if !ok { | ||
return &DreamkastMetricsFake{} | ||
} | ||
return dao | ||
} |
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,5 @@ | ||
package metrics | ||
|
||
const namespace = "broadcast" | ||
|
||
type contextKey struct{} |