-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/{sdk,generator} Expose Prometheus metrics port #323
Merged
hasbro17
merged 1 commit into
operator-framework:master
from
etiennecoutaud:expose-metrics
Jul 6, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,28 @@ | ||
package sdk | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
) | ||
|
||
// ExposeMetricsPort generate a Kubernetes Service to expose metrics port | ||
func ExposeMetricsPort() { | ||
http.Handle("/"+k8sutil.PrometheusMetricsPortName, promhttp.Handler()) | ||
go http.ListenAndServe(":"+strconv.Itoa(k8sutil.PrometheusMetricsPort), nil) | ||
|
||
service, err := k8sutil.InitOperatorService() | ||
if err != nil { | ||
logrus.Fatalf("Failed to init operator service: %v", err) | ||
} | ||
err = Create(service) | ||
if err != nil && !errors.IsAlreadyExists(err) { | ||
logrus.Infof("Failed to create operator service: %v", err) | ||
return | ||
} | ||
logrus.Infof("Metrics service %s created", service.Name) | ||
} |
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,61 @@ | ||
package k8sutil | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetOperatorName(t *testing.T) { | ||
type Output struct { | ||
operatorName string | ||
err error | ||
} | ||
|
||
type Scenario struct { | ||
name string | ||
envVarKey string | ||
envVarValue string | ||
expectedOutput Output | ||
} | ||
|
||
tests := []Scenario{ | ||
Scenario{ | ||
name: "Simple case", | ||
envVarKey: OperatorNameEnvVar, | ||
envVarValue: "myoperator", | ||
expectedOutput: Output{ | ||
operatorName: "myoperator", | ||
err: nil, | ||
}, | ||
}, | ||
Scenario{ | ||
name: "Unset env var", | ||
envVarKey: "", | ||
envVarValue: "", | ||
expectedOutput: Output{ | ||
operatorName: "", | ||
err: fmt.Errorf("%s must be set", OperatorNameEnvVar), | ||
}, | ||
}, | ||
Scenario{ | ||
name: "Empty env var", | ||
envVarKey: OperatorNameEnvVar, | ||
envVarValue: "", | ||
expectedOutput: Output{ | ||
operatorName: "", | ||
err: fmt.Errorf("%s must not be empty", OperatorNameEnvVar), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
_ = os.Setenv(test.envVarKey, test.envVarValue) | ||
operatorName, err := GetOperatorName() | ||
if !(operatorName == test.expectedOutput.operatorName && reflect.DeepEqual(err, test.expectedOutput.err)) { | ||
t.Errorf("test %s failed, expected ouput: %s,%v; got: %s,%v", test.name, test.expectedOutput.operatorName, test.expectedOutput.err, operatorName, err) | ||
} | ||
_ = os.Unsetenv(test.envVarKey) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a suggestion, the prometheus OpenCensus exporter library could be used.
Here is a code snippet to illustrate the usage:
IMHO, this project should integrate and use the OpenCensus library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@secat I'm not too familiar with the OpenCensus library so I can't comment on the need for it yet. However this PR is only meant to be the starting point for exposing the Prometheus metrics so I would prefer to keep it simple right now and iterate on more changes after this.
It'll be helpful if you can create an issue so we can discuss the use cases for the OpenCensus library in the SDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hasbro17 I have created the issue #343