diff options
| author | 2025-05-05 16:22:45 +0000 | |
|---|---|---|
| committer | 2025-05-05 16:22:45 +0000 | |
| commit | ecbdc4227ba49eca622812b7413aa877318fd7a0 (patch) | |
| tree | 37f441534f0f7673f72b8b1fa83ddc12db8ec95c /vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go | |
| parent | [chore] Update goreleaser (#4133) (diff) | |
| download | gotosocial-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/contrib/exporters/autoexport/logs.go')
| -rw-r--r-- | vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go b/vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go new file mode 100644 index 000000000..dd12d0332 --- /dev/null +++ b/vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go @@ -0,0 +1,93 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport" + +import ( + "context" + "os" + + "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" + "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp" + "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" + "go.opentelemetry.io/otel/sdk/log" +) + +const otelExporterOTLPLogsProtoEnvKey = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL" + +// LogOption applies an autoexport configuration option. +type LogOption = option[log.Exporter] + +var logsSignal = newSignal[log.Exporter]("OTEL_LOGS_EXPORTER") + +// WithFallbackLogExporter sets the fallback exporter to use when no exporter +// is configured through the OTEL_LOGS_EXPORTER environment variable. +func WithFallbackLogExporter(logExporterFactory func(ctx context.Context) (log.Exporter, error)) LogOption { + return withFallbackFactory[log.Exporter](logExporterFactory) +} + +// NewLogExporter returns a configured [go.opentelemetry.io/otel/sdk/log.Exporter] +// defined using the environment variables described below. +// +// OTEL_LOGS_EXPORTER defines the logs exporter; supported values: +// - "none" - "no operation" exporter +// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlplog] +// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutlog] +// +// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol; +// supported values: +// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection; +// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp] +// - "grpc" - gRPC with protobuf-encoded data over HTTP/2 connection; +// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc] +// +// OTEL_EXPORTER_OTLP_LOGS_PROTOCOL defines OTLP exporter's transport protocol for the logs signal; +// supported values are the same as OTEL_EXPORTER_OTLP_PROTOCOL. +// +// An error is returned if an environment value is set to an unhandled value. +// +// Use [RegisterLogExporter] to handle more values of OTEL_LOGS_EXPORTER. +// +// Use [WithFallbackLogExporter] option to change the returned exporter +// when OTEL_LOGS_EXPORTER is unset or empty. +// +// Use [IsNoneLogExporter] to check if the returned exporter is a "no operation" exporter. +func NewLogExporter(ctx context.Context, opts ...LogOption) (log.Exporter, error) { + return logsSignal.create(ctx, opts...) +} + +// RegisterLogExporter sets the log.Exporter factory to be used when the +// OTEL_LOGS_EXPORTER environment variable contains the exporter name. +// This will panic if name has already been registered. +func RegisterLogExporter(name string, factory func(context.Context) (log.Exporter, error)) { + must(logsSignal.registry.store(name, factory)) +} + +func init() { + RegisterLogExporter("otlp", func(ctx context.Context) (log.Exporter, error) { + proto := os.Getenv(otelExporterOTLPLogsProtoEnvKey) + if proto == "" { + proto = os.Getenv(otelExporterOTLPProtoEnvKey) + } + + // Fallback to default, http/protobuf. + if proto == "" { + proto = "http/protobuf" + } + + switch proto { + case "grpc": + return otlploggrpc.New(ctx) + case "http/protobuf": + return otlploghttp.New(ctx) + default: + return nil, errInvalidOTLPProtocol + } + }) + RegisterLogExporter("console", func(ctx context.Context) (log.Exporter, error) { + return stdoutlog.New() + }) + RegisterLogExporter("none", func(ctx context.Context) (log.Exporter, error) { + return noopLogExporter{}, nil + }) +} |
