@@ -22,6 +22,8 @@ type PrometheusConfig struct {
2222
2323const PrometheusUrlEnvVar = "PROMETHEUS_URL"
2424
25+ // NewPrometheusConfig creates a new PrometheusConfig instance with the specified name regex patterns.
26+ // It retrieves the Prometheus URL from the environment and is useful for configuring Prometheus query executors.
2527func NewPrometheusConfig (nameRegexPatterns ... string ) * PrometheusConfig {
2628 return & PrometheusConfig {
2729 Url : os .Getenv (PrometheusUrlEnvVar ),
@@ -56,6 +58,8 @@ func NewPrometheusQueryExecutor(queries map[string]string, config PrometheusConf
5658 }, nil
5759}
5860
61+ // NewStandardPrometheusQueryExecutor creates a PrometheusQueryExecutor with standard queries based on the provided time range and configuration.
62+ // It simplifies the setup of query execution for Prometheus by generating queries that match specified name patterns.
5963func NewStandardPrometheusQueryExecutor (startTime , endTime time.Time , config PrometheusConfig ) (* PrometheusQueryExecutor , error ) {
6064 p := & PrometheusQueryExecutor {}
6165
@@ -74,6 +78,8 @@ func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config Pro
7478 return NewPrometheusQueryExecutor (standardQueries , config )
7579}
7680
81+ // Execute runs the defined Prometheus queries concurrently, collecting results and warnings.
82+ // It returns an error if any query fails, allowing for efficient data retrieval and error handling.
7783func (r * PrometheusQueryExecutor ) Execute (ctx context.Context ) error {
7884 for name , query := range r .Queries {
7985 result , warnings , queryErr := r .client .Query (ctx , query , r .EndTime )
@@ -91,14 +97,20 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
9197 return nil
9298}
9399
100+ // Results returns the query results as a map of string to interface{}.
101+ // It allows users to access the outcomes of executed queries, facilitating further processing or analysis of the data.
94102func (r * PrometheusQueryExecutor ) Results () map [string ]interface {} {
95103 return r .QueryResults
96104}
97105
106+ // Kind returns the type of the query executor as a string.
107+ // It is used to identify the specific kind of query executor in a collection.
98108func (l * PrometheusQueryExecutor ) Kind () string {
99109 return l .KindName
100110}
101111
112+ // Validate checks the Prometheus client and the provided queries for correctness.
113+ // It returns an error if the client is nil or if no queries are specified, ensuring that the executor is properly configured before execution.
102114func (r * PrometheusQueryExecutor ) Validate () error {
103115 if r .client == nil {
104116 return errors .New ("prometheus client is nil" )
@@ -111,6 +123,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
111123 return nil
112124}
113125
126+ // IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
127+ // It returns an error if the types do not match, ensuring type safety for query comparisons.
114128func (r * PrometheusQueryExecutor ) IsComparable (other QueryExecutor ) error {
115129 otherType := reflect .TypeOf (other )
116130 if otherType != reflect .TypeOf (r ) {
@@ -141,10 +155,15 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
141155 return nil
142156}
143157
158+ // Warnings returns a map of warnings encountered during query execution.
159+ // This function is useful for retrieving any issues that may have arisen,
160+ // allowing users to address potential problems in their queries.
144161func (r * PrometheusQueryExecutor ) Warnings () map [string ]v1.Warnings {
145162 return r .warnings
146163}
147164
165+ // MustResultsAsValue retrieves the query results as a map of metric names to their corresponding values.
166+ // It ensures that all results are converted to a common value type, facilitating further processing or serialization.
148167func (r * PrometheusQueryExecutor ) MustResultsAsValue () map [string ]model.Value {
149168 results := make (map [string ]model.Value )
150169 for name , result := range r .QueryResults {
@@ -180,6 +199,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
180199 return results
181200}
182201
202+ // TimeRange sets the start and end time for the Prometheus query execution.
203+ // This function is essential for defining the time window for data retrieval, ensuring accurate and relevant results.
183204func (r * PrometheusQueryExecutor ) TimeRange (startTime , endTime time.Time ) {
184205 r .StartTime = startTime
185206 r .EndTime = endTime
@@ -222,6 +243,9 @@ type TypedMetric struct {
222243 MetricType string `json:"metric_type"`
223244}
224245
246+ // MarshalJSON customizes the JSON representation of the PrometheusQueryExecutor.
247+ // It includes only essential fields: Kind, Queries, and simplified QueryResults.
248+ // This function is useful for serializing the executor's state in a concise format.
225249func (g * PrometheusQueryExecutor ) MarshalJSON () ([]byte , error ) {
226250 // we need custom marshalling to only include some parts of the metrics
227251 type QueryExecutor struct {
@@ -248,6 +272,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
248272 return json .Marshal (q )
249273}
250274
275+ // UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
276+ // It populates the QueryResults field with the appropriate metric types,
277+ // enabling structured access to the query results for further processing.
251278func (r * PrometheusQueryExecutor ) UnmarshalJSON (data []byte ) error {
252279 // helper struct with QueryResults map[string]interface{}
253280 type Alias PrometheusQueryExecutor
0 commit comments