diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 59b93d208..f838bd2eb 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -190,6 +190,15 @@ updates: schedule: interval: weekly day: sunday + - package-ecosystem: gomod + directory: /exporters/stdout/stdoutlog + labels: + - dependencies + - go + - Skip Changelog + schedule: + interval: weekly + day: sunday - package-ecosystem: gomod directory: /exporters/stdout/stdoutmetric labels: diff --git a/exporters/stdout/stdoutlog/README.md b/exporters/stdout/stdoutlog/README.md new file mode 100644 index 000000000..cd138deef --- /dev/null +++ b/exporters/stdout/stdoutlog/README.md @@ -0,0 +1,3 @@ +# STDOUT Log Exporter + +[![PkgGoDev](https://pkg.go.dev/badge/go.opentelemetry.io/otel/exporters/stdout/stdoutlog)](https://pkg.go.dev/go.opentelemetry.io/otel/exporters/stdout/stdoutlog) diff --git a/exporters/stdout/stdoutlog/config.go b/exporters/stdout/stdoutlog/config.go new file mode 100644 index 000000000..cd0976f86 --- /dev/null +++ b/exporters/stdout/stdoutlog/config.go @@ -0,0 +1,85 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" + +import ( + "io" + "os" +) + +var ( + defaultWriter = os.Stdout + defaultPrettyPrint = false + defaultTimestamps = true +) + +// config contains options for the STDOUT exporter. +type config struct { + // Writer is the destination. If not set, os.Stdout is used. + Writer io.Writer + + // PrettyPrint will encode the output into readable JSON. Default is + // false. + PrettyPrint bool + + // Timestamps specifies if timestamps should be printed. Default is + // true. + Timestamps bool +} + +// newConfig creates a validated Config configured with options. +func newConfig(options []Option) config { + cfg := config{ + Writer: defaultWriter, + PrettyPrint: defaultPrettyPrint, + Timestamps: defaultTimestamps, + } + for _, opt := range options { + cfg = opt.apply(cfg) + } + return cfg +} + +// Option sets the configuration value for an Exporter. +type Option interface { + apply(config) config +} + +// WithWriter sets the export stream destination. +func WithWriter(w io.Writer) Option { + return writerOption{w} +} + +type writerOption struct { + W io.Writer +} + +func (o writerOption) apply(cfg config) config { + cfg.Writer = o.W + return cfg +} + +// WithPrettyPrint prettifies the emitted output. +func WithPrettyPrint() Option { + return prettyPrintOption(true) +} + +type prettyPrintOption bool + +func (o prettyPrintOption) apply(cfg config) config { + cfg.PrettyPrint = bool(o) + return cfg +} + +// WithoutTimestamps sets the export stream to not include timestamps. +func WithoutTimestamps() Option { + return timestampsOption(false) +} + +type timestampsOption bool + +func (o timestampsOption) apply(cfg config) config { + cfg.Timestamps = bool(o) + return cfg +} diff --git a/exporters/stdout/stdoutlog/config_test.go b/exporters/stdout/stdoutlog/config_test.go new file mode 100644 index 000000000..7f60883b4 --- /dev/null +++ b/exporters/stdout/stdoutlog/config_test.go @@ -0,0 +1,62 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewConfig(t *testing.T) { + testCases := []struct { + name string + options []Option + expected config + }{ + { + name: "default", + expected: config{ + Writer: os.Stdout, + PrettyPrint: false, + Timestamps: true, + }, + }, + { + name: "WithWriter", + options: []Option{WithWriter(os.Stderr)}, + expected: config{ + Writer: os.Stderr, + PrettyPrint: false, + Timestamps: true, + }, + }, + { + name: "WithPrettyPrint", + options: []Option{WithPrettyPrint()}, + expected: config{ + Writer: os.Stdout, + PrettyPrint: true, + Timestamps: true, + }, + }, + { + name: "WithoutTimestamps", + options: []Option{WithoutTimestamps()}, + expected: config{ + Writer: os.Stdout, + PrettyPrint: false, + Timestamps: false, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + cfg := newConfig(tc.options) + assert.Equal(t, tc.expected, cfg) + }) + } +} diff --git a/exporters/stdout/stdoutlog/doc.go b/exporters/stdout/stdoutlog/doc.go new file mode 100644 index 000000000..d400ab8c5 --- /dev/null +++ b/exporters/stdout/stdoutlog/doc.go @@ -0,0 +1,12 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package stdoutlog provides an exporter for OpenTelemetry log +// telemetry. +// +// The exporter is intended to be used for testing and debugging, it is not +// meant for production use. Additionally, it does not provide an interchange +// format for OpenTelemetry that is supported with any stability or +// compatibility guarantees. If these are needed features, please use the OTLP +// exporter instead. +package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" diff --git a/exporters/stdout/stdoutlog/go.mod b/exporters/stdout/stdoutlog/go.mod new file mode 100644 index 000000000..bf090f62a --- /dev/null +++ b/exporters/stdout/stdoutlog/go.mod @@ -0,0 +1,11 @@ +module go.opentelemetry.io/otel/exporters/stdout/stdoutlog + +go 1.21 + +require github.com/stretchr/testify v1.9.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/exporters/stdout/stdoutlog/go.sum b/exporters/stdout/stdoutlog/go.sum new file mode 100644 index 000000000..60ce688a0 --- /dev/null +++ b/exporters/stdout/stdoutlog/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/versions.yaml b/versions.yaml index 265ce8ad7..ea6ba3321 100644 --- a/versions.yaml +++ b/versions.yaml @@ -45,3 +45,4 @@ excluded-modules: - go.opentelemetry.io/otel/internal/tools - go.opentelemetry.io/otel/sdk/log - go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp + - go.opentelemetry.io/otel/exporters/stdout/stdoutlog