cmd

package
v0.0.0-...-82de52e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: BSD-3-Clause Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "erlenmeyer",
	Short: "A proxy that translates queries to Warp10",
	Run: func(cmd *cobra.Command, args []string) {

		r := echo.New()
		addr := viper.GetString("listen")
		sentryDSN := viper.GetString("sentry")

		r.Logger.SetOutput(ioutil.Discard)

		r.Pre(middleware.Rewrite(map[string]string{
			"/warp10/*": "/warp/$1",
		}))

		r.Use(middleware.MethodOverride())
		r.Use(middleware.Secure())
		r.Use(middleware.Recover())
		r.Use(middlewares.Gzip())

		r.Use(middlewares.CORS())
		r.Use(middlewares.Logger())

		if sentryDSN != "" {
			err := sentry.Init(sentry.ClientOptions{
				Dsn:     sentryDSN,
				Release: version,
			})
			if err != nil {
				log.Printf("Sentry initialization failed: %v\n", err)
			} else {
				r.Use(sentryecho.New(sentryecho.Options{}))
			}
		}

		if viper.GetBool("metrics.enabled") {
			if viper.GetBool("metrics.basicauth.enabled") {
				r.Any("/metrics", echo.WrapHandler(promhttp.Handler()), middleware.BasicAuth(
					func(username, password string, c echo.Context) (bool, error) {
						config_username := viper.GetString("metrics.basicauth.user")
						hash_password := viper.GetString("metrics.basicauth.password")
						if subtle.ConstantTimeCompare([]byte(username), []byte(config_username)) == 1 &&
							bcrypt.CompareHashAndPassword([]byte(hash_password), []byte(password)) == nil {
							return true, nil
						}
						return false, nil
					}))
			} else {
				r.Any("/metrics", echo.WrapHandler(promhttp.Handler()))
			}
		}
		r.Any("/", func(ctx echo.Context) error {
			return ctx.NoContent(http.StatusOK)
		})

		tokens := viper.GetStringSlice("deny.tokens")

		openTSDB := opentsdb.NewOpenTSDB()
		gOpenTSDB := r.Group("/opentsdb", middlewares.Protocol("opentsdb"), middlewares.Deny(tokens))
		gOpenTSDB.Any("/api/query*", middlewares.Native(openTSDB.HandleQuery))
		gOpenTSDB.Any("/api/query/last*", middlewares.Native(openTSDB.HandleQueryLast))
		gOpenTSDB.Any("/api/suggest*", middlewares.Native(openTSDB.HandleSuggest))
		gOpenTSDB.Any("/api/aggregators*", middlewares.Native(openTSDB.HandleAggregators))
		gOpenTSDB.Any("/api/search/lookup*", middlewares.Native(openTSDB.HandleLookup))
		gOpenTSDB.Any("/api/config/filters*", middlewares.Native(openTSDB.HandleConfigFilters))

		promQL := prom.NewPromQL()
		gPromQL := r.Group("/prometheus", middlewares.Protocol("prometheus"), middlewares.Deny(tokens))
		gPromQL.Any("/api/v1/query_range*", middlewares.Native(promQL.QueryRange))
		gPromQL.Any("/api/v1/query*", middlewares.Native(promQL.InstantQuery))
		gPromQL.Any("/api/v1/series*", middlewares.Native(promQL.FindAndDeleteSeries))
		gPromQL.Any("/api/v1/label/:label/values*", promQL.FindLabelsValues)
		gPromQL.Any("/remote_read*", remoteRead.HandlerBuilder())

		gGraphite := r.Group("/graphite", middlewares.Protocol("graphite"), middlewares.Deny(tokens))
		gGraphite.Any("/render*", middlewares.Native(graphite.Render))
		gGraphite.Any("/metrics*", middlewares.Native(graphite.Find))
		gGraphite.Any("/metrics/find*", middlewares.Native(graphite.Find))
		gGraphite.Any("/metrics/expand*", middlewares.Native(graphite.Expand))
		gGraphite.Any("/metrics/index.json*", middlewares.Native(graphite.Index))

		i := influxdb.NewInfluxDB()
		gInfluxDB := r.Group("/influxdb", middlewares.Protocol("influxdb"), middlewares.Deny(tokens))
		gInfluxDB.Any("/query*", middlewares.Native(i.Query))

		gWarp := r.Group("/warp", middlewares.Protocol("warp10"))
		gWarp.Any("/api/v0/exec", warp.Exec)
		gWarp.Any("/api/v0/*", middlewares.ReverseWithConfig(middlewares.ReverseConfig{
			URL: viper.GetString("warp_endpoint") + "/api/v0",
		}))

		for _, route := range r.Routes() {
			log.Debugf("%s %s ---> %s", pad.Right(route.Method, 7, " "), pad.Right(route.Path, 50, " "), route.Name)
		}

		server := &http.Server{
			Handler: r,
			Addr:    addr,
		}

		go func() {
			log.Infof("Start erlenmeyer on %s", server.Addr)
			if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
				log.Fatal(err)
			}
		}()

		quit := make(chan os.Signal, 1)

		signal.Notify(quit, os.Interrupt)

		<-quit

		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()

		if err := server.Shutdown(ctx); err != nil {
			log.WithFields(log.Fields{
				"error": err,
			}).Fatal("Cannot gracefully shutdown erlenmeyer")
		}
	},
}

RootCmd represents the base command when called without any subcommands

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL