summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLibravatar Dominik Süß <dominik@suess.wtf>2025-02-06 12:14:37 +0100
committerLibravatar GitHub <noreply@github.com>2025-02-06 12:14:37 +0100
commitdd094e401282e135989f57c0ca3dee7dea3f5207 (patch)
tree74cb77830f621840273255a17565ced73b4fa997 /internal
parent[feature] Use `X-Robots-Tag` headers to instruct scrapers/crawlers (#3737) (diff)
downloadgotosocial-dd094e401282e135989f57c0ca3dee7dea3f5207.tar.xz
[chore] update otel libraries (#3740)
* chore: update otel dependencies * refactor: combine tracing & metrics in observability package * chore: update example tracing compose file
Diffstat (limited to 'internal')
-rw-r--r--internal/api/metrics/metrics.go2
-rw-r--r--internal/api/metrics/no_metrics.go2
-rw-r--r--internal/db/bundb/bundb.go12
-rw-r--r--internal/observability/bun.go (renamed from internal/tracing/no_trace.go)35
-rw-r--r--internal/observability/metrics.go (renamed from internal/metrics/metrics.go)21
-rw-r--r--internal/observability/no_otel.go (renamed from internal/metrics/no_metrics.go)26
-rw-r--r--internal/observability/tracing.go (renamed from internal/tracing/tracing.go)18
7 files changed, 51 insertions, 65 deletions
diff --git a/internal/api/metrics/metrics.go b/internal/api/metrics/metrics.go
index bbca861ef..5161a3b71 100644
--- a/internal/api/metrics/metrics.go
+++ b/internal/api/metrics/metrics.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build !nometrics
+//go:build !nootel
package metrics
diff --git a/internal/api/metrics/no_metrics.go b/internal/api/metrics/no_metrics.go
index ae7fc7ce7..5aa7b722d 100644
--- a/internal/api/metrics/no_metrics.go
+++ b/internal/api/metrics/no_metrics.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build nometrics
+//go:build nootel
package metrics
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go
index c9dd7866d..18fe5384c 100644
--- a/internal/db/bundb/bundb.go
+++ b/internal/db/bundb/bundb.go
@@ -41,9 +41,8 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
- "github.com/superseriousbusiness/gotosocial/internal/metrics"
+ "github.com/superseriousbusiness/gotosocial/internal/observability"
"github.com/superseriousbusiness/gotosocial/internal/state"
- "github.com/superseriousbusiness/gotosocial/internal/tracing"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/pgdialect"
@@ -324,11 +323,10 @@ func bunDB(sqldb *sql.DB, dialect func() schema.Dialect) *bun.DB {
// Add our SQL connection hooks.
db.AddQueryHook(queryHook{})
- if config.GetTracingEnabled() {
- db.AddQueryHook(tracing.InstrumentBun())
- }
- if config.GetMetricsEnabled() {
- db.AddQueryHook(metrics.InstrumentBun())
+ metricsEnabled := config.GetMetricsEnabled()
+ tracingEnabled := config.GetTracingEnabled()
+ if metricsEnabled || tracingEnabled {
+ db.AddQueryHook(observability.InstrumentBun(tracingEnabled, metricsEnabled))
}
// table registration is needed for many-to-many, see:
diff --git a/internal/tracing/no_trace.go b/internal/observability/bun.go
index 12f46ce53..4baf786e4 100644
--- a/internal/tracing/no_trace.go
+++ b/internal/observability/bun.go
@@ -15,29 +15,28 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build notracing
+//go:build !nootel
-package tracing
+package observability
import (
- "errors"
-
- "github.com/gin-gonic/gin"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/uptrace/bun"
+ "github.com/uptrace/bun/extra/bunotel"
+ metricnoop "go.opentelemetry.io/otel/metric/noop"
+ tracenoop "go.opentelemetry.io/otel/trace/noop"
)
-func Initialize() error {
- if config.GetTracingEnabled() {
- return errors.New("tracing was disabled at build time")
+func InstrumentBun(traces bool, metrics bool) bun.QueryHook {
+ opts := []bunotel.Option{
+ bunotel.WithFormattedQueries(true),
}
- return nil
-}
-
-func InstrumentGin() gin.HandlerFunc {
- return func(c *gin.Context) {}
-}
-
-func InstrumentBun() bun.QueryHook {
- return nil
+ if !traces {
+ opts = append(opts, bunotel.WithTracerProvider(tracenoop.NewTracerProvider()))
+ }
+ if !metrics {
+ opts = append(opts, bunotel.WithMeterProvider(metricnoop.NewMeterProvider()))
+ }
+ return bunotel.NewQueryHook(
+ opts...,
+ )
}
diff --git a/internal/metrics/metrics.go b/internal/observability/metrics.go
index e22a67dad..f2ff2ac0d 100644
--- a/internal/metrics/metrics.go
+++ b/internal/observability/metrics.go
@@ -15,24 +15,24 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build !nometrics
+//go:build !nootel
-package metrics
+package observability
import (
"context"
"errors"
- "github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
+
+ "github.com/gin-gonic/gin"
"github.com/technologize/otel-go-contrib/otelginmetrics"
- "github.com/uptrace/bun"
- "github.com/uptrace/bun/extra/bunotel"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
sdk "go.opentelemetry.io/otel/sdk/metric"
+ "go.opentelemetry.io/otel/sdk/metric/exemplar"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
)
@@ -41,7 +41,7 @@ const (
serviceName = "GoToSocial"
)
-func Initialize(db db.DB) error {
+func InitializeMetrics(db db.DB) error {
if !config.GetMetricsEnabled() {
return nil
}
@@ -66,6 +66,7 @@ func Initialize(db db.DB) error {
}
meterProvider := sdk.NewMeterProvider(
+ sdk.WithExemplarFilter(exemplar.AlwaysOffFilter),
sdk.WithResource(r),
sdk.WithReader(prometheusExporter),
)
@@ -127,12 +128,6 @@ func Initialize(db db.DB) error {
return nil
}
-func InstrumentGin() gin.HandlerFunc {
+func MetricsMiddleware() gin.HandlerFunc {
return otelginmetrics.Middleware(serviceName)
}
-
-func InstrumentBun() bun.QueryHook {
- return bunotel.NewQueryHook(
- bunotel.WithMeterProvider(otel.GetMeterProvider()),
- )
-}
diff --git a/internal/metrics/no_metrics.go b/internal/observability/no_otel.go
index 25e156307..122d7a897 100644
--- a/internal/metrics/no_metrics.go
+++ b/internal/observability/no_otel.go
@@ -15,30 +15,32 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build nometrics
+//go:build nootel
-package metrics
+package observability
import (
- "errors"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/gin-gonic/gin"
- "github.com/superseriousbusiness/gotosocial/internal/config"
- "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/uptrace/bun"
)
-func Initialize(db db.DB) error {
- if config.GetMetricsEnabled() {
- return errors.New("metrics was disabled at build time")
- }
+func InitializeMetrics(db db.DB) error {
+ return nil
+}
+func InitializeTracing() error {
+ return nil
+}
+
+func MetricsMiddleware() gin.HandlerFunc {
return nil
}
-func InstrumentGin() gin.HandlerFunc {
- return func(c *gin.Context) {}
+func TracingMiddleware() gin.HandlerFunc {
+ return nil
}
-func InstrumentBun() bun.QueryHook {
+func InstrumentBun(traces bool, metrics bool) bun.QueryHook {
return nil
}
diff --git a/internal/tracing/tracing.go b/internal/observability/tracing.go
index dd1e19be9..7827004a3 100644
--- a/internal/tracing/tracing.go
+++ b/internal/observability/tracing.go
@@ -15,9 +15,9 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//go:build !notracing
+//go:build !nootel
-package tracing
+package observability
import (
"context"
@@ -25,8 +25,6 @@ import (
"codeberg.org/gruf/go-kv"
"github.com/gin-gonic/gin"
- "github.com/uptrace/bun"
- "github.com/uptrace/bun/extra/bunotel"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
@@ -45,10 +43,10 @@ import (
const (
tracerKey = "gotosocial-server-tracer"
- tracerName = "github.com/superseriousbusiness/gotosocial/internal/tracing"
+ tracerName = "github.com/superseriousbusiness/gotosocial/internal/observability"
)
-func Initialize() error {
+func InitializeTracing() error {
if !config.GetTracingEnabled() {
return nil
}
@@ -119,7 +117,7 @@ func Initialize() error {
// InstrumentGin is a middleware injecting tracing information based on the
// otelgin implementation found at
// https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go
-func InstrumentGin() gin.HandlerFunc {
+func TracingMiddleware() gin.HandlerFunc {
provider := otel.GetTracerProvider()
tracer := provider.Tracer(
tracerName,
@@ -182,9 +180,3 @@ func InjectRequestID() gin.HandlerFunc {
}
}
}
-
-func InstrumentBun() bun.QueryHook {
- return bunotel.NewQueryHook(
- bunotel.WithFormattedQueries(true),
- )
-}