Skip to content

Commit d237fc9

Browse files
authored
Add OpenTelemetry metrics instrumentation (#290)
## Motivation and Context - It is important to have basic observability in place so that availability issues can be figured out easily ## How Has This Been Tested? - Registration of metrics is tested - Prometheus endpoint `/metrics` is included in test cases to test the metrics exposed - Test locally to understand any regression. ## Breaking Changes - No ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context - Telemetry implementation is currently done by keeping metrics in mind as of now, other types of telemetry can be added on top of it later. - Instrumentation takes care of these problems already: - Cardinality control by limiting latency buckets and not including labels that grows with throughput. - Latency buckets are added as per SLA guidelines - Each metric prefixed with `mcp` to easily identify. - Pulling mechanism is used since the idea is to scrape metrics via an agent like vmagent, prometheus agent or otel-collector. - Metrics follow standard approach of defining counters, histogram and gauges as per opentelemetry guidelines.
1 parent 12ab632 commit d237fc9

File tree

11 files changed

+538
-21
lines changed

11 files changed

+538
-21
lines changed

cmd/registry/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/modelcontextprotocol/registry/internal/database"
1717
"github.com/modelcontextprotocol/registry/internal/model"
1818
"github.com/modelcontextprotocol/registry/internal/service"
19+
"github.com/modelcontextprotocol/registry/internal/telemetry"
1920
)
2021

2122
func main() {
@@ -91,8 +92,20 @@ func main() {
9192
}
9293
}
9394

95+
shutdownTelemetry, metrics, err := telemetry.InitMetrics(cfg.Version)
96+
if err != nil {
97+
log.Printf("Failed to initialize metrics: %v", err)
98+
return
99+
}
100+
101+
defer func() {
102+
if err := shutdownTelemetry(context.Background()); err != nil {
103+
log.Printf("Failed to shutdown telemetry: %v", err)
104+
}
105+
}()
106+
94107
// Initialize HTTP server
95-
server := api.NewServer(cfg, registryService)
108+
server := api.NewServer(cfg, registryService, metrics)
96109

