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 20 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
81 changes: 81 additions & 0 deletions exporters/stdout/stdoutlog/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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

running atomic.Bool
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

// 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")
}

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

return &e, nil
}

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

enc := e.encoder.Load()
if enc == nil {
return nil

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

View check run for this annotation

Codecov / codecov/patch

exporters/stdout/stdoutlog/exporter.go#L51

Added line #L51 was not covered by tests
}

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 63 in exporters/stdout/stdoutlog/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/stdout/stdoutlog/exporter.go#L63

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

// Shutdown stops the exporter.
pellared marked this conversation as resolved.
Show resolved Hide resolved
func (e *Exporter) Shutdown(context.Context) error {
e.running.Store(false)
// Free the encoder resources.
e.encoder.Store(nil)

return nil
}

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