Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
avtakkar committed Mar 29, 2024
1 parent b556ded commit c750262
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 33 deletions.
1 change: 1 addition & 0 deletions build/ci/scripts/azure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ print_peerd_metrics() {
for pod in $( echo "$p" | tr -s " " "\012" ); do
echo "checking pod '$pod' for metrics"
kubectl -n peerd-ns exec -i $pod -- bash -c "cat /var/log/peerdmetrics"
kubectl --context=$KIND_CLUSTER_CONTEXT -n peerd-ns exec -i $pod -- bash -c "curl http://localhost:5004/metrics/prometheus"
done
}

Expand Down
1 change: 1 addition & 0 deletions build/ci/scripts/kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ print_p2p_metrics() {
for pod in $( echo "$p" | tr -s " " "\012" ); do
echo "checking pod '$pod' for metrics"
kubectl --context=$KIND_CLUSTER_CONTEXT -n peerd-ns exec -i $pod -- bash -c "cat /var/log/peerdmetrics"
kubectl --context=$KIND_CLUSTER_CONTEXT -n peerd-ns exec -i $pod -- bash -c "curl http://localhost:5004/metrics/prometheus"
done
}

Expand Down
4 changes: 2 additions & 2 deletions build/package/peerd-helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.1-alpha
version: 0.0.2-alpha

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.0.1-alpha"
appVersion: "0.0.4-alpha"
60 changes: 60 additions & 0 deletions build/package/peerd-helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "peerd.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "peerd.namespace" -}}
{{ include "peerd.name" . }}-ns
{{- end }}

