Skip to content

Commit

Permalink
Merge pull request #43 from cloudnativedaysjp/issue/15
Browse files Browse the repository at this point in the history
Support some metrics
  • Loading branch information
ShotaKitazawa authored Oct 11, 2022
2 parents 9d4cb76 + fd91e07 commit 3c9771a
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 12 deletions.
17 changes: 13 additions & 4 deletions cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ func LoadConf(filename string) (*Config, error) {
}

type Config struct {
Debug DebugConfig `json:"debug"`
WsProxyBindAddr string `json:"bindAddr" default:":20080"`
Dreamkast DreamkastConfig `json:"dreamkast" validate:"required"`
Tracks []TrackConfig `json:"tracks" validate:"required"`
Debug DebugConfig `json:"debug"`
WsProxy WsProxyConfig `json:"wsProxy" validate:"required"`
Metrics MetricsConfig `json:"metrics"`
Dreamkast DreamkastConfig `json:"dreamkast" validate:"required"`
Tracks []TrackConfig `json:"tracks" validate:"required"`
}

type DebugConfig struct {
Expand All @@ -50,6 +51,14 @@ type DebugConfig struct {
DisableWsProxy bool `json:"disableWsProxy"`
}

type MetricsConfig struct {
BindAddr string `json:"bindAddr" default:":20081"`
}

type WsProxyConfig struct {
BindAddr string `json:"bindAddr" default:":20080"`
}

type DreamkastConfig struct {
EventAbbr string `json:"eventAbbr" validate:"required"`
EndpointUrl string `json:"endpointUrl" validate:"required"`
Expand Down
8 changes: 7 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cloudnativedaysjp/cnd-operation-server/cmd/server/config"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/dkwatcher"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/notifier"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/obswatcher"
Expand All @@ -35,6 +36,11 @@ func main() {
eg, ctx := errgroup.WithContext(ctx)
talkStream := make(chan model.CurrentAndNextTalk, 16)

// metrics
go func() {
_ = metrics.RunCndOperationServer(conf.Metrics.BindAddr)
}()

// obswatcher
if !conf.Debug.DisableObsWatcher {
eg.Go(func() error {
Expand Down Expand Up @@ -102,7 +108,7 @@ func main() {
return server.Run(ctx, server.Config{
Development: conf.Debug.Development,
Debug: conf.Debug.Debug,
BindAddr: conf.WsProxyBindAddr,
BindAddr: conf.WsProxy.BindAddr,
Obs: configObs,
})
})
Expand Down
3 changes: 2 additions & 1 deletion development/cnd-operation-server.yaml
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}
Expand Down
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/google/go-cmp v0.5.9
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/k0kubun/pp v3.0.1+incompatible
github.com/labstack/echo/v4 v4.9.0
github.com/prometheus/client_golang v1.13.0
github.com/slack-go/slack v0.11.3
github.com/spf13/cobra v1.5.0
go.uber.org/zap v1.23.0
Expand All @@ -25,24 +27,33 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
366 changes: 365 additions & 1 deletion go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/dkwatcher/dkwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils"
)
Expand Down Expand Up @@ -47,6 +48,7 @@ func Run(ctx context.Context, conf Config) error {
}
logger := zapr.NewLogger(zapLogger).WithName(componentName)
ctx = logr.NewContext(ctx, logger)
ctx = metrics.SetDreamkastMetricsToCtx(ctx, metrics.NewDreamkastMetricsDao(conf.DkEndpointUrl))

dkClient, err := dreamkast.NewClient(conf.EventAbbr, conf.DkEndpointUrl,
conf.Auth0Domain, conf.Auth0ClientId, conf.Auth0ClientSecret, conf.Auth0ClientAudience)
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/dreamkast/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *ClientImpl) setSpecifiedTalkOnAir(ctx context.Context, talkId int32) er
return xerrors.Errorf("message: %w", err)
}

if err := c.client.UpdateTalks(ctx, talkId, true); err != nil {
if err := c.client.UpdateTalk(ctx, talkId, true); err != nil {
return xerrors.Errorf("message: %w", err)
}
return nil
Expand Down Expand Up @@ -160,7 +160,7 @@ func (c *ClientImpl) setNextTalkOnAir(ctx context.Context, trackId int32) error
}
}

if err := c.client.UpdateTalks(ctx, nextTalkId, true); err != nil {
if err := c.client.UpdateTalk(ctx, nextTalkId, true); err != nil {
return xerrors.Errorf("message: %w", err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/dreamkast/lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DreamkastClient interface {
GenerateAuth0Token(ctx context.Context, auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience string) error
ListTracks(ctx context.Context, eventAbbr string) (ListTracksResp, error)
ListTalks(ctx context.Context, eventAbbr string, trackId int32) (ListTalksResp, error)
UpdateTalks(ctx context.Context, talkId int32, onAir bool) error
UpdateTalk(ctx context.Context, talkId int32, onAir bool) error
}

type DreamkastClientImpl struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/dreamkast/lib/talks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *DreamkastClientImpl) ListTalks(ctx context.Context, eventAbbr string, t

// TODO: set Token to header
// TODO: write test
func (c *DreamkastClientImpl) UpdateTalks(ctx context.Context, talkId int32, onAir bool) error {
func (c *DreamkastClientImpl) UpdateTalk(ctx context.Context, talkId int32, onAir bool) error {
url := c.dkEndpointUrl
url.Path = filepath.Join(url.Path, "/v1/talks", strconv.Itoa(int(talkId)))
reqBody, err := json.Marshal(&UpdateTalksReq{OnAir: onAir})
Expand Down
56 changes: 56 additions & 0 deletions pkg/infra/dreamkast/lib/wrapper.go
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

}
25 changes: 25 additions & 0 deletions pkg/metrics/cnd-operation-server.go
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)
}
89 changes: 89 additions & 0 deletions pkg/metrics/dreamkast.go
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
}
5 changes: 5 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package metrics

const namespace = "broadcast"

type contextKey struct{}

0 comments on commit 3c9771a

Please sign in to comment.