Skip to content

Commit 7b3ac16

Browse files
Init OpenTelemetry Support
1 parent 6d5aa92 commit 7b3ac16

File tree

9 files changed

+255
-12
lines changed

9 files changed

+255
-12
lines changed

assets/go-licenses.json

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"code.gitea.io/gitea/models/db"
1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/opentelemetry"
2021
"code.gitea.io/gitea/modules/setting"
2122
"code.gitea.io/gitea/modules/util"
2223

@@ -67,6 +68,9 @@ func initDB(ctx context.Context) error {
6768
Ensure you are running in the correct environment or set the correct configuration file with -c.
6869
If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
6970
}
71+
if err := opentelemetry.InitOtel(ctx); err != nil {
72+
return fmt.Errorf("unable to initialize OpenTelemetry. Error: %w", err)
73+
}
7074
if err := db.InitEngine(ctx); err != nil {
7175
return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err)
7276
}

go.mod

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ require (
104104
github.com/prometheus/client_golang v1.20.5
105105
github.com/quasoft/websspi v1.1.2
106106
github.com/redis/go-redis/v9 v9.7.0
107+
github.com/riandyrn/otelchi v0.11.0
107108
github.com/robfig/cron/v3 v3.0.1
108109
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
109110
github.com/sassoftware/go-rpmutils v0.4.0
@@ -113,6 +114,7 @@ require (
113114
github.com/syndtr/goleveldb v1.0.0
114115
github.com/tstranex/u2f v1.0.0
115116
github.com/ulikunitz/xz v0.5.12
117+
github.com/uptrace/opentelemetry-go-extra/otelsql v0.3.2
116118
github.com/urfave/cli/v2 v2.27.5
117119
github.com/wneessen/go-mail v0.5.2
118120
github.com/xanzy/go-gitlab v0.112.0
@@ -121,6 +123,8 @@ require (
121123
github.com/yuin/goldmark v1.7.8
122124
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
123125
github.com/yuin/goldmark-meta v1.1.0
126+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0
127+
go.opentelemetry.io/otel/sdk v1.32.0
124128
golang.org/x/crypto v0.31.0
125129
golang.org/x/image v0.21.0
126130
golang.org/x/net v0.33.0
@@ -139,6 +143,16 @@ require (
139143
xorm.io/xorm v1.3.9
140144
)
141145

146+
require (
147+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
148+
github.com/go-logr/logr v1.4.2 // indirect
149+
github.com/go-logr/stdr v1.2.2 // indirect
150+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
151+
go.opentelemetry.io/otel/metric v1.32.0 // indirect
152+
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
153+
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
154+
)
155+
142156
require (
143157
cloud.google.com/go/compute/metadata v0.5.2 // indirect
144158
dario.cat/mergo v1.0.1 // indirect
@@ -310,15 +324,18 @@ require (
310324
github.com/zeebo/blake3 v0.2.4 // indirect
311325
go.etcd.io/bbolt v1.3.11 // indirect
312326
go.mongodb.org/mongo-driver v1.17.1 // indirect
313-
go.opentelemetry.io/otel v1.31.0 // indirect
314-
go.opentelemetry.io/otel/trace v1.31.0 // indirect
327+
go.opentelemetry.io/otel v1.32.0
328+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
329+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0
330+
go.opentelemetry.io/otel/sdk/metric v1.32.0
331+
go.opentelemetry.io/otel/trace v1.32.0 // indirect
315332
go.uber.org/atomic v1.11.0 // indirect
316333
go.uber.org/multierr v1.11.0 // indirect
317334
go.uber.org/zap v1.27.0 // indirect
318335
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
319336
golang.org/x/mod v0.21.0 // indirect
320337
golang.org/x/time v0.7.0 // indirect
321-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
338+
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
322339
gopkg.in/warnings.v0 v0.1.2 // indirect
323340
gopkg.in/yaml.v2 v2.4.0 // indirect
324341
)

go.sum

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ github.com/caddyserver/certmagic v0.21.4 h1:e7VobB8rffHv8ZZpSiZtEwnLDHUwLVYLWzWS
199199
github.com/caddyserver/certmagic v0.21.4/go.mod h1:swUXjQ1T9ZtMv95qj7/InJvWLXURU85r+CfG0T+ZbDE=
200200
github.com/caddyserver/zerossl v0.1.3 h1:onS+pxp3M8HnHpN5MMbOMyNjmTheJyWRaZYwn+YTAyA=
201201
github.com/caddyserver/zerossl v0.1.3/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4=
202+
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
203+
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
202204
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a h1:MISbI8sU/PSK/ztvmWKFcI7UGb5/HQT7B+i3a2myKgI=
203205
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8=
204206
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -335,6 +337,11 @@ github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
335337
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
336338
github.com/go-ldap/ldap/v3 v3.4.8 h1:loKJyspcRezt2Q3ZRMq2p/0v8iOurlmeXDPw6fikSvQ=
337339
github.com/go-ldap/ldap/v3 v3.4.8/go.mod h1:qS3Sjlu76eHfHGpUdWkAXQTw4beih+cHsco2jXlIXrk=
340+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
341+
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
342+
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
343+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
344+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
338345
github.com/go-openapi/analysis v0.23.0 h1:aGday7OWupfMs+LbmLZG4k0MYXIANxcuBTYUC03zFCU=
339346
github.com/go-openapi/analysis v0.23.0/go.mod h1:9mz9ZWaSlV8TvjQHLl2mUW2PbZtemkE8yA5v22ohupo=
340347
github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w=
@@ -468,6 +475,8 @@ github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/z
468475
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
469476
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
470477
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
478+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 h1:ad0vkEBuk23VJzZR9nkLVG0YAoN9coASF1GusYX6AlU=
479+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0/go.mod h1:igFoXX2ELCW06bol23DWPB5BEWfZISOzSP5K2sbLea0=
471480
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
472481
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
473482
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
@@ -711,6 +720,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
711720
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
712721
github.com/rhysd/actionlint v1.7.3 h1:WD919WuLYrSCwY8VGBqJBEuzyVEIL5viXmXqRRcKOVs=
713722
github.com/rhysd/actionlint v1.7.3/go.mod h1:rl+8ZoX1rqnbcMWKaTyOHmw08mmb/zlmG/Zu1fY47F4=
723+
github.com/riandyrn/otelchi v0.11.0 h1:x9MFoTgHcwCC2DdWkTEEZ2ZQFkbl6z7GXLQtTANN6Gk=
724+
github.com/riandyrn/otelchi v0.11.0/go.mod h1:FlBYmG9fBQu0jFRvZZrATP4mDvLX2H5gwELfpZvNlxY=
714725
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
715726
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
716727
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -810,6 +821,8 @@ github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
810821
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
811822
github.com/unknwon/com v1.0.1 h1:3d1LTxD+Lnf3soQiD4Cp/0BRB+Rsa/+RTvz8GMMzIXs=
812823
github.com/unknwon/com v1.0.1/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
824+
github.com/uptrace/opentelemetry-go-extra/otelsql v0.3.2 h1:ZjUj9BLYf9PEqBn8W/OapxhPjVRdC6CsXTdULHsyk5c=
825+
github.com/uptrace/opentelemetry-go-extra/otelsql v0.3.2/go.mod h1:O8bHQfyinKwTXKkiKNGmLQS7vRsqRxIQTFZpYpHK3IQ=
813826
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
814827
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
815828
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
@@ -866,10 +879,24 @@ go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
866879
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
867880
go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM=
868881
go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
869-
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
870-
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
871-
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
872-
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
882+
go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U=
883+
go.opentelemetry.io/otel v1.32.0/go.mod h1:00DCVSB0RQcnzlwyTfqtxSm+DRr9hpYrHjNGiBHVQIg=
884+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0 h1:t/Qur3vKSkUCcDVaSumWF2PKHt85pc7fRvFuoVT8qFU=
885+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0/go.mod h1:Rl61tySSdcOJWoEgYZVtmnKdA0GeKrSqkHC1t+91CH8=
886+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 h1:IJFEoHiytixx8cMiVAO+GmHR6Frwu+u5Ur8njpFO6Ac=
887+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0/go.mod h1:3rHrKNtLIoS0oZwkY2vxi+oJcwFRWdtUyRII+so45p8=
888+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0 h1:cMyu9O88joYEaI47CnQkxO1XZdpoTF9fEnW2duIddhw=
889+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0/go.mod h1:6Am3rn7P9TVVeXYG+wtcGE7IE1tsQ+bP3AuWcKt/gOI=
890+
go.opentelemetry.io/otel/metric v1.32.0 h1:xV2umtmNcThh2/a/aCP+h64Xx5wsj8qqnkYZktzNa0M=
891+
go.opentelemetry.io/otel/metric v1.32.0/go.mod h1:jH7CIbbK6SH2V2wE16W05BHCtIDzauciCRLoc/SyMv8=
892+
go.opentelemetry.io/otel/sdk v1.32.0 h1:RNxepc9vK59A8XsgZQouW8ue8Gkb4jpWtJm9ge5lEG4=
893+
go.opentelemetry.io/otel/sdk v1.32.0/go.mod h1:LqgegDBjKMmb2GC6/PrTnteJG39I8/vJCAP9LlJXEjU=
894+
go.opentelemetry.io/otel/sdk/metric v1.32.0 h1:rZvFnvmvawYb0alrYkjraqJq0Z4ZUJAiyYCU9snn1CU=
895+
go.opentelemetry.io/otel/sdk/metric v1.32.0/go.mod h1:PWeZlq0zt9YkYAp3gjKZ0eicRYvOh1Gd+X99x6GHpCQ=
896+
go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQDe03fM=
897+
go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8=
898+
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
899+
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
873900
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
874901
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
875902
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
@@ -1036,8 +1063,10 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
10361063
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10371064
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10381065
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1039-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 h1:zciRKQ4kBpFgpfC5QQCVtnnNAcLIqweL7plyZRQHVpI=
1040-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
1066+
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 h1:M0KvPgPmDZHPlbRbaNU1APr28TvwvvdUPlSv7PUvy8g=
1067+
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:dguCy7UOdZhTvLzDyt15+rOrawrpM4q7DD9dQ1P11P4=
1068+
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 h1:XVhgTWWV3kGQlwJHR3upFWZeTsei6Oks1apkZSeonIE=
1069+
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
10411070
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
10421071
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
10431072
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=

models/db/engine_init.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import (
1111
"code.gitea.io/gitea/modules/log"
1212
"code.gitea.io/gitea/modules/setting"
1313

14+
"github.com/uptrace/opentelemetry-go-extra/otelsql"
15+
"go.opentelemetry.io/otel/attribute"
16+
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
1417
"xorm.io/xorm"
18+
"xorm.io/xorm/core"
1519
"xorm.io/xorm/names"
1620
)
1721

@@ -30,15 +34,36 @@ func newXORMEngine() (*xorm.Engine, error) {
3034
}
3135

3236
var engine *xorm.Engine
37+
engineName := setting.Database.Type.String()
3338

3439
if setting.Database.Type.IsPostgreSQL() && len(setting.Database.Schema) > 0 {
3540
// OK whilst we sort out our schema issues - create a schema aware postgres
3641
registerPostgresSchemaDriver()
37-
engine, err = xorm.NewEngine("postgresschema", connStr)
38-
} else {
39-
engine, err = xorm.NewEngine(setting.Database.Type.String(), connStr)
42+
engineName = "postgresschema"
4043
}
4144

45+
var semconvDBSystem attribute.KeyValue
46+
switch setting.Database.Type {
47+
case "sqlite3":
48+
semconvDBSystem = semconv.DBSystemSqlite
49+
case "mysql":
50+
semconvDBSystem = semconv.DBSystemMySQL
51+
case "postgres":
52+
semconvDBSystem = semconv.DBSystemPostgreSQL
53+
case "mssql":
54+
semconvDBSystem = semconv.DBSystemMSSQL
55+
default:
56+
return nil, fmt.Errorf("unsupported database type: %s", setting.Database.Type)
57+
}
58+
59+
db, err := otelsql.Open(engineName, connStr,
60+
otelsql.WithAttributes(semconvDBSystem),
61+
otelsql.WithDBName(setting.Database.Name))
62+
if err != nil {
63+
return engine, err
64+
}
65+
xormCore := core.FromDB(db)
66+
engine, err = xorm.NewEngineWithDB(engineName, connStr, xormCore)
4267
if err != nil {
4368
return nil, err
4469
}

modules/opentelemetry/context.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package opentelemetry
5+
6+
import (
7+
"context"
8+
9+
"go.opentelemetry.io/otel/sdk/metric"
10+
"go.opentelemetry.io/otel/sdk/trace"
11+
)
12+
13+
type Context struct {
14+
context.Context
15+
tp *trace.TracerProvider
16+
mp *metric.MeterProvider
17+
}
18+
19+
var DefaultContext *Context
20+
21+
func GetTracerProvider(ctx context.Context) *trace.TracerProvider {
22+
if telemetryCtx, ok := ctx.(*Context); ok {
23+
return telemetryCtx.tp
24+
}
25+
return DefaultContext.tp
26+
}
27+
28+
func GetMeterProvider(ctx context.Context) *metric.MeterProvider {
29+
if telemetryCtx, ok := ctx.(*Context); ok {
30+
return telemetryCtx.mp
31+
}
32+
return DefaultContext.mp
33+
}
34+
35+
func SetDefaultProviders(ctx context.Context, tp *trace.TracerProvider, mp *metric.MeterProvider) {
36+
DefaultContext = &Context{
37+
Context: ctx,
38+
tp: tp,
39+
mp: mp,
40+
}
41+
}

modules/opentelemetry/telemetry.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package opentelemetry
5+
6+
import (
7+
"context"
8+
9+
"go.opentelemetry.io/otel"
10+
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
11+
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
12+
"go.opentelemetry.io/otel/sdk/metric"
13+
"go.opentelemetry.io/otel/sdk/trace"
14+
)
15+
16+
func InitOtel(ctx context.Context) error {
17+
traceExporter, _ := otlptracehttp.New(ctx)
18+
metricExporter, _ := otlpmetrichttp.New(ctx)
19+
20+
tp := trace.NewTracerProvider(
21+
trace.WithBatcher(traceExporter),
22+
)
23+
otel.SetTracerProvider(tp)
24+
25+
mp := metric.NewMeterProvider(
26+
metric.WithReader(metric.NewPeriodicReader(metricExporter)),
27+
)
28+
otel.SetMeterProvider(mp)
29+
30+
SetDefaultProviders(ctx, tp, mp)
31+
return nil
32+
}

modules/web/route.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func NewRouter() *Router {
5252
return &Router{chiRouter: r}
5353
}
5454

55+
func (r *Router) GetRouter() chi.Router {
56+
return r.chiRouter
57+
}
58+
5559
// Use supports two middlewares
5660
func (r *Router) Use(middlewares ...any) {
5761
for _, m := range middlewares {

routers/init.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/markup"
2020
"code.gitea.io/gitea/modules/markup/external"
21+
"code.gitea.io/gitea/modules/opentelemetry"
2122
"code.gitea.io/gitea/modules/setting"
2223
"code.gitea.io/gitea/modules/ssh"
2324
"code.gitea.io/gitea/modules/storage"
@@ -55,6 +56,9 @@ import (
5556
"code.gitea.io/gitea/services/task"
5657
"code.gitea.io/gitea/services/uinotification"
5758
"code.gitea.io/gitea/services/webhook"
59+
60+
"github.com/riandyrn/otelchi"
61+
otelchimetric "github.com/riandyrn/otelchi/metric"
5862
)
5963

6064
func mustInit(fn func() error) {
@@ -113,6 +117,7 @@ func InitWebInstallPage(ctx context.Context) {
113117

114118
// InitWebInstalled is for global installed configuration.
115119
func InitWebInstalled(ctx context.Context) {
120+
mustInitCtx(ctx, opentelemetry.InitOtel)
116121
mustInitCtx(ctx, git.InitFull)
117122
log.Info("Git version: %s (home: %s)", git.DefaultFeatures().VersionInfo(), git.HomeDir())
118123
if !git.DefaultFeatures().SupportHashSha256 {
@@ -183,6 +188,17 @@ func InitWebInstalled(ctx context.Context) {
183188
func NormalRoutes() *web.Router {
184189
_ = templates.HTMLRenderer()
185190
r := web.NewRouter()
191+
192+
mp := opentelemetry.GetMeterProvider(context.Background())
193+
194+
baseCfg := otelchimetric.NewBaseConfig("gitea", otelchimetric.WithMeterProvider(mp))
195+
r.Use(
196+
otelchi.Middleware("gitea", otelchi.WithChiRoutes(r.GetRouter())),
197+
otelchimetric.NewRequestDurationMillis(baseCfg),
198+
otelchimetric.NewRequestInFlight(baseCfg),
199+
otelchimetric.NewResponseSizeBytes(baseCfg),
200+
)
201+
186202
r.Use(common.ProtocolMiddlewares()...)
187203

188204
r.Mount("/", web_routers.Routes())

0 commit comments

Comments
 (0)