-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[content-service] Refactor to use baseserver #9973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,11 @@ | |
package cmd | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
|
||
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc" | ||
"github.com/gitpod-io/gitpod/common-go/baseserver" | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/gitpod-io/gitpod/common-go/pprof" | ||
"github.com/gitpod-io/gitpod/content-service/api" | ||
"github.com/gitpod-io/gitpod/content-service/pkg/service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// runCmd starts the content service | ||
|
@@ -32,106 +19,48 @@ var runCmd = &cobra.Command{ | |
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg := getConfig() | ||
reg := prometheus.NewRegistry() | ||
|
||
common_grpc.SetupLogging() | ||
|
||
grpcMetrics := grpc_prometheus.NewServerMetrics() | ||
grpcMetrics.EnableHandlingTimeHistogram() | ||
reg.MustRegister(grpcMetrics) | ||
|
||
grpcOpts := common_grpc.ServerOptionsWithInterceptors( | ||
[]grpc.StreamServerInterceptor{grpcMetrics.StreamServerInterceptor()}, | ||
[]grpc.UnaryServerInterceptor{grpcMetrics.UnaryServerInterceptor()}, | ||
srv, err := baseserver.New("content-service", | ||
baseserver.WithGRPC(&cfg.Service), | ||
) | ||
tlsOpt, err := cfg.Service.TLS.ServerOption() | ||
if err != nil { | ||
log.WithError(err).Fatal("cannot use TLS config") | ||
} | ||
if tlsOpt != nil { | ||
log.WithField("crt", cfg.Service.TLS.Certificate).WithField("key", cfg.Service.TLS.PrivateKey).Debug("securing gRPC server with TLS") | ||
grpcOpts = append(grpcOpts, tlsOpt) | ||
} else { | ||
log.Warn("no TLS configured - gRPC server will be unsecured") | ||
log.WithError(err).Fatal("Failed to create server.") | ||
} | ||
|
||
server := grpc.NewServer(grpcOpts...) | ||
|
||
contentService, err := service.NewContentService(cfg.Storage) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot create content service") | ||
log.WithError(err).Fatalf("Cannot create content service") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please leave this lowercase. The vast majority of our error messages is lowercase, and we should keep things consistent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General practice in Go (and elsewhere) is to uppercase logs to make them sentences. They are designed to be consumed by humans and first letter uppercase is slightly easier to visually grep. Even our style guide uses first letter uppercase in logs https://github.com/uber-go/guide/blob/master/style.md#reduce-nesting The change here is really just a drive by improvement to be more consistent with general practice. Errors, on the other hand, should never start with an uppercase because they get wrapped and composed so don't really form sentences. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's another reference from go code review style guide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point. /hold cancel |
||
} | ||
api.RegisterContentServiceServer(server, contentService) | ||
api.RegisterContentServiceServer(srv.GRPC(), contentService) | ||
|
||
blobService, err := service.NewBlobService(cfg.Storage) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot create blobs service") | ||
log.WithError(err).Fatalf("Cannot create blobs service") | ||
} | ||
api.RegisterBlobServiceServer(server, blobService) | ||
api.RegisterBlobServiceServer(srv.GRPC(), blobService) | ||
|
||
workspaceService, err := service.NewWorkspaceService(cfg.Storage) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot create workspace service") | ||
log.WithError(err).Fatalf("Cannot create workspace service") | ||
} | ||
api.RegisterWorkspaceServiceServer(server, workspaceService) | ||
api.RegisterWorkspaceServiceServer(srv.GRPC(), workspaceService) | ||
|
||
headlessLogService, err := service.NewHeadlessLogService(cfg.Storage) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot create log service") | ||
log.WithError(err).Fatalf("Cannot create log service") | ||
} | ||
api.RegisterHeadlessLogServiceServer(server, headlessLogService) | ||
api.RegisterHeadlessLogServiceServer(srv.GRPC(), headlessLogService) | ||
|
||
idePluginService, err := service.NewIDEPluginService(cfg.Storage) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot create IDE Plugin service") | ||
} | ||
api.RegisterIDEPluginServiceServer(server, idePluginService) | ||
|
||
lis, err := net.Listen("tcp", cfg.Service.Addr) | ||
if err != nil { | ||
log.WithError(err).Fatalf("cannot listen on %s", cfg.Service.Addr) | ||
} | ||
go func() { | ||
err := server.Serve(lis) | ||
if err != nil { | ||
log.WithError(err).Fatal("cannot start server") | ||
} | ||
}() | ||
log.WithField("addr", cfg.Service.Addr).Info("started gRPC server") | ||
|
||
if cfg.Prometheus.Addr != "" { | ||
reg.MustRegister( | ||
collectors.NewGoCollector(), | ||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), | ||
) | ||
|
||
handler := http.NewServeMux() | ||
handler.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) | ||
|
||
go func() { | ||
err := http.ListenAndServe(cfg.Prometheus.Addr, handler) | ||
if err != nil { | ||
log.WithError(err).Error("Prometheus metrics server failed") | ||
} | ||
}() | ||
log.WithField("addr", cfg.Prometheus.Addr).Info("started Prometheus metrics server") | ||
} | ||
|
||
if cfg.PProf.Addr != "" { | ||
go pprof.Serve(cfg.PProf.Addr) | ||
log.WithError(err).Fatalf("Cannot create IDE Plugin service") | ||
} | ||
api.RegisterIDEPluginServiceServer(srv.GRPC(), idePluginService) | ||
|
||
err = srv.ListenAndServe() | ||
if err != nil { | ||
log.WithError(err).Fatal("cannot start daemon") | ||
log.WithError(err).Fatal("Cannot start server") | ||
} | ||
|
||
// run until we're told to stop | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||
log.Info("🧫 content-service is up and running. Stop with SIGINT or CTRL+C") | ||
<-sigChan | ||
server.Stop() | ||
log.Info("Received SIGINT - shutting down") | ||
}, | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLS config is now part of
baseserver
so using it directly.