-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Stdoutlog exporter metrics #7351
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
base: main
Are you sure you want to change the base?
Changes from all commits
6aa74c9
9242601
36b381a
f8b91e1
a35fc1b
8ab2b0b
55909d0
253afc2
26a279b
670c3ab
6eac6ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,16 +8,30 @@ import ( | |||||||||||||||||||||||||||||||||||||
"encoding/json" | ||||||||||||||||||||||||||||||||||||||
"sync/atomic" | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog/internal" | ||||||||||||||||||||||||||||||||||||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog/internal/counter" | ||||||||||||||||||||||||||||||||||||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog/internal/observ" | ||||||||||||||||||||||||||||||||||||||
"go.opentelemetry.io/otel/sdk/log" | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// otelComponentType is a name identifying the type of the OpenTelemetry component. | ||||||||||||||||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||||||||||||||||
otelComponentType = "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Version is the current version of this instrumentation. | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// This matches the version of the exporter. | ||||||||||||||||||||||||||||||||||||||
Version = internal.Version | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds to the public API of the package, it should not be exported. It does not seem needed either. Using just |
||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||
encoder atomic.Pointer[json.Encoder] | ||||||||||||||||||||||||||||||||||||||
timestamps bool | ||||||||||||||||||||||||||||||||||||||
instrumentation *observ.Instrumentation | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// New creates an [Exporter]. | ||||||||||||||||||||||||||||||||||||||
|
@@ -34,29 +48,50 @@ func New(options ...Option) (*Exporter, error) { | |||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
e.encoder.Store(enc) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
exporterID := counter.NextExporterID() | ||||||||||||||||||||||||||||||||||||||
inst, err := observ.NewInstrumentation(otelComponentType, exporterID) | ||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
e.instrumentation = inst | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
return &e, nil | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Export exports log records to writer. | ||||||||||||||||||||||||||||||||||||||
func (e *Exporter) Export(ctx context.Context, records []log.Record) error { | ||||||||||||||||||||||||||||||||||||||
if inst := e.instrumentation; inst != nil { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just shorten the field name to |
||||||||||||||||||||||||||||||||||||||
done := inst.ExportLogs(ctx, len(records)) | ||||||||||||||||||||||||||||||||||||||
exported, err := e.exportRecords(ctx, records) | ||||||||||||||||||||||||||||||||||||||
done(exported, err) | ||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
_, err := e.exportRecords(ctx, records) | ||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+63
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be needed to be called in two different code paths. Use
Suggested change
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func (e *Exporter) exportRecords(ctx context.Context, records []log.Record) (int64, error) { | ||||||||||||||||||||||||||||||||||||||
enc := e.encoder.Load() | ||||||||||||||||||||||||||||||||||||||
if enc == nil { | ||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||
return 0, nil | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
var exported int64 | ||||||||||||||||||||||||||||||||||||||
for _, record := range records { | ||||||||||||||||||||||||||||||||||||||
// Honor context cancellation. | ||||||||||||||||||||||||||||||||||||||
if err := ctx.Err(); err != nil { | ||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||
return exported, err | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Encode record, one by one. | ||||||||||||||||||||||||||||||||||||||
recordJSON := e.newRecordJSON(record) | ||||||||||||||||||||||||||||||||||||||
if err := enc.Encode(recordJSON); err != nil { | ||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||
return exported, err | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
exported++ | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
return exported, nil | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Shutdown shuts down the Exporter. | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be defined in
observ
not here. It is only used there and passed from here. This is an unneeded coupling.