Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added foundation for distributor
Signed-off-by: Andrew Neudegg <andrew.neudegg@finbourne.com>
- Loading branch information
1 parent
33d1969
commit 4773ffc
Showing
5 changed files
with
154 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Distributor | ||
|
||
The distributor is the mechanism for transporting messages that have been bridged or sunk from their relative | ||
inputs to target applications. |
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,23 @@ | ||
package apputil | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/andrewneudegg/delta/pkg/probes" | ||
"github.com/andrewneudegg/delta/pkg/telemetry" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// AppState contains the information for the app to run. | ||
type AppState struct { | ||
Probes *probes.ProbeServer | ||
Logger *log.Logger | ||
TelemetryServer *telemetry.PrometheusServer | ||
wg sync.WaitGroup | ||
} | ||
|
||
// Block the application indefinitely on the given context. | ||
func (a *AppState) Block(ctx context.Context) { | ||
<-ctx.Done() | ||
} |
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,93 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/andrewneudegg/delta/cmd/distributor/apputil" | ||
"github.com/andrewneudegg/delta/cmd/distributor/subcmd/naive" | ||
"github.com/andrewneudegg/delta/pkg/probes" | ||
"github.com/andrewneudegg/delta/pkg/telemetry" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
app() | ||
} | ||
|
||
func configureLogger(verbose bool) *log.Logger { | ||
logger := log.New() | ||
|
||
logger.SetFormatter(&log.TextFormatter{ | ||
DisableColors: false, | ||
FullTimestamp: true, | ||
}) | ||
// Output to stdout instead of the default stderr | ||
// Can be any io.Writer, see below for File example | ||
logger.SetOutput(os.Stdout) | ||
|
||
// Only log the warning severity or above. | ||
if verbose { | ||
logger.SetLevel(log.DebugLevel) | ||
} else { | ||
logger.SetLevel(log.InfoLevel) | ||
} | ||
|
||
return logger | ||
} | ||
|
||
func configureProbes(probesEnabled bool) *probes.ProbeServer { | ||
probes := probes.ProbeServer{ | ||
ListenAddr: ":8082", | ||
} | ||
|
||
if probesEnabled { | ||
go probes.StartProbeServer() | ||
} | ||
|
||
probes.AliveNow() | ||
return &probes | ||
} | ||
|
||
func configureTelemetryServer(telemetryEnabled bool) *telemetry.PrometheusServer { | ||
prometheusServer := telemetry.PrometheusServer{ | ||
ListenAddr: ":8081", | ||
Route: "/metrics", | ||
} | ||
|
||
if telemetryEnabled { | ||
go prometheusServer.Serve(context.TODO()) | ||
} | ||
|
||
return &prometheusServer | ||
} | ||
|
||
func app() error { | ||
var verboseMode bool | ||
var telemetryEnabled bool | ||
var probesEnabled bool | ||
|
||
appState := apputil.AppState{ | ||
Probes: nil, | ||
Logger: nil, | ||
TelemetryServer: nil, | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "distributor", | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
appState.Logger = configureLogger(verboseMode) | ||
appState.Probes = configureProbes(probesEnabled) | ||
appState.TelemetryServer = configureTelemetryServer(telemetryEnabled) | ||
return nil | ||
}, | ||
} | ||
|
||
rootCmd.PersistentFlags().BoolVarP(&verboseMode, "verbose", "v", false, "verbose output") | ||
rootCmd.PersistentFlags().BoolVarP(&telemetryEnabled, "telemetry", "", false, "telemetry server") | ||
rootCmd.PersistentFlags().BoolVarP(&probesEnabled, "probes", "", false, "liveness / readiness probes") | ||
|
||
rootCmd.AddCommand(naive.Cmd(&appState)) | ||
return rootCmd.Execute() | ||
} |
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,23 @@ | ||
package naive | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/andrewneudegg/delta/cmd/distributor/apputil" | ||
) | ||
|
||
// Cmd demonstrates how to configure a new subcommand. | ||
func Cmd(appState *apputil.AppState) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "naive", | ||
Short: "naively distribute events to a specific target/s", | ||
Long: `naively distribute events to a specific target/s without any leader election`, | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
appState.Logger.Debug("starting example cmd") | ||
appState.Block(context.TODO()) | ||
}, | ||
} | ||
} |