{{- define "peerd.serviceAccountName" -}}
{{ include "peerd.name" . }}-sa
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "peerd.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "peerd.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "peerd.labels" -}}
helm.sh/chart: {{ include "peerd.chart" . }}
{{ include "peerd.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Common selector labels
*/}}
{{- define "peerd.selectorLabels" -}}
app: {{ include "peerd.name" . }}
app.kubernetes.io/name: {{ include "peerd.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
20 changes: 12 additions & 8 deletions build/package/peerd-helm/templates/app.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
apiVersion: v1
kind: Namespace
metadata:
name: peerd-ns
name: {{ include "peerd.namespace" . }}
labels:
{{- include "peerd.labels" . | nindent 4 }}
---
kind: ConfigMap
apiVersion: v1
Expand All @@ -24,14 +26,16 @@ data:
metadata:
name: ama-metrics-prometheus-config
namespace: kube-system
labels:
{{- include "peerd.labels" . | nindent 4 }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: &name peerd
namespace: peerd-ns
name: &name {{ include "peerd.name" . }}
namespace: {{ include "peerd.namespace" . }}
labels:
app: *name
{{- include "peerd.labels" . | nindent 4 }}
annotations:
prometheus.io/scrape: 'true'
prometheus.io/path: '/metrics/prometheus'
Expand All @@ -45,7 +49,7 @@ spec:
labels:
app: *name
spec:
serviceAccountName: peerd-sa
serviceAccountName: {{ include "peerd.serviceAccountName" . }}
containers:
- image: "{{ .Values.peerd.image.ref }}"
imagePullPolicy: "{{ .Values.peerd.image.pullPolicy }}"
Expand Down Expand Up @@ -95,10 +99,10 @@ spec:
apiVersion: v1
kind: Service
metadata:
name: &name peerd
namespace: peerd-ns
name: &name {{ include "peerd.name" . }}
namespace: {{ include "peerd.namespace" . }}
labels:
app: *name
{{- include "peerd.labels" . | nindent 4 }}
annotations:
prometheus.io/scrape: 'true'
prometheus.io/path: '/metrics/prometheus'
Expand Down
38 changes: 21 additions & 17 deletions build/package/peerd-helm/templates/rbac.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: peerd-sa
namespace: peerd-ns
name: {{ include "peerd.serviceAccountName" . }}
namespace: {{ include "peerd.namespace" . }}
labels:
app: peerd
{{- include "peerd.labels" . | nindent 4 }}
---
apiVersion: v1
kind: Secret
metadata:
name: peerd-sa-secret
namespace: peerd-ns
name: {{ include "peerd.serviceAccountName" . }}-secret
namespace: {{ include "peerd.namespace" . }}
labels:
app: peerd
{{- include "peerd.labels" . | nindent 4 }}
annotations:
kubernetes.io/service-account.name: peerd-sa
kubernetes.io/service-account.name: {{ include "peerd.serviceAccountName" . }}
type: kubernetes.io/service-account-token
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: peerd-manager-role
namespace: peerd-ns
name: {{ include "peerd.name" . }}-manager-role
namespace: {{ include "peerd.namespace" . }}
labels:
app: peerd
{{- include "peerd.labels" . | nindent 4 }}
rules:
- apiGroups: ["coordination.k8s.io"]
resources: ["configmaps"]
Expand All @@ -38,23 +38,25 @@ rules:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: peerd-manager-role-binding
namespace: peerd-ns
name: {{ include "peerd.name" . }}-manager-role-binding
namespace: {{ include "peerd.namespace" . }}
labels:
app: peerd
{{- include "peerd.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: peerd-manager-role
subjects:
- kind: ServiceAccount
name: peerd-sa
namespace: peerd-ns
name: {{ include "peerd.serviceAccountName" . }}
namespace: {{ include "peerd.namespace" . }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader-and-event-creator-clusterrole
labels:
{{- include "peerd.labels" . | nindent 4 }}
rules:
- apiGroups: [""]
resources: ["nodes"]
Expand All @@ -67,10 +69,12 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: node-reader-and-event-creator-clusterrole-binding
labels:
{{- include "peerd.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: peerd-sa
namespace: peerd-ns
name: {{ include "peerd.serviceAccountName" . }}
namespace: {{ include "peerd.namespace" . }}
roleRef:
kind: ClusterRole
name: node-reader-and-event-creator-clusterrole
Expand Down
4 changes: 3 additions & 1 deletion internal/metrics/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.
package metrics

import "github.com/prometheus/client_golang/prometheus"

// Metrics defines an interface to collect p2p metrics.
type Metrics interface {
// RecordRequest records the time it takes to process a request.
Expand All @@ -18,4 +20,4 @@ type Metrics interface {
}

// Global is the global metrics collector.
var Global Metrics = NewPromMetrics()
var Global Metrics = NewPromMetrics(prometheus.DefaultRegisterer)
10 changes: 5 additions & 5 deletions internal/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@ func (m *promMetrics) RecordUpstreamResponse(hostname string, key string, op str
}

// NewPromMetrics creates a new instance of promMetrics.
func NewPromMetrics() *promMetrics {
func NewPromMetrics(reg prometheus.Registerer) *promMetrics {

requestDurationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "peerd_request_duration_seconds",
Help: "Duration of requests in seconds.",
Buckets: prometheus.LinearBuckets(0.005, 0.025, 200),
}, []string{"self", "method", "handler"})
prometheus.MustRegister(requestDurationHist)
reg.MustRegister(requestDurationHist)

peerDiscoveryDurationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "peerd_peer_discovery_duration_seconds",
Help: "Duration of peer discovery in seconds.",
Buckets: prometheus.LinearBuckets(0.001, 0.002, 200),
}, []string{"self", "ip"})
prometheus.MustRegister(peerDiscoveryDurationHist)
reg.MustRegister(peerDiscoveryDurationHist)

peerResponseDurationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "peerd_peer_response_speed_mib_per_second",
Help: "Speed of peer response in Mib per second.",
Buckets: prometheus.LinearBuckets(1, 15, 200),
}, []string{"self", "ip", "op"})
prometheus.MustRegister(peerResponseDurationHist)
reg.MustRegister(peerResponseDurationHist)

upstreamResponseDurationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "peerd_upstream_response_speed_mib_per_seconds",
Help: "Speed of upstream response in Mib per second.",
Buckets: prometheus.LinearBuckets(1, 15, 200),
}, []string{"self", "hostname", "op"})
prometheus.MustRegister(upstreamResponseDurationHist)
reg.MustRegister(upstreamResponseDurationHist)

return &promMetrics{
requestDuration: requestDurationHist,
Expand Down
Loading

0 comments on commit c750262

Please sign in to comment.