summaryrefslogtreecommitdiff
path: root/vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go
diff options
context:
space:
mode:
authorLibravatar Daenney <daenney@noreply.codeberg.org>2025-05-05 16:22:45 +0000
committerLibravatar tobi <kipvandenbos@noreply.codeberg.org>2025-05-05 16:22:45 +0000
commitecbdc4227ba49eca622812b7413aa877318fd7a0 (patch)
tree37f441534f0f7673f72b8b1fa83ddc12db8ec95c /vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go
parent[chore] Update goreleaser (#4133) (diff)
downloadgotosocial-ecbdc4227ba49eca622812b7413aa877318fd7a0.tar.xz
[chore] Simplify the OTEL setup (#4110)
# Description This simplifies our OTEL setup by: * Getting rid of some deprecated things. * Using `autoexport` and letting things get configured by the `OTEL_` environment variables. * Removing all the unnecessary config options. ## Checklist Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]` If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want). - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [ ] I/we have commented the added code, particularly in hard-to-understand areas. - [x] I/we have made any necessary changes to documentation. - [ ] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4110 Reviewed-by: tobi <kipvandenbos@noreply.codeberg.org> Co-authored-by: Daenney <daenney@noreply.codeberg.org> Co-committed-by: Daenney <daenney@noreply.codeberg.org>
Diffstat (limited to 'vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go')
-rw-r--r--vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go b/vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go
new file mode 100644
index 000000000..bdb915ba8
--- /dev/null
+++ b/vendor/go.opentelemetry.io/otel/exporters/stdout/stdouttrace/trace.go
@@ -0,0 +1,103 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
+
+import (
+ "context"
+ "encoding/json"
+ "sync"
+ "time"
+
+ "go.opentelemetry.io/otel/sdk/trace"
+ "go.opentelemetry.io/otel/sdk/trace/tracetest"
+)
+
+var zeroTime time.Time
+
+var _ trace.SpanExporter = &Exporter{}
+
+// New creates an Exporter with the passed options.
+func New(options ...Option) (*Exporter, error) {
+ cfg := newConfig(options...)
+
+ enc := json.NewEncoder(cfg.Writer)
+ if cfg.PrettyPrint {
+ enc.SetIndent("", "\t")
+ }
+
+ return &Exporter{
+ encoder: enc,
+ timestamps: cfg.Timestamps,
+ }, nil
+}
+
+// Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
+type Exporter struct {
+ encoder *json.Encoder
+ encoderMu sync.Mutex
+ timestamps bool
+
+ stoppedMu sync.RWMutex
+ stopped bool
+}
+
+// ExportSpans writes spans in json format to stdout.
+func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+ e.stoppedMu.RLock()
+ stopped := e.stopped
+ e.stoppedMu.RUnlock()
+ if stopped {
+ return nil
+ }
+
+ if len(spans) == 0 {
+ return nil
+ }
+
+ stubs := tracetest.SpanStubsFromReadOnlySpans(spans)
+
+ e.encoderMu.Lock()
+ defer e.encoderMu.Unlock()
+ for i := range stubs {
+ stub := &stubs[i]
+ // Remove timestamps
+ if !e.timestamps {
+ stub.StartTime = zeroTime
+ stub.EndTime = zeroTime
+ for j := range stub.Events {
+ ev := &stub.Events[j]
+ ev.Time = zeroTime
+ }
+ }
+
+ // Encode span stubs, one by one
+ if err := e.encoder.Encode(stub); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// Shutdown is called to stop the exporter, it performs no action.
+func (e *Exporter) Shutdown(ctx context.Context) error {
+ e.stoppedMu.Lock()
+ e.stopped = true
+ e.stoppedMu.Unlock()
+
+ return nil
+}
+
+// MarshalLog is the marshaling function used by the logging system to represent this Exporter.
+func (e *Exporter) MarshalLog() interface{} {
+ return struct {
+ Type string
+ WithTimestamps bool
+ }{
+ Type: "stdout",
+ WithTimestamps: e.timestamps,
+ }
+}