Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdoutlog: Add exporter #5172

Merged
merged 24 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exporters/stdout/stdoutlog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

var (
defaultWriter = os.Stdout
defaultPrettyPrint = false
defaultTimestamps = true
defaultWriter io.Writer = os.Stdout
defaultPrettyPrint = false
defaultTimestamps = true
)

// config contains options for the STDOUT exporter.
Expand Down
72 changes: 72 additions & 0 deletions exporters/stdout/stdoutlog/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog"

import (
"context"
"encoding/json"
"sync/atomic"

"go.opentelemetry.io/otel/sdk/log"
)

var _ log.Exporter = &Exporter{}

// Exporter writes JSON-encoded log records to an [io.Writer] ([os.Stdout] by default).
// Exporter must be created with [New].
type Exporter struct {
encoder atomic.Pointer[json.Encoder]
timestamps bool
}

// New creates an [Exporter].
func New(options ...Option) (*Exporter, error) {
cfg := newConfig(options)

enc := json.NewEncoder(cfg.Writer)
if cfg.PrettyPrint {
enc.SetIndent("", "\t")
}

e := Exporter{
timestamps: cfg.Timestamps,
}
e.encoder.Store(enc)

return &e, nil
}

// Export exports log records to writer.
func (e *Exporter) Export(ctx context.Context, records []log.Record) error {
enc := e.encoder.Load()
if enc == nil {
return nil
}

for _, record := range records {
// Honor context cancellation.
if err := ctx.Err(); err != nil {
return err
}

// Encode record, one by one.
recordJSON := e.newRecordJSON(record)
if err := enc.Encode(recordJSON); err != nil {
return err

Check warning on line 56 in exporters/stdout/stdoutlog/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/stdout/stdoutlog/exporter.go#L56

Added line #L56 was not covered by tests
}
}
return nil
}

// Shutdown shuts down the Exporter.
// Calls to Export will perform no operation after this is called.
func (e *Exporter) Shutdown(context.Context) error {
e.encoder.Store(nil)
return nil
}

// ForceFlush performs no action.
func (e *Exporter) ForceFlush(context.Context) error {
return nil
}
Loading