-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example how to use plugin API for telemetry. (#88)
- Loading branch information
1 parent
e6d3dc9
commit 291bb9c
Showing
8 changed files
with
289 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
telemetry |
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,12 @@ | ||
# Telemetry | ||
|
||
This directory contains an example of how to use the `weaver kube` plugin API to | ||
configure how metrics and traces are exported. In `main.go`, we register a | ||
plugin to export traces to Jaeger and a plugin to export metrics to Prometheus. | ||
Compile the `telemetry` binary and use it as you would `weaver kube`. Use | ||
`prometheus.yaml` and `jaeger.yaml` to deploy Prometheus and Jaeger to a | ||
Kubernetes cluster. | ||
|
||
```console | ||
$ kubectl apply -f jaeger.yaml -f prometheus.yaml -f $(telemetry deploy kube_deploy.yaml) | ||
``` |
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,51 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# 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. | ||
|
||
# Jaegar Deployment | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: jaeger | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: jaeger | ||
template: | ||
metadata: | ||
labels: | ||
app: jaeger | ||
spec: | ||
containers: | ||
- name: jaeger | ||
image: jaegertracing/all-in-one:latest | ||
|
||
--- | ||
|
||
# Jaegar Service | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: jaeger | ||
spec: | ||
ports: | ||
- name: ui-port | ||
port: 16686 | ||
targetPort: 16686 | ||
protocol: TCP | ||
- name: collector-port | ||
port: 14268 | ||
targetPort: 14268 | ||
protocol: TCP | ||
selector: | ||
app: jaeger |
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,105 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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. | ||
|
||
// This file contains an example of how to use the plugin API to configure how | ||
// metrics and traces are exported. We register a plugin to export traces to | ||
// Jaeger and a plugin to export metrics to Prometheus. Compile the telemetry | ||
// binary and use it as you would "weaver kube". Use "prometheus.yaml" and | ||
// "jaeger.yaml" to deploy Prometheus and Jaeger to a Kubernetes cluster. | ||
// | ||
// $ kubectl apply \ | ||
// -f jaeger.yaml \ | ||
// -f prometheus.yaml \ | ||
// -f $(telemetry deploy kube_deploy.yaml) | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/ServiceWeaver/weaver-kube/tool" | ||
"github.com/ServiceWeaver/weaver/runtime/metrics" | ||
"github.com/ServiceWeaver/weaver/runtime/prometheus" | ||
"go.opentelemetry.io/otel/exporters/jaeger" //lint:ignore SA1019 TODO: Update | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
const ( | ||
// The Jaeger and Prometheus ports. These values should be the same as the | ||
// ones in jaeger.yaml and prometheus.yaml. | ||
jaegerPort = 14268 | ||
prometheusPort = 9090 | ||
) | ||
|
||
// prometheusExporter exports metrics via HTTP in Prometheus text format. | ||
type prometheusExporter struct { | ||
mu sync.Mutex | ||
metrics []*metrics.MetricSnapshot | ||
} | ||
|
||
// serve runs an HTTP server that exports metrics in Prometheus text format. | ||
func (p *prometheusExporter) serve(lis net.Listener) error { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
var b bytes.Buffer | ||
prometheus.TranslateMetricsToPrometheusTextFormat(&b, p.metrics, r.Host, "/metrics") | ||
w.Write(b.Bytes()) //nolint:errcheck // response write error | ||
}) | ||
return http.Serve(lis, mux) | ||
} | ||
|
||
// handleMetrics implements tool.Plugins.HandleMetrics. | ||
func (p *prometheusExporter) handleMetrics(_ context.Context, metrics []*metrics.MetricSnapshot) error { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.metrics = metrics | ||
return nil | ||
} | ||
|
||
func main() { | ||
// Export traces to Jaegar. | ||
jaegerURL := fmt.Sprintf("http://jaeger:%d/api/traces", jaegerPort) | ||
endpoint := jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(jaegerURL)) | ||
traceExporter, err := jaeger.New(endpoint) | ||
if err != nil { | ||
panic(err) | ||
} | ||
handleTraceSpans := func(ctx context.Context, spans []trace.ReadOnlySpan) error { | ||
return traceExporter.ExportSpans(ctx, spans) | ||
} | ||
|
||
// Export metrics to Prometheus. | ||
p := &prometheusExporter{} | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", prometheusPort)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
if err := p.serve(lis); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
tool.Run("deploy", tool.Plugins{ | ||
HandleTraceSpans: handleTraceSpans, | ||
HandleMetrics: p.handleMetrics, | ||
}) | ||
} |
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,112 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# 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. | ||
|
||
# Give Prometheus permissions to list pods. | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
creationTimestamp: null | ||
name: pods-getter | ||
namespace: default | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
verbs: | ||
- get | ||
- list | ||
- watch | ||
|
||
--- | ||
|
||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
creationTimestamp: null | ||
name: default-pods-getter | ||
namespace: default | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: pods-getter | ||
subjects: | ||
- kind: ServiceAccount | ||
name: default | ||
namespace: default | ||
|
||
--- | ||
|
||
# Store prometheus.yaml in a ConfigMap. | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: prometheus-config | ||
data: | ||
prometheus.yaml: | | ||
global: | ||
scrape_interval: 15s | ||
scrape_configs: | ||
- job_name: "serviceweaver" | ||
kubernetes_sd_configs: | ||
- role: pod | ||
--- | ||
|
||
# Prometheus Service. | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: prometheus | ||
spec: | ||
ports: | ||
- port: 80 | ||
protocol: TCP | ||
targetPort: 9090 | ||
selector: | ||
app: prometheus | ||
|
||
--- | ||
|
||
# Prometheus Deployment. | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: prometheus | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: prometheus | ||
template: | ||
metadata: | ||
labels: | ||
app: prometheus | ||
spec: | ||
containers: | ||
- name: prometheus | ||
image: prom/prometheus:v2.30.3 | ||
args: | ||
- --config.file=/etc/prometheus.yaml | ||
- --storage.tsdb.path=/prometheus | ||
ports: | ||
- containerPort: 9090 | ||
volumeMounts: | ||
- mountPath: /etc/prometheus.yaml | ||
name: prometheus-config | ||
subPath: prometheus.yaml | ||
volumes: | ||
- name: prometheus-config | ||
configMap: | ||
name: prometheus-config |
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