-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rishabh Kumar
committed
Jan 9, 2025
1 parent
037b4ac
commit 129536f
Showing
6 changed files
with
164 additions
and
110 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
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 |
---|---|---|
@@ -1,87 +1,118 @@ | ||
package promclient | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/api" | ||
clientv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
"github.com/prometheus/common/route" | ||
"github.com/prometheus/prometheus/config" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/prometheus/prometheus/promql/promqltest" | ||
"github.com/prometheus/prometheus/storage" | ||
v1 "github.com/prometheus/prometheus/web/api/v1" | ||
) | ||
|
||
// CreateTestServer simply will create a test HTTP server on the path defined and return | ||
// an API client, a clode method, and any error when creating | ||
|
||
//TODO Fix it later | ||
//func CreateTestServer(t *testing.T, path string) (API, func(), error) { | ||
// var close func() | ||
// content, err := os.ReadFile(path) | ||
// if err != nil { | ||
// return nil, close, err | ||
// } | ||
// | ||
// test, err := promqltest.NewTestEngine(t, string(content)) | ||
// if err != nil { | ||
// return nil, nil, err | ||
// } | ||
// close = test.Close | ||
// | ||
// // Load the data | ||
// if err := test.Run(); err != nil { | ||
// return nil, close, err | ||
// } | ||
// | ||
// ln, err := net.Listen("tcp", "") | ||
// if err != nil { | ||
// return nil, close, err | ||
// } | ||
// | ||
// // Start up API server for engine | ||
// cfgFunc := func() config.Config { return config.DefaultConfig } | ||
// // Return 503 until ready (for us there isn't much startup, so this might not need to be implemented | ||
// readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f } | ||
// | ||
// apiRouter := route.New() | ||
// webv1.NewAPI( | ||
// test.QueryEngine(), // Query Engine | ||
// test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable | ||
// nil, //appendable | ||
// nil, // exemplarQueryable | ||
// nil, //factoryTr | ||
// nil, //factoryAr | ||
// nil, | ||
// cfgFunc, | ||
// nil, // flags | ||
// webv1.GlobalURLOptions{ | ||
// ListenAddress: ln.Addr().String(), | ||
// Host: "localhost", | ||
// Scheme: "http", | ||
// }, // global URL options | ||
// readyFunc, // ready | ||
// nil, // local storage | ||
// "", // tsdb dir | ||
// false, // enable admin API | ||
// nil, // logger | ||
// nil, // FactoryRr | ||
// 50000000, // RemoteReadSampleLimit | ||
// 1000, // RemoteReadConcurrencyLimit | ||
// 1048576, // RemoteReadBytesInFrame | ||
// false, // isAgent | ||
// nil, // CORSOrigin | ||
// nil, // runtimeInfo | ||
// nil, // buildInfo | ||
// nil, | ||
// nil, | ||
// nil, // gatherer | ||
// nil, // registerer | ||
// nil, // statsRenderer | ||
// false, | ||
// nil, | ||
// false, | ||
// ).Register(apiRouter.WithPrefix("/api/v1")) | ||
// | ||
// srv := &http.Server{Handler: apiRouter} | ||
// go srv.Serve(ln) // TODO: cancel/stop ability | ||
// close = func() { | ||
// //test.Close() | ||
// srv.Close() | ||
// } | ||
// | ||
// client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()}) | ||
// if err != nil { | ||
// return nil, close, err | ||
// } | ||
// | ||
// return &PromAPIV1{clientv1.NewAPI(client)}, close, nil | ||
//} | ||
func CreateTestServer(t *testing.T, path string) (API, func(), error) { | ||
var close func() | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, close, err | ||
} | ||
|
||
test, err := promqltest.NewTest(t, string(content)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
close = test.Close | ||
|
||
engineOpts := promql.EngineOpts{ | ||
Logger: nil, | ||
Reg: nil, | ||
MaxSamples: 50000000, | ||
Timeout: 10 * time.Minute, | ||
ActiveQueryTracker: nil, | ||
LookbackDelta: 0, | ||
NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() }, | ||
EnableAtModifier: true, | ||
EnableNegativeOffset: false, | ||
EnablePerStepStats: false, | ||
EnableDelayedNameRemoval: false, | ||
} | ||
|
||
queryEngine := promql.NewEngine(engineOpts) | ||
|
||
// Load the data | ||
if err := test.Run(queryEngine); err != nil { | ||
return nil, close, err | ||
} | ||
|
||
ln, err := net.Listen("tcp", "") | ||
if err != nil { | ||
return nil, close, err | ||
} | ||
|
||
// Start up API server for engine | ||
cfgFunc := func() config.Config { return config.DefaultConfig } | ||
// Return 503 until ready (for us there isn't much startup, so this might not need to be implemented | ||
readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f } | ||
apiRouter := route.New() | ||
v1.NewAPI( | ||
queryEngine, // Query Engine | ||
test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable | ||
nil, //appendable | ||
nil, // exemplarQueryable | ||
nil, //factoryTr | ||
nil, //factoryAr | ||
nil, | ||
cfgFunc, | ||
nil, // flags | ||
v1.GlobalURLOptions{ | ||
ListenAddress: ln.Addr().String(), | ||
Host: "localhost", | ||
Scheme: "http", | ||
}, // global URL options | ||
readyFunc, // ready | ||
nil, // local storage | ||
"", // tsdb dir | ||
false, // enable admin API | ||
nil, // logger | ||
nil, // FactoryRr | ||
50000000, // RemoteReadSampleLimit | ||
1000, // RemoteReadConcurrencyLimit | ||
1048576, // RemoteReadBytesInFrame | ||
false, // isAgent | ||
nil, // CORSOrigin | ||
nil, // runtimeInfo | ||
nil, // buildInfo | ||
nil, | ||
nil, | ||
nil, // gatherer | ||
nil, // registerer | ||
nil, // statsRenderer | ||
false, | ||
nil, | ||
false, | ||
).Register(apiRouter.WithPrefix("/api/v1")) | ||
|
||
srv := &http.Server{Handler: apiRouter} | ||
go srv.Serve(ln) // TODO: cancel/stop ability | ||
close = func() { | ||
//test.Close() | ||
srv.Close() | ||
} | ||
|
||
client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()}) | ||
if err != nil { | ||
return nil, close, err | ||
} | ||
|
||
return &PromAPIV1{clientv1.NewAPI(client)}, close, nil | ||
} |
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
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