@@ -47,6 +47,8 @@ func NewPrometheusQueryExecutor(url string, startTime, endTime time.Time, querie
4747 }, nil
4848}
4949
50+ // NewStandardPrometheusQueryExecutor creates a PrometheusQueryExecutor with standard queries based on the provided time range and name regex pattern.
51+ // It simplifies the setup of query execution against a Prometheus instance for monitoring and reporting purposes.
5052func NewStandardPrometheusQueryExecutor (url string , startTime , endTime time.Time , nameRegexPattern string ) (* PrometheusQueryExecutor , error ) {
5153 p := & PrometheusQueryExecutor {}
5254
@@ -58,6 +60,8 @@ func NewStandardPrometheusQueryExecutor(url string, startTime, endTime time.Time
5860 return NewPrometheusQueryExecutor (url , startTime , endTime , standardQueries )
5961}
6062
63+ // Execute runs a series of Prometheus queries concurrently, collecting results and warnings for each query.
64+ // It returns an error if any query fails, allowing for efficient data retrieval in reporting scenarios.
6165func (r * PrometheusQueryExecutor ) Execute (ctx context.Context ) error {
6266 for name , query := range r .Queries {
6367 result , warnings , queryErr := r .client .Query (ctx , query , r .endTime )
@@ -75,14 +79,22 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
7579 return nil
7680}
7781
82+ // Results returns the query results as a map of string to interface{}.
83+ // It allows users to access the outcomes of executed queries, facilitating further processing or analysis of the data.
7884func (r * PrometheusQueryExecutor ) Results () map [string ]interface {} {
7985 return r .QueryResults
8086}
8187
88+ // Kind returns the type of the query executor as a string.
89+ // It is used to identify the specific kind of executor in a collection,
90+ // enabling filtering and type-specific operations.
8291func (l * PrometheusQueryExecutor ) Kind () string {
8392 return l .KindName
8493}
8594
95+ // Validate checks the PrometheusQueryExecutor's configuration for correctness.
96+ // It ensures that the client is set, queries are provided, and both start and end times are defined.
97+ // This function is essential for preventing execution errors due to misconfiguration.
8698func (r * PrometheusQueryExecutor ) Validate () error {
8799 if r .client == nil {
88100 return errors .New ("prometheus client is nil" )
@@ -103,6 +115,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
103115 return nil
104116}
105117
118+ // IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
119+ // It returns an error if the types do not match, ensuring type safety for query comparisons.
106120func (r * PrometheusQueryExecutor ) IsComparable (other QueryExecutor ) error {
107121 otherType := reflect .TypeOf (other )
108122 if otherType != reflect .TypeOf (r ) {
@@ -133,10 +147,16 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
133147 return nil
134148}
135149
150+ // Warnings returns a map of warnings encountered during Prometheus queries.
151+ // This function is useful for retrieving any issues that may have occurred,
152+ // allowing users to handle or log them appropriately.
136153func (r * PrometheusQueryExecutor ) Warnings () map [string ]v1.Warnings {
137154 return r .warnings
138155}
139156
157+ // MustResultsAsValue returns a map of query results as model.Values.
158+ // It is used to simplify the retrieval of metric values from the query results,
159+ // enabling easier processing and serialization of metrics in various formats.
140160func (r * PrometheusQueryExecutor ) MustResultsAsValue () map [string ]model.Value {
141161 results := make (map [string ]model.Value )
142162 for name , result := range r .QueryResults {
@@ -145,6 +165,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
145165 return results
146166}
147167
168+ // TimeRange sets the start and end time for the Prometheus query execution.
169+ // This function is essential for defining the time window in which data will be fetched and analyzed.
148170func (r * PrometheusQueryExecutor ) TimeRange (startTime , endTime time.Time ) {
149171 r .startTime = startTime
150172 r .endTime = endTime
@@ -188,6 +210,9 @@ type TypedMetric struct {
188210 MetricType string `json:"metric_type"`
189211}
190212
213+ // MarshalJSON customizes the JSON representation of the PrometheusQueryExecutor.
214+ // It includes only essential fields: Kind, Queries, and simplified QueryResults,
215+ // making it suitable for efficient serialization and data transfer.
191216func (g * PrometheusQueryExecutor ) MarshalJSON () ([]byte , error ) {
192217 // we need custom marshalling to only include some parts of the metrics
193218 type QueryExecutor struct {
@@ -214,6 +239,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
214239 return json .Marshal (q )
215240}
216241
242+ // UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
243+ // It populates the QueryResults field with appropriately typed metrics,
244+ // enabling easy access to the results of Prometheus queries.
217245func (r * PrometheusQueryExecutor ) UnmarshalJSON (data []byte ) error {
218246 // helper struct with QueryResults map[string]interface{}
219247 type Alias PrometheusQueryExecutor
0 commit comments