Skip to content

Commit

Permalink
Revise metrics emission to reflect data for both GitLab and Bitbucket
Browse files Browse the repository at this point in the history
The display of metrics in pipelines as code
now includes information for GitLab and Bitbucket.

Signed-off-by: Savita Ashture <[email protected]>
  • Loading branch information
savitaashture committed Jan 5, 2024
1 parent b89347a commit aeaf2cf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/content/docs/guide/repositorycrd.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ and the scoping fails for the repository level configuration because the reposit
namespace: test-repo
spec:
url: "https://github.com/linda/project"
setting
settings:
github_app_token_scope_repos:
- "owner5/project5"
```
Expand Down
15 changes: 15 additions & 0 deletions docs/content/docs/install/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Running the PipelineRun
weight: 4
---

# Metrics Overview

The metrics for pipelines-as-code can be accessed through the `pipelines-as-code-watcher` service on port `9090`.

pipelines-as-code supports various exporters, such as Prometheus, Google Stackdriver, and more.
You can configure these exporters by referring to the [observability configuration](../config/config-observability.yaml).

| Name | Type | Description |
| ---------- |---------|-----------------------------------------------------|
| `pipelines_as_code_pipelinerun_count` | Counter | Number of pipelineruns created by pipelines-as-code |
9 changes: 7 additions & 2 deletions pkg/reconciler/emit_metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package reconciler

import (
"strings"
"fmt"

"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
Expand All @@ -11,12 +11,17 @@ func (r *Reconciler) emitMetrics(pr *tektonv1.PipelineRun) error {
gitProvider := pr.GetAnnotations()[keys.GitProvider]
eventType := pr.GetAnnotations()[keys.EventType]

if strings.HasPrefix(gitProvider, "github") {
switch gitProvider {
case "github":
if _, ok := pr.GetAnnotations()[keys.InstallationID]; ok {
gitProvider += "-app"
} else {
gitProvider += "-webhook"
}
case "gitlab", "gitea", "bitbucket-cloud", "bitbucket-server":
gitProvider += "-webhook"
default:
return fmt.Errorf("no supported Git provider")
}

return r.metrics.Count(gitProvider, eventType)
Expand Down
71 changes: 71 additions & 0 deletions pkg/reconciler/emit_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package reconciler

import (
"testing"

"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/metrics"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestEmitMetrics(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
wantErr bool
}{
{
name: "provider is GitHub App",
annotations: map[string]string{
keys.GitProvider: "github",
keys.EventType: "pull_request",
keys.InstallationID: "123",
},
wantErr: false,
},
{
name: "provider is GitHub Webhook",
annotations: map[string]string{
keys.GitProvider: "github",
keys.EventType: "pull_request",
},
wantErr: false,
},
{
name: "provider is GitLab",
annotations: map[string]string{
keys.GitProvider: "gitlab",
keys.EventType: "push",
},
wantErr: false,
},
{
name: "unsupported provider",
annotations: map[string]string{
keys.GitProvider: "unsupported",
keys.EventType: "push",
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m, err := metrics.NewRecorder()
assert.NilError(t, err)
r := &Reconciler{
metrics: m,
}
pr := &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Annotations: tt.annotations,
},
}
if err = r.emitMetrics(pr); (err != nil) != tt.wantErr {
t.Errorf("emitMetrics() error = %v, wantErr %v", err != nil, tt.wantErr)
}
})
}
}

0 comments on commit aeaf2cf

Please sign in to comment.