Skip to content

Commit

Permalink
#14: Introduced a new telemetry struct to hold all observability comp…
Browse files Browse the repository at this point in the history
…onents
  • Loading branch information
roma-glushko committed Dec 21, 2023
1 parent e3808ef commit d17a295
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
20 changes: 11 additions & 9 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package pkg
import (
"context"
"fmt"
"glide/pkg/telemetry"
"go.uber.org/zap"
"os"
"os/signal"
"syscall"

"glide/pkg/telemetry"
"go.uber.org/zap"

"glide/pkg/api"
"glide/pkg/api/http"

Expand All @@ -18,6 +19,8 @@ import (
// Gateway represents an instance of running Glide gateway.
// It loads configs, start API server(s), and listen to termination signals to shut down
type Gateway struct {
// telemetry holds logger, meter, and tracer
telemetry *telemetry.Telemetry
// serverManager controls API over different protocols
serverManager *api.ServerManager
// signalChannel is used to receive termination signals from the OS.
Expand All @@ -32,8 +35,7 @@ func NewGateway() (*Gateway, error) {
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
logConfig.Encoding = "console"

logger, err := telemetry.NewLogger(logConfig)

tel, err := telemetry.NewTelemetry(&telemetry.Config{LogConfig: logConfig})
if err != nil {
return nil, err
}
Expand All @@ -44,7 +46,7 @@ func NewGateway() (*Gateway, error) {
}

return &Gateway{
logger: logger,
telemetry: tel,
serverManager: serverManager,
signalC: make(chan os.Signal, 3), // equal to number of signal types we expect to receive
shutdownC: make(chan struct{}),
Expand All @@ -63,14 +65,14 @@ LOOP:
for {
select {
// TODO: Watch for config updates
case <-gw.signalC:
// TODO: log this occurrence
case sig := <-gw.signalC:
gw.telemetry.Logger().Info("Received signal from OS", zap.String("signal", sig.String()))
break LOOP
case <-gw.shutdownC:
// TODO: log this occurrence
gw.telemetry.Logger().Info("received shutdown request")
break LOOP
case <-ctx.Done():
// TODO: log this occurrence
gw.telemetry.Logger().Info("context done, terminating process")
// Call shutdown with background context as the passed in context has been canceled
return gw.shutdown(context.Background())
}
Expand Down
14 changes: 4 additions & 10 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package telemetry

import "go.uber.org/zap"

type TelemetryConfig struct {
type Config struct {
LogConfig *LogConfig `json:"logs" yaml:"logs"`
// TODO: add OTEL config
}
Expand All @@ -12,21 +12,15 @@ type Telemetry struct {
// TODO: add OTEL meter, tracer
}

func NewTelemetry(cfg *TelemetryConfig) (*Telemetry, error) {
// TODO: gonna be read from a config file
logConfig := NewLogConfig()
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
logConfig.Encoding = "console"

logger, err := telemetry.NewLogger(logConfig)

func NewTelemetry(cfg *Config) (*Telemetry, error) {
logger, err := NewLogger(cfg.LogConfig)
if err != nil {
return nil, err
}

return &Telemetry{
logger: logger,
}
}, nil
}

func (t *Telemetry) Logger() *zap.Logger {
Expand Down

0 comments on commit d17a295

Please sign in to comment.