mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-28 21:09:17 +02:00
Use reflect to construct a Record in logtest
(#5275)
* Use reflect to construct Record * Fix merge * Fix merge
This commit is contained in:
parent
e0ed6a3028
commit
36bee9356a
@ -109,7 +109,7 @@ var (
|
|||||||
TraceID: trace.TraceID(traceIDA),
|
TraceID: trace.TraceID(traceIDA),
|
||||||
SpanID: trace.SpanID(spanIDA),
|
SpanID: trace.SpanID(spanIDA),
|
||||||
TraceFlags: trace.TraceFlags(flagsA),
|
TraceFlags: trace.TraceFlags(flagsA),
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
Resource: res,
|
Resource: res,
|
||||||
}.NewRecord())
|
}.NewRecord())
|
||||||
|
|
||||||
@ -123,7 +123,7 @@ var (
|
|||||||
TraceID: trace.TraceID(traceIDA),
|
TraceID: trace.TraceID(traceIDA),
|
||||||
SpanID: trace.SpanID(spanIDA),
|
SpanID: trace.SpanID(spanIDA),
|
||||||
TraceFlags: trace.TraceFlags(flagsA),
|
TraceFlags: trace.TraceFlags(flagsA),
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
Resource: res,
|
Resource: res,
|
||||||
}.NewRecord())
|
}.NewRecord())
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ var (
|
|||||||
TraceID: trace.TraceID(traceIDB),
|
TraceID: trace.TraceID(traceIDB),
|
||||||
SpanID: trace.SpanID(spanIDB),
|
SpanID: trace.SpanID(spanIDB),
|
||||||
TraceFlags: trace.TraceFlags(flagsB),
|
TraceFlags: trace.TraceFlags(flagsB),
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
Resource: res,
|
Resource: res,
|
||||||
}.NewRecord())
|
}.NewRecord())
|
||||||
|
|
||||||
@ -151,7 +151,7 @@ var (
|
|||||||
TraceID: trace.TraceID(traceIDB),
|
TraceID: trace.TraceID(traceIDB),
|
||||||
SpanID: trace.SpanID(spanIDB),
|
SpanID: trace.SpanID(spanIDB),
|
||||||
TraceFlags: trace.TraceFlags(flagsB),
|
TraceFlags: trace.TraceFlags(flagsB),
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
Resource: res,
|
Resource: res,
|
||||||
}.NewRecord())
|
}.NewRecord())
|
||||||
|
|
||||||
|
@ -294,7 +294,7 @@ func getRecord(now time.Time) sdklog.Record {
|
|||||||
"https://example.com/custom-resource-schema",
|
"https://example.com/custom-resource-schema",
|
||||||
attribute.String("foo", "bar"),
|
attribute.String("foo", "bar"),
|
||||||
),
|
),
|
||||||
InstrumentationScope: instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"},
|
InstrumentationScope: &instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"},
|
||||||
DroppedAttributes: 10,
|
DroppedAttributes: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ import (
|
|||||||
func ExampleRecordFactory() {
|
func ExampleRecordFactory() {
|
||||||
exp := exporter{os.Stdout}
|
exp := exporter{os.Stdout}
|
||||||
rf := logtest.RecordFactory{
|
rf := logtest.RecordFactory{
|
||||||
InstrumentationScope: instrumentation.Scope{Name: "myapp"},
|
InstrumentationScope: &instrumentation.Scope{Name: "myapp"},
|
||||||
}
|
}
|
||||||
|
|
||||||
rf.Body = logapi.StringValue("foo")
|
rf.Body = logapi.StringValue("foo")
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest"
|
package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"reflect"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"go.opentelemetry.io/otel/log"
|
"go.opentelemetry.io/otel/log"
|
||||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||||
@ -33,75 +33,44 @@ type RecordFactory struct {
|
|||||||
TraceFlags trace.TraceFlags
|
TraceFlags trace.TraceFlags
|
||||||
|
|
||||||
Resource *resource.Resource
|
Resource *resource.Resource
|
||||||
InstrumentationScope instrumentation.Scope
|
InstrumentationScope *instrumentation.Scope
|
||||||
|
|
||||||
DroppedAttributes int
|
DroppedAttributes int
|
||||||
|
AttributeValueLengthLimit int
|
||||||
|
AttributeCountLimit int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRecord returns a log record.
|
// NewRecord returns a [sdklog.Record] configured from the values of f.
|
||||||
func (b RecordFactory) NewRecord() sdklog.Record {
|
func (f RecordFactory) NewRecord() sdklog.Record {
|
||||||
var record sdklog.Record
|
// r needs to be addressable for set() below.
|
||||||
p := processor(func(r sdklog.Record) {
|
r := new(sdklog.Record)
|
||||||
r.SetTimestamp(b.Timestamp)
|
|
||||||
r.SetObservedTimestamp(b.ObservedTimestamp)
|
|
||||||
r.SetSeverity(b.Severity)
|
|
||||||
r.SetSeverityText(b.SeverityText)
|
|
||||||
r.SetBody(b.Body)
|
|
||||||
r.SetAttributes(slices.Clone(b.Attributes)...)
|
|
||||||
|
|
||||||
// Generate dropped attributes.
|
// Set to unlimited so attributes are set exactly.
|
||||||
for i := 0; i < b.DroppedAttributes; i++ {
|
set(r, "attributeCountLimit", -1)
|
||||||
r.AddAttributes(log.KeyValue{})
|
set(r, "attributeValueLengthLimit", -1)
|
||||||
}
|
|
||||||
|
|
||||||
r.SetTraceID(b.TraceID)
|
r.SetTimestamp(f.Timestamp)
|
||||||
r.SetSpanID(b.SpanID)
|
r.SetObservedTimestamp(f.ObservedTimestamp)
|
||||||
r.SetTraceFlags(b.TraceFlags)
|
r.SetSeverity(f.Severity)
|
||||||
|
r.SetSeverityText(f.SeverityText)
|
||||||
|
r.SetBody(f.Body)
|
||||||
|
r.SetAttributes(f.Attributes...)
|
||||||
|
r.SetTraceID(f.TraceID)
|
||||||
|
r.SetSpanID(f.SpanID)
|
||||||
|
r.SetTraceFlags(f.TraceFlags)
|
||||||
|
|
||||||
record = r
|
set(r, "resource", f.Resource)
|
||||||
})
|
set(r, "scope", f.InstrumentationScope)
|
||||||
|
set(r, "dropped", f.DroppedAttributes)
|
||||||
|
set(r, "attributeCountLimit", f.AttributeCountLimit)
|
||||||
|
set(r, "attributeValueLengthLimit", f.AttributeValueLengthLimit)
|
||||||
|
|
||||||
attributeCountLimit := -1
|
return *r
|
||||||
if b.DroppedAttributes > 0 {
|
|
||||||
// Make sure that we can generate dropped attributes.
|
|
||||||
attributeCountLimit = len(b.Attributes)
|
|
||||||
}
|
|
||||||
|
|
||||||
res := b.Resource
|
|
||||||
if res == nil {
|
|
||||||
res = resource.Empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
provider := sdklog.NewLoggerProvider(
|
|
||||||
sdklog.WithResource(res),
|
|
||||||
sdklog.WithAttributeCountLimit(attributeCountLimit),
|
|
||||||
sdklog.WithAttributeValueLengthLimit(-1),
|
|
||||||
sdklog.WithProcessor(p),
|
|
||||||
)
|
|
||||||
|
|
||||||
l := provider.Logger(b.InstrumentationScope.Name,
|
|
||||||
log.WithInstrumentationVersion(b.InstrumentationScope.Version),
|
|
||||||
log.WithSchemaURL(b.InstrumentationScope.SchemaURL),
|
|
||||||
)
|
|
||||||
l.Emit(context.Background(), log.Record{}) // This executes the processor function.
|
|
||||||
return record
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type processor func(r sdklog.Record)
|
func set(r *sdklog.Record, name string, value any) {
|
||||||
|
rVal := reflect.ValueOf(r).Elem()
|
||||||
func (p processor) OnEmit(ctx context.Context, r sdklog.Record) error {
|
rf := rVal.FieldByName(name)
|
||||||
p(r)
|
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
|
||||||
return nil
|
rf.Set(reflect.ValueOf(value))
|
||||||
}
|
|
||||||
|
|
||||||
func (processor) Enabled(context.Context, sdklog.Record) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (processor) Shutdown(ctx context.Context) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (processor) ForceFlush(context.Context) error {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,10 @@ import (
|
|||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestRecordFactoryEmpty(t *testing.T) {
|
||||||
|
assert.Equal(t, sdklog.Record{}, RecordFactory{}.NewRecord())
|
||||||
|
}
|
||||||
|
|
||||||
func TestRecordFactory(t *testing.T) {
|
func TestRecordFactory(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
observed := now.Add(time.Second)
|
observed := now.Add(time.Second)
|
||||||
@ -49,7 +53,7 @@ func TestRecordFactory(t *testing.T) {
|
|||||||
SpanID: spanID,
|
SpanID: spanID,
|
||||||
TraceFlags: traceFlags,
|
TraceFlags: traceFlags,
|
||||||
DroppedAttributes: dropped,
|
DroppedAttributes: dropped,
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
Resource: r,
|
Resource: r,
|
||||||
}.NewRecord()
|
}.NewRecord()
|
||||||
|
|
||||||
@ -82,7 +86,7 @@ func TestRecordFactoryMultiple(t *testing.T) {
|
|||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
Attributes: attrs,
|
Attributes: attrs,
|
||||||
DroppedAttributes: 1,
|
DroppedAttributes: 1,
|
||||||
InstrumentationScope: scope,
|
InstrumentationScope: &scope,
|
||||||
}
|
}
|
||||||
|
|
||||||
record1 := f.NewRecord()
|
record1 := f.NewRecord()
|
||||||
|
Loading…
Reference in New Issue
Block a user