@@ -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 execution.
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 generates queries for each name regex pattern and returns the executor or an error if the generation fails.
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 a series of Prometheus queries concurrently, collecting results and warnings.
82+ // It returns an error if any query fails, allowing for efficient data retrieval in reporting tasks.
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,22 @@ 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 results of executed queries, facilitating data retrieval and manipulation.
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 implementation of a query executor,
108+ // enabling filtering and processing of results based on their executor type.
98109func (l * PrometheusQueryExecutor ) Kind () string {
99110 return l .KindName
100111}
101112
113+ // Validate checks the Prometheus query executor's configuration for correctness.
114+ // It ensures that the Prometheus client is initialized and that at least one query is provided.
115+ // It returns an error if any validation checks fail, helping to prevent execution issues.
102116func (r * PrometheusQueryExecutor ) Validate () error {
103117 if r .client == nil {
104118 return errors .New ("prometheus client is nil" )
@@ -111,6 +125,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
111125 return nil
112126}
113127
128+ // IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
129+ // It returns an error if the types do not match, ensuring type safety for query comparisons.
114130func (r * PrometheusQueryExecutor ) IsComparable (other QueryExecutor ) error {
115131 otherType := reflect .TypeOf (other )
116132 if otherType != reflect .TypeOf (r ) {
@@ -141,10 +157,15 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
141157 return nil
142158}
143159
160+ // Warnings returns a map of warnings encountered during query execution.
161+ // This function is useful for retrieving any issues that may have arisen,
162+ // allowing users to handle or log them appropriately.
144163func (r * PrometheusQueryExecutor ) Warnings () map [string ]v1.Warnings {
145164 return r .warnings
146165}
147166
167+ // MustResultsAsValue retrieves the query results as a map of metric names to their corresponding values.
168+ // It ensures that the results are in a format compatible with further processing or serialization.
148169func (r * PrometheusQueryExecutor ) MustResultsAsValue () map [string ]model.Value {
149170 results := make (map [string ]model.Value )
150171 for name , result := range r .QueryResults {
@@ -180,6 +201,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
180201 return results
181202}
182203
204+ // TimeRange sets the start and end time for the Prometheus query execution.
205+ // This function is essential for defining the time window for data retrieval in monitoring and analytics tasks.
183206func (r * PrometheusQueryExecutor ) TimeRange (startTime , endTime time.Time ) {
184207 r .StartTime = startTime
185208 r .EndTime = endTime
@@ -222,6 +245,9 @@ type TypedMetric struct {
222245 MetricType string `json:"metric_type"`
223246}
224247
248+ // MarshalJSON customizes the JSON representation of the PrometheusQueryExecutor.
249+ // It includes only essential fields: Kind, Queries, and simplified QueryResults,
250+ // making it useful for efficient data transfer and storage in JSON format.
225251func (g * PrometheusQueryExecutor ) MarshalJSON () ([]byte , error ) {
226252 // we need custom marshalling to only include some parts of the metrics
227253 type QueryExecutor struct {
@@ -248,6 +274,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
248274 return json .Marshal (q )
249275}
250276
277+ // UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
278+ // It populates the QueryResults field with the appropriate metric types,
279+ // enabling structured access to the results of Prometheus queries.
251280func (r * PrometheusQueryExecutor ) UnmarshalJSON (data []byte ) error {
252281 // helper struct with QueryResults map[string]interface{}
253282 type Alias PrometheusQueryExecutor
0 commit comments