diff --git a/pkg/command/konnectd.go b/pkg/command/konnectd.go index 459aba7a59..2142dd7f72 100644 --- a/pkg/command/konnectd.go +++ b/pkg/command/konnectd.go @@ -1,3 +1,5 @@ +// +build simple + package command import ( diff --git a/pkg/command/simple.go b/pkg/command/simple.go new file mode 100644 index 0000000000..ab3afc85ee --- /dev/null +++ b/pkg/command/simple.go @@ -0,0 +1,153 @@ +// +build simple + +package command + +import ( + "strings" + "time" + + "contrib.go.opencensus.io/exporter/jaeger" + "contrib.go.opencensus.io/exporter/ocagent" + "contrib.go.opencensus.io/exporter/zipkin" + "github.com/micro/cli" + "github.com/micro/go-micro/config/cmd" + openzipkin "github.com/openzipkin/zipkin-go" + zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http" + "github.com/owncloud/ocis/pkg/config" + "github.com/owncloud/ocis/pkg/flagset" + "github.com/owncloud/ocis/pkg/micro/runtime" + "github.com/owncloud/ocis/pkg/register" + "go.opencensus.io/stats/view" + "go.opencensus.io/trace" +) + +// Simple is the entrypoint for the server command. +func Simple(cfg *config.Config) cli.Command { + return cli.Command{ + Name: "server", + Usage: "Start fullstack server", + Category: "Fullstack", + Flags: flagset.ServerWithConfig(cfg), + Before: func(c *cli.Context) error { + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil + }, + Action: func(c *cli.Context) error { + logger := NewLogger(cfg) + + if cfg.Tracing.Enabled { + switch t := cfg.Tracing.Type; t { + case "agent": + exporter, err := ocagent.NewExporter( + ocagent.WithReconnectionPeriod(5*time.Second), + ocagent.WithAddress(cfg.Tracing.Endpoint), + ocagent.WithServiceName(cfg.Tracing.Service), + ) + + if err != nil { + logger.Error(). + Err(err). + Str("endpoint", cfg.Tracing.Endpoint). + Str("collector", cfg.Tracing.Collector). + Msg("Failed to create agent tracing") + + return err + } + + trace.RegisterExporter(exporter) + view.RegisterExporter(exporter) + + case "jaeger": + exporter, err := jaeger.NewExporter( + jaeger.Options{ + AgentEndpoint: cfg.Tracing.Endpoint, + CollectorEndpoint: cfg.Tracing.Collector, + ServiceName: cfg.Tracing.Service, + }, + ) + + if err != nil { + logger.Error(). + Err(err). + Str("endpoint", cfg.Tracing.Endpoint). + Str("collector", cfg.Tracing.Collector). + Msg("Failed to create jaeger tracing") + + return err + } + + trace.RegisterExporter(exporter) + + case "zipkin": + endpoint, err := openzipkin.NewEndpoint( + cfg.Tracing.Service, + cfg.Tracing.Endpoint, + ) + + if err != nil { + logger.Error(). + Err(err). + Str("endpoint", cfg.Tracing.Endpoint). + Str("collector", cfg.Tracing.Collector). + Msg("Failed to create zipkin tracing") + + return err + } + + exporter := zipkin.NewExporter( + zipkinhttp.NewReporter( + cfg.Tracing.Collector, + ), + endpoint, + ) + + trace.RegisterExporter(exporter) + + default: + logger.Warn(). + Str("type", t). + Msg("Unknown tracing backend") + } + + trace.ApplyConfig( + trace.Config{ + DefaultSampler: trace.AlwaysSample(), + }, + ) + } else { + logger.Debug(). + Msg("Tracing is not enabled") + } + + runtime := runtime.New( + runtime.Services( + append( + runtime.RuntimeServices, + []string{ + "hello", + "konnectd", + "phoenix", + }..., + ), + ), + runtime.Logger(logger), + runtime.MicroRuntime(cmd.DefaultCmd.Options().Runtime), + ) + + // fork uses the micro runtime to fork go-micro services + runtime.Start() + + // trap blocks until a kill signal is sent + runtime.Trap() + + return nil + }, + } +} + +func init() { + register.AddCommand(Simple) +}