Skip to content

Commit

Permalink
add debug server to activitylog & check web availability
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Oct 9, 2024
1 parent 7ed5b9f commit 956df08
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 19 deletions.
29 changes: 11 additions & 18 deletions services/activitylog/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
microstore "go-micro.dev/v4/store"

"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis-pkg/service/debug"
ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
Expand All @@ -25,6 +23,7 @@ import (
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/logging"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/metrics"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/server/http"
)

Expand Down Expand Up @@ -145,25 +144,19 @@ func Server(cfg *config.Config) *cli.Command {
}

{
checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(logger),
)

server := debug.NewService(
debugServer, err := debug.Server(
debug.Logger(logger),
debug.Name(cfg.Service.Name),
debug.Version(version.GetString()),
debug.Address(cfg.Debug.Addr),
debug.Token(cfg.Debug.Token),
debug.Pprof(cfg.Debug.Pprof),
debug.Zpages(cfg.Debug.Zpages),
debug.Health(checkHandler),
debug.Ready(checkHandler),
debug.Context(ctx),
debug.Config(cfg),
)

gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
if err != nil {
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
return err
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})
}
Expand Down
50 changes: 50 additions & 0 deletions services/activitylog/pkg/server/debug/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package debug

import (
"context"

"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config"
)

// Option defines a single option function.
type Option func(o *Options)

// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Config *config.Config
}

// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}

for _, o := range opts {
o(&opt)
}

return opt
}

// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}

// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}

// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
40 changes: 40 additions & 0 deletions services/activitylog/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package debug

import (
"context"
"fmt"
"net/http"

"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"github.com/owncloud/ocis/v2/ocis-pkg/service/debug"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
)

// Server initializes the debug service and server.
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)

checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger).WithCheck("nats reachability", func(ctx context.Context) error {
_, err := stream.NatsFromConfig("healthcheckfornats", false, stream.NatsConfig(options.Config.Events))
if err != nil {
return fmt.Errorf("could not connect to nats server: %v", err)
}
return nil
}),
)

return debug.NewService(
debug.Logger(options.Logger),
debug.Name(options.Config.Service.Name),
debug.Version(version.GetString()),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(checkHandler),
debug.Ready(checkHandler),
), nil
}
18 changes: 17 additions & 1 deletion services/web/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package debug

import (
"context"
"fmt"
"net"
"net/http"

"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
Expand All @@ -14,7 +17,20 @@ func Server(opts ...Option) (*http.Server, error) {

checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger),
WithLogger(options.Logger).WithCheck("web reachability", func(ctx context.Context) error {
conn, err := net.Dial("tcp", options.Config.HTTP.Addr)
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
return
}
}(conn)
if err != nil {
return fmt.Errorf("could not connect to web server: %v", err)
}

return nil
}),
)

return debug.NewService(
Expand Down

0 comments on commit 956df08

Please sign in to comment.