-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into record-dropped
- Loading branch information
Showing
7 changed files
with
493 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
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 | ||
} |
Oops, something went wrong.