97110
// Start server in a goroutine so it doesn't block signal handling
98111
go func() {

go.mod

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,47 @@ go 1.24.0
55
require (
66
github.com/caarlos0/env/v11 v11.3.1
77
github.com/danielgtaylor/huma/v2 v2.34.1
8+
github.com/golang-jwt/jwt/v5 v5.3.0
89
github.com/google/uuid v1.6.0
10+
github.com/prometheus/client_golang v1.23.0
911
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
1012
github.com/stretchr/testify v1.10.0
1113
go.mongodb.org/mongo-driver v1.17.3
14+
go.opentelemetry.io/contrib/instrumentation/runtime v0.62.0
15+
go.opentelemetry.io/otel v1.37.0
16+
go.opentelemetry.io/otel/exporters/prometheus v0.59.1
17+
go.opentelemetry.io/otel/metric v1.37.0
18+
go.opentelemetry.io/otel/sdk v1.37.0
19+
go.opentelemetry.io/otel/sdk/metric v1.37.0
1220
)
1321

1422
require (
23+
github.com/beorn7/perks v1.0.1 // indirect
24+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1525
github.com/davecgh/go-spew v1.1.1 // indirect
16-
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
26+
github.com/go-logr/logr v1.4.3 // indirect
27+
github.com/go-logr/stdr v1.2.2 // indirect
1728
github.com/golang/snappy v0.0.4 // indirect
29+
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
1830
github.com/klauspost/compress v1.18.0 // indirect
19-
github.com/kr/text v0.2.0 // indirect
2031
github.com/montanaflynn/stats v0.7.1 // indirect
32+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2133
github.com/pmezard/go-difflib v1.0.0 // indirect
22-
github.com/rogpeppe/go-internal v1.11.0 // indirect
34+
github.com/prometheus/client_model v0.6.2 // indirect
35+
github.com/prometheus/common v0.65.0 // indirect
36+
github.com/prometheus/otlptranslator v0.0.0-20250717125610-8549f4ab4f8f // indirect
37+
github.com/prometheus/procfs v0.17.0 // indirect
2338
github.com/stretchr/objx v0.5.2 // indirect
2439
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
2540
github.com/xdg-go/scram v1.1.2 // indirect
2641
github.com/xdg-go/stringprep v1.0.4 // indirect
2742
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
43+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
44+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
2845
golang.org/x/crypto v0.41.0 // indirect
2946
golang.org/x/sync v0.16.0 // indirect
47+
golang.org/x/sys v0.35.0 // indirect
3048
golang.org/x/text v0.28.0 // indirect
31-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
49+
google.golang.org/protobuf v1.36.6 // indirect
3250
gopkg.in/yaml.v3 v3.0.1 // indirect
3351
)

go.sum

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
13
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
24
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
3-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
5+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
6+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
47
github.com/danielgtaylor/huma/v2 v2.34.1 h1:EmOJAbzEGfy0wAq/QMQ1YKfEMBEfE94xdBRLPBP0gwQ=
58
github.com/danielgtaylor/huma/v2 v2.34.1/go.mod h1:ynwJgLk8iGVgoaipi5tgwIQ5yoFNmiu+QdhU7CEEmhk=
69
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
710
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
12+
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
13+
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
14+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
15+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
816
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
917
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
1018
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
@@ -13,21 +21,34 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1321
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1422
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1523
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
24+
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
25+
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
1626
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
1727
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
18-
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1928
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2029
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
21-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
22-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2330
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2431
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
32+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
33+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
2534
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
2635
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
36+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
37+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
2738
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2839
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
29-
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
30-
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
40+
github.com/prometheus/client_golang v1.23.0 h1:ust4zpdl9r4trLY/gSjlm07PuiBq2ynaXXlptpfy8Uc=
41+
github.com/prometheus/client_golang v1.23.0/go.mod h1:i/o0R9ByOnHX0McrTMTyhYvKE4haaf2mW08I+jGAjEE=
42+
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
43+
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
44+
github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE=
45+
github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
46+
github.com/prometheus/otlptranslator v0.0.0-20250717125610-8549f4ab4f8f h1:QQB6SuvGZjK8kdc2YaLJpYhV8fxauOsjE6jgcL6YJ8Q=
47+
github.com/prometheus/otlptranslator v0.0.0-20250717125610-8549f4ab4f8f/go.mod h1:P8AwMgdD7XEr6QRUJ2QWLpiAZTgTE2UYgjlu3svompI=
48+
github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0=
49+
github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw=
50+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
51+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
3152
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
3253
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
3354
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
@@ -45,6 +66,24 @@ github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfS
4566
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
4667
go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeHxQ=
4768
go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
69+
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
70+
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
71+
go.opentelemetry.io/contrib/instrumentation/runtime v0.62.0 h1:ZIt0ya9/y4WyRIzfLC8hQRRsWg0J9M9GyaGtIMiElZI=
72+
go.opentelemetry.io/contrib/instrumentation/runtime v0.62.0/go.mod h1:F1aJ9VuiKWOlWwKdTYDUp1aoS0HzQxg38/VLxKmhm5U=
73+
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
74+
go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I=
75+
go.opentelemetry.io/otel/exporters/prometheus v0.59.1 h1:HcpSkTkJbggT8bjYP+BjyqPWlD17BH9C5CYNKeDzmcA=
76+
go.opentelemetry.io/otel/exporters/prometheus v0.59.1/go.mod h1:0FJL+gjuUoM07xzik3KPBaN+nz/CoB15kV6WLMiXZag=
77+
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
78+
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
79+
go.opentelemetry.io/otel/sdk v1.37.0 h1:ItB0QUqnjesGRvNcmAcU0LyvkVyGJ2xftD29bWdDvKI=
80+
go.opentelemetry.io/otel/sdk v1.37.0/go.mod h1:VredYzxUvuo2q3WRcDnKDjbdvmO0sCzOvVAiY+yUkAg=
81+
go.opentelemetry.io/otel/sdk/metric v1.37.0 h1:90lI228XrB9jCMuSdA0673aubgRobVZFhbjxHHspCPc=
82+
go.opentelemetry.io/otel/sdk/metric v1.37.0/go.mod h1:cNen4ZWfiD37l5NhS+Keb5RXVWZWpRE+9WyVCpbo5ps=
83+
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
84+
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
85+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
86+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
4887
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4988
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
5089
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
@@ -62,6 +101,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
62101
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63102
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
64103
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
104+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
105+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
65106
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
66107
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
67108
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -74,6 +115,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
74115
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
75116
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
76117
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
118+
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
119+
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
77120
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
78121
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
79122
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

internal/api/handlers/v0/health.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import (
55
"net/http"
66

77
"github.com/danielgtaylor/huma/v2"
8+
"go.opentelemetry.io/otel/attribute"
9+
"go.opentelemetry.io/otel/metric"
10+
811
"github.com/modelcontextprotocol/registry/internal/config"
12+
"github.com/modelcontextprotocol/registry/internal/telemetry"
913
)
1014

1115
// HealthBody represents the health check response body
@@ -15,15 +19,18 @@ type HealthBody struct {
1519
}
1620

1721
// RegisterHealthEndpoint registers the health check endpoint
18-
func RegisterHealthEndpoint(api huma.API, cfg *config.Config) {
22+
func RegisterHealthEndpoint(api huma.API, cfg *config.Config, metrics *telemetry.Metrics) {
1923
huma.Register(api, huma.Operation{
2024
OperationID: "get-health",
2125
Method: http.MethodGet,
2226
Path: "/v0/health",
2327
Summary: "Health check",
2428
Description: "Check the health status of the API",
2529
Tags: []string{"health"},
26-
}, func(_ context.Context, _ *struct{}) (*Response[HealthBody], error) {
30+
}, func(ctx context.Context, _ *struct{}) (*Response[HealthBody], error) {
31+
// Record the health check metrics
32+
recordHealthMetrics(ctx, metrics, "/v0/health", cfg.Version)
33+
2734
return &Response[HealthBody]{
2835
Body: HealthBody{
2936
Status: "ok",
@@ -32,3 +39,15 @@ func RegisterHealthEndpoint(api huma.API, cfg *config.Config) {
3239
}, nil
3340
})
3441
}
42+
43+
// recordHealthMetrics records the health check metrics
44+
func recordHealthMetrics(ctx context.Context, metrics *telemetry.Metrics, path string, version string) {
45+
attrs := []attribute.KeyValue{
46+
attribute.String("path", path),
47+
attribute.String("version", version),
48+
attribute.String("service", telemetry.Namespace),
49+
}
50+
51+
// metric : Up status (1 = healthy, 0 = unhealthy)
52+
metrics.Up.Record(ctx, 1, metric.WithAttributes(attrs...))
53+
}

internal/api/handlers/v0/health_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package v0_test
22

33
import (
4+
"context"
45
"net/http"
56
"net/http/httptest"
67
"testing"
78

89
"github.com/danielgtaylor/huma/v2"
910
"github.com/danielgtaylor/huma/v2/adapters/humago"
11+
"github.com/stretchr/testify/assert"
12+
1013
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
1114
"github.com/modelcontextprotocol/registry/internal/config"
12-
"github.com/stretchr/testify/assert"
15+
"github.com/modelcontextprotocol/registry/internal/telemetry"
1316
)
1417

1518
func TestHealthEndpoint(t *testing.T) {
@@ -50,8 +53,10 @@ func TestHealthEndpoint(t *testing.T) {
5053
mux := http.NewServeMux()
5154
api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0"))
5255

56+
shutdownTelemetry, metrics, _ := telemetry.InitMetrics("test")
57+
5358
// Register the health endpoint
54-
v0.RegisterHealthEndpoint(api, tc.config)
59+
v0.RegisterHealthEndpoint(api, tc.config, metrics)
5560

5661
// Create a test request
5762
req := httptest.NewRequest(http.MethodGet, "/v0/health", nil)
@@ -60,6 +65,9 @@ func TestHealthEndpoint(t *testing.T) {
6065
// Serve the request
6166
mux.ServeHTTP(w, req)
6267

68+
// shut down the metric provider
69+
_ = shutdownTelemetry(context.Background())
70+
6371
// Check the status code
6472
assert.Equal(t, tc.expectedStatus, w.Code)
6573

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package v0_test
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/danielgtaylor/huma/v2"
10+
"github.com/danielgtaylor/huma/v2/adapters/humago"
11+
"github.com/google/uuid"
12+
"github.com/stretchr/testify/assert"
13+
14+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
15+
"github.com/modelcontextprotocol/registry/internal/api/router"
16+
"github.com/modelcontextprotocol/registry/internal/config"
17+
"github.com/modelcontextprotocol/registry/internal/model"
18+
"github.com/modelcontextprotocol/registry/internal/telemetry"
19+
)
20+
21+
func mockServerEndpoint(registry *MockRegistryService, serverID string) {
22+
serverDetail := &model.ServerDetail{
23+
Server: model.Server{
24+
ID: serverID,
25+
Name: "test-server-detail",
26+
Description: "Test server detail",
27+
Repository: model.Repository{
28+
URL: "https://github.com/example/test-server-detail",
29+
Source: "github",
30+
ID: "example/test-server-detail",
31+
},
32+
VersionDetail: model.VersionDetail{
33+
Version: "2.0.0",
34+
ReleaseDate: "2025-05-27T12:00:00Z",
35+
IsLatest: true,
36+
},
37+
},
38+
}
39+
registry.Mock.On("GetByID", serverID).Return(serverDetail, nil)
40+
}
41+
42+
func TestPrometheusHandler(t *testing.T) {
43+
mockRegistry := new(MockRegistryService)
44+
45+
serverID := uuid.New().String()
46+
mockServerEndpoint(mockRegistry, serverID)
47+
48+
cfg := config.NewConfig()
49+
shutdownTelemetry, metrics, _ := telemetry.InitMetrics("dev")
50+
51+
mux := http.NewServeMux()
52+
api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0"))
53+
54+
// Add metrics middleware with options
55+
api.UseMiddleware(router.MetricTelemetryMiddleware(metrics,
56+
router.WithSkipPaths("/health", "/metrics", "/ping", "/docs"),
57+
))
58+
v0.RegisterHealthEndpoint(api, cfg, metrics)
59+
v0.RegisterServersEndpoints(api, mockRegistry)
60+
61+
// Add /metrics for Prometheus metrics using promhttp
62+
mux.Handle("/metrics", metrics.PrometheusHandler())
63+
64+
// Create request
65+
url := "/v0/servers/" + serverID
66+
req := httptest.NewRequest(http.MethodGet, url, nil)
67+
w := httptest.NewRecorder()
68+
69+
// Serve the request
70+
mux.ServeHTTP(w, req)
71+
72+
// Check the status code
73+
if w.Code != http.StatusOK {
74+
t.Fatalf("expected status %d, got %d", http.StatusOK, w.Code)
75+
}
76+
77+
req = httptest.NewRequest(http.MethodGet, "/metrics", nil)
78+
w = httptest.NewRecorder()
79+
mux.ServeHTTP(w, req)
80+
81+
// shutdown metrics provider
82+
_ = shutdownTelemetry(context.Background())
83+
84+
assert.Equal(t, http.StatusOK, w.Code, "Expected status OK for /metrics endpoint")
85+
86+
body := w.Body.String()
87+
// Check if the response body contains expected metrics
88+
assert.Contains(t, body, "mcp_registry_http_request_duration_bucket")
89+
assert.Contains(t, body, "mcp_registry_http_requests_total")
90+
assert.Contains(t, body, "path=\"/v0/servers/{id}\"")
91+
}

0 commit comments

Comments
 (0)