forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ansible add metrics (operator-framework#5438)
* Users can now create metrics through Ansible Metrics are created with the osdk_metrics module in operator-sdk-ansible-util which communicates with an ansible API server. Signed-off-by: austin <austin@redhat.com> * bump collection Signed-off-by: austin <austin@redhat.com> * cleanup Signed-off-by: austin <austin@redhat.com> * Add copyright info Signed-off-by: austin <austin@redhat.com> * clean up log message levels Signed-off-by: austin <austin@redhat.com> * add metrics address option and fix errors Signed-off-by: austin <austin@redhat.com> * log cleanup Signed-off-by: austin <austin@redhat.com> * sanity checks Signed-off-by: austin <austin@redhat.com> * comments Signed-off-by: austin <austin@redhat.com> * bump retries to avoid flake Signed-off-by: austin <austin@redhat.com> * feedback from ryan Signed-off-by: austin <austin@redhat.com> * helper functions Signed-off-by: austin <austin@redhat.com> * handle error Signed-off-by: austin <austin@redhat.com> * refactor metric creation into helper Signed-off-by: austin <austin@redhat.com> * revert Run changes for now Signed-off-by: austin <austin@redhat.com> * put Run in goroutine, not goroutine in Run Signed-off-by: austin <austin@redhat.com> * s/server/err since it is error type Signed-off-by: austin <austin@redhat.com> * read and close request body, catch error on negative counter.Add value Signed-off-by: austin <austin@redhat.com> * explicitly ignore errors returned by copy Signed-off-by: austin <austin@redhat.com>
- Loading branch information
Showing
6 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2022 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apiserver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
||
"github.com/operator-framework/operator-sdk/internal/ansible/metrics" | ||
) | ||
|
||
var log = logf.Log.WithName("apiserver") | ||
|
||
type Options struct { | ||
Address string | ||
Port int | ||
} | ||
|
||
func Run(options Options) error { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/metrics", metricsHandler) | ||
|
||
server := http.Server{ | ||
Addr: fmt.Sprintf("%s:%d", options.Address, options.Port), | ||
Handler: mux, | ||
} | ||
log.Info("Starting to serve metrics listener", "Address", server.Addr) | ||
return server.ListenAndServe() | ||
} | ||
|
||
func metricsHandler(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
_, _ = io.Copy(io.Discard, r.Body) | ||
r.Body.Close() | ||
}() | ||
log.V(3).Info(fmt.Sprintf("%s %s", r.Method, r.URL)) | ||
|
||
var userMetric metrics.UserMetric | ||
|
||
switch r.Method { | ||
case http.MethodPost: | ||
log.V(3).Info("The apiserver has received a POST") | ||
err := json.NewDecoder(r.Body).Decode(&userMetric) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
err = metrics.HandleUserMetric(crmetrics.Registry, userMetric) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
default: | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters