@@ -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 used to configure query execution for Prometheus data sources.
2527func NewPrometheusConfig (nameRegexPatterns ... string ) * PrometheusConfig {
2628 return & PrometheusConfig {
2729 Url : os .Getenv (PrometheusUrlEnvVar ),
@@ -56,6 +58,9 @@ func NewPrometheusQueryExecutor(queries map[string]string, config *PrometheusCon
5658 }, nil
5759}
5860
61+ // NewStandardPrometheusQueryExecutor creates a PrometheusQueryExecutor with standard queries
62+ // based on the provided time range and configuration. It simplifies the process of generating
63+ // queries for Prometheus, making it easier to integrate Prometheus data into reports.
5964func NewStandardPrometheusQueryExecutor (startTime , endTime time.Time , config * PrometheusConfig ) (* PrometheusQueryExecutor , error ) {
6065 p := & PrometheusQueryExecutor {}
6166
@@ -74,6 +79,8 @@ func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config *Pr
7479 return NewPrometheusQueryExecutor (standardQueries , config )
7580}
7681
82+ // Execute runs the defined Prometheus queries concurrently, collecting results and warnings.
83+ // It returns an error if any query fails, allowing for efficient data retrieval in reporting tasks.
7784func (r * PrometheusQueryExecutor ) Execute (ctx context.Context ) error {
7885 for name , query := range r .Queries {
7986 result , warnings , queryErr := r .client .Query (ctx , query , r .EndTime )
@@ -91,14 +98,20 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
9198 return nil
9299}
93100
101+ // Results returns the query results as a map of string to interface{}.
102+ // It allows users to access the results of executed queries, facilitating data retrieval and manipulation.
94103func (r * PrometheusQueryExecutor ) Results () map [string ]interface {} {
95104 return r .QueryResults
96105}
97106
107+ // Kind returns the type of the query executor as a string.
108+ // It is used to identify the specific kind of executor in a collection of query executors.
98109func (l * PrometheusQueryExecutor ) Kind () string {
99110 return l .KindName
100111}
101112
113+ // Validate checks the PrometheusQueryExecutor for a valid client and ensures that at least one query is provided.
114+ // It returns an error if the client is nil or no queries are specified, helping to ensure proper configuration before execution.
102115func (r * PrometheusQueryExecutor ) Validate () error {
103116 if r .client == nil {
104117 return errors .New ("prometheus client is nil" )
@@ -111,6 +124,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
111124 return nil
112125}
113126
127+ // IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
128+ // It returns an error if the types do not match, ensuring type safety for query comparisons.
114129func (r * PrometheusQueryExecutor ) IsComparable (other QueryExecutor ) error {
115130 otherType := reflect .TypeOf (other )
116131 if otherType != reflect .TypeOf (r ) {
@@ -141,10 +156,15 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
141156 return nil
142157}
143158
159+ // Warnings returns a map of warnings encountered during query execution.
160+ // This function is useful for retrieving any issues that may have arisen,
161+ // allowing users to handle or log them appropriately.
144162func (r * PrometheusQueryExecutor ) Warnings () map [string ]v1.Warnings {
145163 return r .warnings
146164}
147165
166+ // MustResultsAsValue retrieves the query results as a map of metric names to their corresponding values.
167+ // It ensures that the results are in a consistent format, making it easier to work with metrics in subsequent operations.
148168func (r * PrometheusQueryExecutor ) MustResultsAsValue () map [string ]model.Value {
149169 results := make (map [string ]model.Value )
150170 for name , result := range r .QueryResults {
@@ -180,6 +200,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
180200 return results
181201}
182202
203+ // TimeRange sets the start and end time for the Prometheus query execution.
204+ // This function is essential for defining the time window for data retrieval, ensuring accurate and relevant results.
183205func (r * PrometheusQueryExecutor ) TimeRange (startTime , endTime time.Time ) {
184206 r .StartTime = startTime
185207 r .EndTime = endTime
@@ -222,6 +244,9 @@ type TypedMetric struct {
222244 MetricType string `json:"metric_type"`
223245}
224246
247+ // MarshalJSON customizes the JSON representation of PrometheusQueryExecutor.
248+ // It includes only essential fields: Kind, Queries, and simplified QueryResults.
249+ // This function is useful for serializing the executor's state in a concise format.
225250func (g * PrometheusQueryExecutor ) MarshalJSON () ([]byte , error ) {
226251 // we need custom marshalling to only include some parts of the metrics
227252 type QueryExecutor struct {
@@ -248,6 +273,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
248273 return json .Marshal (q )
249274}
250275
276+ // UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
277+ // It populates the QueryResults field with appropriately typed metrics,
278+ // enabling easy access to the results of Prometheus queries.
251279func (r * PrometheusQueryExecutor ) UnmarshalJSON (data []byte ) error {
252280 // helper struct with QueryResults map[string]interface{}
253281 type Alias PrometheusQueryExecutor
0 commit comments