Skip to content

Commit 9bcbc1b

Browse files
committed
feat: add /metrics endpoint
1 parent 830632a commit 9bcbc1b

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

lib/pact_broker/api.rb

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ module PactBroker
9191

9292
add ['integrations'], Api::Resources::Integrations, {resource_name: "integrations"}
9393
add ['integrations', 'provider', :provider_name, 'consumer', :consumer_name], Api::Resources::Integration, {resource_name: "integration"}
94+
add ['metrics'], Api::Resources::Metrics, {resource_name: 'metrics'}
9495
add [], Api::Resources::Index, {resource_name: "index"}
9596
end
9697
end

lib/pact_broker/api/resources/index.rb

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def links
106106
title: "Get, create or delete a tag for a pacticipant version",
107107
templated: true
108108
},
109+
'pb:metrics' =>
110+
{
111+
href: base_url + '/metrics',
112+
title: "Get Pact Broker metrics",
113+
},
109114
'beta:pending-provider-pacts' =>
110115
{
111116
href: base_url + '/pacts/provider/{provider}/pending',
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'pact_broker/api/resources/base_resource'
2+
3+
module PactBroker
4+
module Api
5+
module Resources
6+
class Metrics < BaseResource
7+
def content_types_provided
8+
[["application/hal+json", :to_json]]
9+
end
10+
11+
def allowed_methods
12+
["GET", "OPTIONS"]
13+
end
14+
15+
def to_json
16+
metrics_service.metrics.to_json
17+
end
18+
end
19+
end
20+
end
21+
end

lib/pact_broker/metrics/service.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'pact_broker/pacts/pact_publication'
2+
require 'pact_broker/pacts/pact_version'
3+
require 'pact_broker/domain/pacticipant'
4+
require 'pact_broker/integrations/integration'
5+
require 'pact_broker/domain/verification'
6+
require 'pact_broker/domain/version'
7+
require 'pact_broker/api/decorators/format_date_time'
8+
require 'pact_broker/webhooks/webhook'
9+
require 'pact_broker/webhooks/triggered_webhook'
10+
require 'pact_broker/webhooks/execution'
11+
12+
module PactBroker
13+
module Metrics
14+
module Service
15+
include PactBroker::Api::Decorators::FormatDateTime
16+
17+
extend self
18+
19+
def metrics
20+
{
21+
pacticipants: {
22+
count: PactBroker::Domain::Pacticipant.count
23+
},
24+
integrations: {
25+
count: PactBroker::Integrations::Integration.count
26+
},
27+
pactPublications: {
28+
count: PactBroker::Pacts::PactPublication.count,
29+
first: format_date_time(PactBroker::Pacts::PactPublication.order(:id).first&.created_at),
30+
last: format_date_time(PactBroker::Pacts::PactPublication.order(:id).last&.created_at)
31+
},
32+
pactVersions: {
33+
count: PactBroker::Pacts::PactVersion.count
34+
},
35+
verificationResults: {
36+
count: PactBroker::Domain::Verification.count,
37+
first: format_date_time(PactBroker::Domain::Verification.order(:id).first.created_at),
38+
last: format_date_time(PactBroker::Domain::Verification.order(:id).last.created_at)
39+
},
40+
pacticipantVersions: {
41+
count: PactBroker::Domain::Version.count
42+
},
43+
webhooks: {
44+
count: PactBroker::Webhooks::Webhook.count
45+
},
46+
triggeredWebhooks: {
47+
count: PactBroker::Webhooks::TriggeredWebhook.count
48+
},
49+
webhookExecutions: {
50+
count: PactBroker::Webhooks::Execution.count
51+
}
52+
}
53+
end
54+
end
55+
end
56+
end

lib/pact_broker/services.rb

+5
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ def webhook_trigger_service
7171
require 'pact_broker/webhooks/trigger_service'
7272
Webhooks::TriggerService
7373
end
74+
75+
def metrics_service
76+
require 'pact_broker/metrics/service'
77+
Metrics::Service
78+
end
7479
end
7580
end

spec/features/metrics_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
describe "get metrics" do
2+
let(:path) { "/metrics"}
3+
let(:json_response_body) { JSON.parse(subject.body, symbolize_names: true) }
4+
5+
subject { get(path) }
6+
7+
before do
8+
TestDataBuilder.new
9+
.create_consumer("Consumer")
10+
.create_provider("Provider")
11+
.create_consumer_version("1.2.3")
12+
.create_consumer_version_tag("prod")
13+
.create_pact
14+
.create_verification(provider_version: "4.5.6")
15+
.create_webhook
16+
.create_triggered_webhook
17+
.create_webhook_execution
18+
end
19+
20+
it "returns some metrics" do
21+
expect(json_response_body).to be_instance_of(Hash)
22+
end
23+
end

0 commit comments

Comments
 (0)