Skip to content

Commit

Permalink
Merge pull request #59 from sustainable-computing-io/issue30-sampling…
Browse files Browse the repository at this point in the history
…rate

enable deployment customization of sampling rate
  • Loading branch information
trent-s authored Mar 15, 2024
2 parents a7216c6 + 1dd4b50 commit a006409
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"flag"
"os"
"time"
"strconv"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -55,13 +57,15 @@ func main() {
var keplerPrometheusUrl string
var susqlPrometheusMetricsUrl string
var susqlPrometheusDatabaseUrl string
var samplingRate string

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&keplerPrometheusUrl, "kepler-prometheus-url", "", "The URL for the Prometheus server where Kepler stores the energy data")
flag.StringVar(&susqlPrometheusDatabaseUrl, "susql-prometheus-database-url", "", "The URL for the Prometheus database where SusQL stores the energy data")
flag.StringVar(&susqlPrometheusMetricsUrl, "susql-prometheus-metrics-url", "", "The URL for the Prometheus metrics where SusQL exposes the energy data")
flag.StringVar(&samplingRate, "sampling-rate", "", "Sampling rate in seconds")

opts := zap.Options{
Development: true,
Expand Down Expand Up @@ -95,12 +99,18 @@ func main() {
os.Exit(1)
}

samplingRateInteger, err := strconv.Atoi(samplingRate)
if err != nil {
samplingRateInteger = 2
}

if err = (&controller.LabelGroupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
KeplerPrometheusUrl: keplerPrometheusUrl,
SusQLPrometheusDatabaseUrl: susqlPrometheusDatabaseUrl,
SusQLPrometheusMetricsUrl: susqlPrometheusMetricsUrl,
SamplingRate: time.Duration(samplingRateInteger) * time.Second,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "LabelGroup")
os.Exit(1)
Expand Down
8 changes: 8 additions & 0 deletions deployment/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ if [[ -z ${SUSQL_PROMETHEUS_URL} ]]; then
fi
fi

if [[ -z ${SUSQL_SAMPLING_RATE} ]]; then
SUSQL_SAMPLING_RATE="2"
fi


# Check if namespace exists
if [[ -z $(kubectl get namespaces --no-headers -o custom-columns=':{.metadata.name}' | grep ${SUSQL_NAMESPACE}) ]]; then
echo "Namespace '${SUSQL_NAMESPACE}' doesn't exist. Creating it."
Expand Down Expand Up @@ -98,6 +103,7 @@ echo "PROMETHEUS_DOMAIN - '${PROMETHEUS_DOMAIN}'"
echo "PROMETHEUS_PORT - '${PROMETHEUS_PORT}'"
echo "KEPLER_PROMETHEUS_URL - '${KEPLER_PROMETHEUS_URL}'"
echo "SUSQL_PROMETHEUS_URL - '${SUSQL_PROMETHEUS_URL}'"
echo "SUSQL_SAMPLING_RATE - '${SUSQL_SAMPLING_RATE}'"
echo "SUSQL_ENHANCED - '${SUSQL_ENHANCED}'"
echo "SUSQL_REGISTRY - '${SUSQL_REGISTRY}'"
echo "SUSQL_IMAGE_NAME - '${SUSQL_IMAGE_NAME}'"
Expand Down Expand Up @@ -125,6 +131,7 @@ echo "export PROMETHEUS_DOMAIN=${PROMETHEUS_DOMAIN}" >> ${LOGFILE}
echo "export PROMETHEUS_PORT=${PROMETHEUS_PORT}" >> ${LOGFILE}
echo "export KEPLER_PROMETHEUS_URL=${KEPLER_PROMETHEUS_URL}" >> ${LOGFILE}
echo "export SUSQL_PROMETHEUS_URL=${SUSQL_PROMETHEUS_URL}" >> ${LOGFILE}
echo "export SUSQL_SAMPLING_RATE=${SUSQL_SAMPLING_RATE}" >> ${LOGFILE}
echo "export SUSQL_ENHANCED=${SUSQL_ENHANCED}" >> ${LOGFILE}
echo "export SUSQL_REGISTRY=${SUSQL_REGISTRY}" >> ${LOGFILE}
echo "export SUSQL_IMAGE_NAME=${SUSQL_IMAGE_NAME}" >> ${LOGFILE}
Expand Down Expand Up @@ -214,6 +221,7 @@ do
--set keplerPrometheusUrl="${KEPLER_PROMETHEUS_URL}" \
--set susqlPrometheusDatabaseUrl="${SUSQL_PROMETHEUS_URL}" \
--set susqlPrometheusMetricsUrl="http://0.0.0.0:8082" \
--set samplingRate="${SUSQL_SAMPLING_RATE}" \
--set imagePullPolicy="Always" \
--set containerImage="${SUSQL_REGISTRY}/${SUSQL_IMAGE_NAME}:${SUSQL_IMAGE_TAG}"
if [[ ! -z ${SUSQL_ENHANCED} ]]; then
Expand Down
1 change: 1 addition & 0 deletions deployment/susql-controller/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
- "--kepler-prometheus-url={{ .Values.keplerPrometheusUrl }}"
- "--susql-prometheus-database-url={{ .Values.susqlPrometheusDatabaseUrl }}"
- "--susql-prometheus-metrics-url={{ .Values.susqlPrometheusMetricsUrl }}"
- "--sampling-rate={{ .Values.samplingRate }}"
ports:
- name: metrics
containerPort: 8082
Expand Down
1 change: 1 addition & 0 deletions deployment/susql-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ requests:
keplerPrometheusUrl: "http://prometheus-k8s.monitoring.svc.cluster.local:9090"
susqlPrometheusDatabaseUrl: "http://prometheus-susql.openshift-kepler-operator.svc.cluster.local:9090"
susqlPrometheusMetricsUrl: "http://0.0.0.0:8082"
samplingRate: "2"
4 changes: 2 additions & 2 deletions internal/controller/labelgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ type LabelGroupReconciler struct {
KeplerPrometheusUrl string
SusQLPrometheusDatabaseUrl string
SusQLPrometheusMetricsUrl string
SamplingRate time.Duration // Sampling rate for all the label groups
}

const (
keplerMetricName = "kepler_container_joules_total" // Kepler metric to query
susqlMetricName = "susql_total_energy_joules" // SusQL metric to query
samplingRate = 2 * time.Second // Sampling rate for all the label groups
fixingDelay = 15 * time.Second // Time to wait in the even the label group was badly constructed
errorDelay = 1 * time.Second // Time to wait when an error happens due to network connectivity issues
)
Expand Down Expand Up @@ -227,7 +227,7 @@ func (r *LabelGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request)
r.SetAggregatedEnergyForLabels(totalEnergy, labelGroup.Status.PrometheusLabels)

// Requeue
return ctrl.Result{RequeueAfter: samplingRate}, nil
return ctrl.Result{RequeueAfter: r.SamplingRate}, nil

default:
// First time seeing this object
Expand Down

0 comments on commit a006409

Please sign in to comment.