Exemple #1
0
func TestConstEngine(t *testing.T) {
	engine := suggest.NewSuggester(
		"log", "logger", "loggest", "a", "an", "and", "aback")
	assertValueDeepEquals(
		t, []string{"log", "logger", "loggest"}, engine.Suggest(0, "log"))
	assertValueDeepEquals(
		t, []string{"log", "logger", "loggest"}, engine.Suggest(3, "log"))
	assertValueDeepEquals(
		t, []string{"log", "logger"}, engine.Suggest(2, "log"))
	assertValueDeepEquals(
		t, []string{"log"}, engine.Suggest(1, "log"))
	assertValueDeepEquals(
		t, []string{"logger", "loggest"}, engine.Suggest(2, "logg"))
	assertValueDeepEquals(
		t, []string{"a", "an", "and", "aback"}, engine.Suggest(0, "a"))
	assertValueDeepEquals(
		t, []string{"a", "an", "and", "aback"}, engine.Suggest(0, "a"))
	assertValueDeepEquals(
		t, []string{"aback"}, engine.Suggest(0, "ab"))
	assertValueDeepEquals(
		t, []string{"log", "logger"}, engine.Suggest(2, ""))
	assertValueDeepEquals(
		t, []string{"log", "logger", "loggest", "a", "an", "and", "aback"},
		engine.Suggest(0, ""))
	assertValueEquals(t, 0, len(engine.Suggest(0, "abc")))
	assertValueEquals(t, 0, len(engine.Suggest(0, "m")))
	assertValueEquals(t, 0, len(engine.Suggest(5, "m")))

}
Exemple #2
0
func main() {
	tricorder.RegisterFlags()
	flag.Parse()
	circularBuffer := logbuf.New()
	logger := log.New(circularBuffer, "", log.LstdFlags)
	handleSignals(logger)
	// Read configs early so that we will fail fast.
	maybeNilMemoryManager := maybeCreateMemoryManager(logger)
	consumerBuilders, err := newPStoreConsumers(maybeNilMemoryManager)
	if err != nil {
		log.Println("Pstore config file error:", err)
		logger.Println("Pstore config file error:", err)
	}
	metricNameEngine := suggest.NewEngine()
	metricNameAdder := newTsdbAdder(metricNameEngine)
	tagkEngine := suggest.NewSuggester("appname", "HostName")
	tagvEngine := suggest.NewEngine()
	tagvAdder := newTsdbAdder(tagvEngine)

	appList := createApplicationList()
	for _, app := range appList.All() {
		tagvAdder.Add(app.Name())
	}
	applicationStats := createApplicationStats(
		appList, logger, tagvAdder, maybeNilMemoryManager)
	rpc.RegisterName(
		"Scotty",
		&rpcType{AS: applicationStats},
	)
	rpc.HandleHTTP()
	connectionErrors := newConnectionErrorsType()
	if consumerBuilders == nil {
		startCollector(
			applicationStats,
			connectionErrors,
			totalCountCollectionType(nil),
			metricNameAdder,
			&maybeNilMemoryManagerWrapperType{maybeNilMemoryManager})
	} else {
		var coord coordinatorBuilderType
		if *fCoord != "" {
			var err error
			coord, err = consul.NewCoordinator(logger)
			if err != nil {
				logger.Println(err)
				coord = &blockingCoordinatorType{}
			}
		}
		totalCounts := startPStoreLoops(
			applicationStats,
			consumerBuilders,
			logger,
			coord)
		startCollector(
			applicationStats,
			connectionErrors,
			totalCountCollectionType(totalCounts),
			metricNameAdder,
			&maybeNilMemoryManagerWrapperType{maybeNilMemoryManager})
	}

	http.Handle(
		"/",
		gzipHandler{&splash.Handler{
			AS:  applicationStats,
			Log: circularBuffer,
		}})
	http.Handle(
		"/showAllApps",
		gzipHandler{&showallapps.Handler{
			AS:             applicationStats,
			CollectionFreq: *fCollectionFrequency,
		}})
	http.Handle(
		"/api/hosts/",
		http.StripPrefix(
			"/api/hosts/",
			gzipHandler{&byEndpointHandler{
				AS: applicationStats,
			}}))
	http.Handle(
		"/api/latest/",
		http.StripPrefix(
			"/api/latest/",
			gzipHandler{&latestHandler{
				AS: applicationStats,
			}}))

	http.Handle(
		"/api/errors/",
		gzipHandler{&errorHandler{
			ConnectionErrors: connectionErrors,
		}},
	)

	tsdbServeMux := http.NewServeMux()

	tsdbServeMux.Handle(
		"/api/query",
		tsdbexec.NewHandler(
			func(r *tsdbjson.QueryRequest) ([]tsdbjson.TimeSeries, error) {
				return tsdbexec.Query(
					r, applicationStats, *fCollectionFrequency)
			}))
	tsdbServeMux.Handle(
		"/api/suggest",
		tsdbexec.NewHandler(
			func(req url.Values) ([]string, error) {
				return tsdbexec.Suggest(
					req,
					map[string]suggest.Suggester{
						"metrics": metricNameEngine,
						"tagk":    tagkEngine,
						"tagv":    tagvEngine,
					})
			},
		))
	tsdbServeMux.Handle(
		"/api/aggregators",
		tsdbexec.NewHandler(
			func(req url.Values) ([]string, error) {
				return aggregators.Names(), nil
			},
		))
	tsdbServeMux.Handle(
		"/api/version",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"version": "1.0",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api/config",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"tsd.ore.auto_create_metrics": "true",
					"tsd.ore.auto_create_tagks":   "true",
					"tsd.ore.auto_create_tagvs":   "true",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api/config/filters",
		tsdbexec.NewHandler(
			func(req url.Values) (interface{}, error) {
				return tsdbjson.AllFilterDescriptions(), nil
			},
		))
	tsdbServeMux.Handle(
		"/api/dropcaches",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"message": "Caches dropped",
					"status":  "200",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api",
		tsdbexec.NotFoundHandler,
	)

	go func() {
		if err := http.ListenAndServe(fmt.Sprintf(":%d", *fTsdbPort), tsdbServeMux); err != nil {
			log.Fatal(err)
		}
	}()

	if err := http.ListenAndServe(fmt.Sprintf(":%d", *fPort), nil); err != nil {
		log.Fatal(err)
	}
}
Exemple #3
0
func TestNilEngine(t *testing.T) {
	engine := suggest.NewSuggester()
	assertValueEquals(t, 0, len(engine.Suggest(0, "")))
	assertValueEquals(t, 0, len(engine.Suggest(0, "a")))
	assertValueEquals(t, 0, len(engine.Suggest(2, "a")))
}