-
Notifications
You must be signed in to change notification settings - Fork 1.8k
doc/user: Add metrics and service-monitor docs #719
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ba806ef
doc/user/metrics: Add metrics docs
lilic 84b3313
Update doc/user/metrics/README.md
joelanford 6266047
Update doc/user/metrics/README.md
joelanford c9331a6
Add links to SM docs
lilic a41d456
doc/../service-monitor.md: Rename file and fix
lilic 15c324c
doc/../metrics/*: Wrap main func in docs
lilic 9ee94be
doc/user-guide.md: Link metrics
lilic ff00784
Update doc/user/metrics/service-monitor.md
AlexNPavel be804d1
Update doc/user/metrics/service-monitor.md
AlexNPavel f4546a5
Update doc/user/metrics/service-monitor.md
AlexNPavel cc206ec
Update doc/user-guide.md
hasbro17 f0c3529
Change link name
lilic 4e24d78
Update doc/user/metrics/service-monitor.md
hasbro17 46bfff2
Update doc/user/metrics/README.md
hasbro17 280a2e1
Update doc/user/metrics/service-monitor.md
hasbro17 be7d2c3
Update doc/user/metrics/service-monitor.md
hasbro17 6f2a9fd
Update doc/user/metrics/README.md
hasbro17 5abe606
Update doc/user/metrics/service-monitor.md
hasbro17 20d07b7
Address comments
lilic 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
This file contains hidden or 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 hidden or 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,51 @@ | ||
# Monitoring with Prometheus | ||
|
||
[Prometheus][prometheus] is an open-source systems monitoring and alerting toolkit. Below is the overview of the different helpers that exist in operator-sdk to help setup metrics in the generated operator. | ||
|
||
## Metrics in operator-sdk | ||
|
||
The `func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)` function exposes general metrics about the running program. These metrics are inherited from controller-runtime. This helper function creates a [Service][service] object with the metrics port exposed, which can then be accessed by Prometheus. The Service object is [garbage collected][gc] when the leader pod's root owner is deleted. | ||
|
||
By default, the metrics are served on `0.0.0.0:8383/metrics`. To modify the port the metrics are exposed on, change the `var metricsPort int32 = 8383` variable in the `cmd/manager/main.go` file of the generated operator. | ||
|
||
### Usage: | ||
|
||
```go | ||
import( | ||
"github.com/operator-framework/operator-sdk/pkg/metrics" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
func main() { | ||
|
||
... | ||
|
||
// Change the below variables to serve metrics on different host or port. | ||
var metricsHost = "0.0.0.0" | ||
var metricsPort int32 = 8383 | ||
|
||
// Pass metrics address to controller-runtime manager | ||
mgr, err := manager.New(cfg, manager.Options{ | ||
Namespace: namespace, | ||
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), | ||
}) | ||
|
||
... | ||
|
||
// Create Service object to expose the metrics port. | ||
_, err = metrics.ExposeMetricsPort(ctx, metricsPort) | ||
if err != nil { | ||
// handle error | ||
log.Info(err.Error()) | ||
} | ||
|
||
... | ||
} | ||
``` | ||
|
||
*Note:* The above example is already present in `cmd/manager/main.go` in all the operators generated with operator-sdk. | ||
|
||
[prometheus]: https://prometheus.io/ | ||
[service]: https://kubernetes.io/docs/concepts/services-networking/service/ | ||
[gc]: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents | ||
|
This file contains hidden or 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,44 @@ | ||
## Using the ServiceMonitor prometheus-operator CRD | ||
|
||
[prometheus-operator][prom-operator] is an operator that creates, configures, and manages Prometheus clusters atop Kubernetes. | ||
|
||
`ServiceMonitor` is a CustomResource of the prometheus-operator, which discovers the `Endpoints` in `Service` objects and configures Prometheus to monitor those pods. See the prometheus-operator [documentation][service-monitor] to learn more about `ServiceMonitor`. | ||
|
||
The `GenerateServiceMonitor` function takes a `Service` object and generates a `ServiceMonitor` resource based on it. To add `Service` target discovery of your created monitoring `Service` you can use the `metrics.CreateServiceMonitor()` helper function, which accepts the newly created `Service`. | ||
|
||
### Prerequisites: | ||
|
||
- [prometheus-operator][prom-quickstart] needs to be deployed in the cluster. | ||
|
||
### Usage example: | ||
|
||
```go | ||
import( | ||
"k8s.io/api/core/v1" | ||
"github.com/operator-framework/operator-sdk/pkg/metrics" | ||
) | ||
|
||
func main() { | ||
|
||
... | ||
|
||
// Populate below with the Service(s) for which you want to create ServiceMonitors. | ||
services := []*v1.Service{} | ||
|
||
// Create one `ServiceMonitor` per application per namespace. | ||
// Change below value to name of the Namespace you want the `ServiceMonitor` to be created in. | ||
ns := "default" | ||
|
||
// Pass the Service(s) to the helper function, which in turn returns the array of `ServiceMonitor` objects. | ||
serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services) | ||
if err != nil { | ||
// handle error here | ||
} | ||
|
||
... | ||
} | ||
lilic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
[prom-operator]: https://github.com/coreos/prometheus-operator | ||
[service-monitor]: https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor | ||
[prom-quickstart]: https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#quickstart |
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.
Uh oh!
There was an error while loading. Please reload this page.