@@ -23,6 +23,7 @@ import (
2323 "slices"
2424 "strings"
2525 "sync"
26+ "sync/atomic"
2627 "testing"
2728 "time"
2829
@@ -695,12 +696,9 @@ func crashRecords(t *testing.T) *[]stdoutRecord {
695696 return logRecords (t , actorevent .Crashed .Body )
696697}
697698
698- // stdoutRecord is one captured record from the stdout copy. It keeps the level
699- // and the time, not just the attributes, so a test can hold those against the
700- // OTLP copy. The message is whatever logRecords filtered on.
699+ // stdoutRecord is the attributes of one captured stdout record. The message is
700+ // whatever logRecords filtered on.
701701type stdoutRecord struct {
702- level slog.Level
703- time time.Time
704702 attrs map [string ]string
705703}
706704
@@ -714,7 +712,7 @@ func logRecords(t *testing.T, msg string) *[]stdoutRecord {
714712 if r .Message != msg {
715713 return
716714 }
717- rec := stdoutRecord {level : r . Level , time : r . Time , attrs : map [string ]string {}}
715+ rec := stdoutRecord {attrs : map [string ]string {}}
718716 r .Attrs (func (a slog.Attr ) bool {
719717 rec .attrs [a .Key ] = a .Value .String ()
720718 return true
@@ -725,33 +723,42 @@ func logRecords(t *testing.T, msg string) *[]stdoutRecord {
725723 return & records
726724}
727725
728- // otlpEvent is one captured record from the OTLP copy of a log record.
726+ // otlpEvent is one captured OTLP log record.
729727type otlpEvent struct {
730- name string
731- body string
732- severity otellog.Severity
733- timestamp time.Time
734- attrs map [string ]string
728+ name string
729+ body string
730+ severity otellog.Severity
731+ attrs map [string ]string
735732}
736733
737734var (
738735 otlpSinkOnce sync.Once
739736 otlpSinkMu sync.Mutex
740737 otlpSink []otlpEvent
738+ // otlpOn is whether the one global provider takes records. Off, the
739+ // actor events go to stdout, as when no logs exporter is configured.
740+ otlpOn atomic.Bool
741741)
742742
743+ // switchProcessor takes records only while otlpOn is set, so one global
744+ // provider can stand for the logs exporter being on or off.
745+ type switchProcessor struct { sdklog.Processor }
746+
747+ func (p switchProcessor ) Enabled (ctx context.Context , param sdklog.EnabledParameters ) bool {
748+ return otlpOn .Load () && p .Processor .Enabled (ctx , param )
749+ }
750+
743751type otlpSinkExporter struct {}
744752
745753func (otlpSinkExporter ) Export (_ context.Context , records []sdklog.Record ) error {
746754 otlpSinkMu .Lock ()
747755 defer otlpSinkMu .Unlock ()
748756 for _ , r := range records {
749757 e := otlpEvent {
750- name : r .EventName (),
751- body : r .Body ().String (),
752- severity : r .Severity (),
753- timestamp : r .Timestamp (),
754- attrs : map [string ]string {},
758+ name : r .EventName (),
759+ body : r .Body ().String (),
760+ severity : r .Severity (),
761+ attrs : map [string ]string {},
755762 }
756763 r .WalkAttributes (func (kv attribute.KeyValue ) bool {
757764 e .attrs [string (kv .Key )] = kv .Value .String ()
@@ -765,16 +772,17 @@ func (otlpSinkExporter) Export(_ context.Context, records []sdklog.Record) error
765772func (otlpSinkExporter ) Shutdown (context.Context ) error { return nil }
766773func (otlpSinkExporter ) ForceFlush (context.Context ) error { return nil }
767774
768- // otlpEvents captures the events emitted while a test runs, so a test can assert
769- // the OTLP copy beside the stdout one. The global logger provider only ever
770- // delegates once, so one provider serves the whole binary and each call clears
771- // the sink. Like logRecords, this makes the caller non-parallel.
775+ // otlpEvents turns the logs exporter on for the rest of the test and captures
776+ // the events emitted, which then go to OTLP instead of stdout. The global logger
777+ // provider only ever delegates once, so one provider serves the whole binary,
778+ // switched off between tests, and each call clears the sink. Like logRecords,
779+ // this makes the caller non-parallel.
772780func otlpEvents (t * testing.T ) func () []otlpEvent {
773781 t .Helper ()
774782
775783 otlpSinkOnce .Do (func () {
776784 global .SetLoggerProvider (sdklog .NewLoggerProvider (
777- sdklog .WithProcessor (sdklog .NewSimpleProcessor (otlpSinkExporter {}))))
785+ sdklog .WithProcessor (switchProcessor { sdklog .NewSimpleProcessor (otlpSinkExporter {})} )))
778786 })
779787
780788 clear := func () {
@@ -783,7 +791,11 @@ func otlpEvents(t *testing.T) func() []otlpEvent {
783791 otlpSink = nil
784792 }
785793 clear ()
786- t .Cleanup (clear )
794+ otlpOn .Store (true )
795+ t .Cleanup (func () {
796+ otlpOn .Store (false )
797+ clear ()
798+ })
787799
788800 return func () []otlpEvent {
789801 otlpSinkMu .Lock ()
@@ -802,25 +814,20 @@ func (f slogHandlerFunc) Handle(_ context.Context, r slog.Record) error {
802814func (f slogHandlerFunc ) WithAttrs ([]slog.Attr ) slog.Handler { return f }
803815func (f slogHandlerFunc ) WithGroup (string ) slog.Handler { return f }
804816
805- // assertCopiesAgree checks the fields that no longer live at a call site. Both
806- // copies take their severity and body from ev, so neither can hold its own. A
807- // stdout body that drifted fails earlier, when logRecords matches nothing.
808- func assertCopiesAgree (t * testing.T , stdout stdoutRecord , otlp otlpEvent , ev actorevent.Event ) {
817+ // assertEvent checks the fields that no longer live at a call site: the event
818+ // takes its severity and body from ev, so it cannot hold its own.
819+ func assertEvent (t * testing.T , otlp otlpEvent , ev actorevent.Event ) {
809820 t .Helper ()
810821
811- if stdout . level != ev .Level () {
812- t .Errorf ("stdout level = %v , want %v " , stdout . level , ev .Level () )
822+ if otlp . name != ev .Name {
823+ t .Errorf ("event name = %q , want %q " , otlp . name , ev .Name )
813824 }
814825 if otlp .severity != ev .Severity {
815826 t .Errorf ("OTLP severity = %v, want %v" , otlp .severity , ev .Severity )
816827 }
817828 if otlp .body != ev .Body {
818829 t .Errorf ("OTLP body = %q, want %q" , otlp .body , ev .Body )
819830 }
820- // One time.Now() serves both, so a consumer can join them on it.
821- if ! stdout .time .Equal (otlp .timestamp ) {
822- t .Errorf ("timestamps differ: stdout %v, OTLP %v" , stdout .time , otlp .timestamp )
823- }
824831}
825832
826833// The crash record is the only signal carrying actor identity, so it must fire
@@ -848,11 +855,17 @@ func TestCrashActor_RecordAndCounterAgree(t *testing.T) {
848855 if err := crashActor (ctx , st , actorRef , ateattr .OperationResume , "test crash" ); err != nil {
849856 t .Fatalf ("crashActor: %v" , err )
850857 }
851- if len (* records ) != 1 {
852- t .Fatalf ("got %d crash records, want 1" , len (* records ))
858+ // The logs exporter is on, so the record goes to OTLP only.
859+ if len (* records ) != 0 {
860+ t .Errorf ("got %d stdout crash records with the exporter on, want 0" , len (* records ))
861+ }
862+ gotEvents := events ()
863+ if len (gotEvents ) != 1 {
864+ t .Fatalf ("got %d crash events, want 1: %v" , len (gotEvents ), gotEvents )
853865 }
866+ assertEvent (t , gotEvents [0 ], actorevent .Crashed )
854867
855- got := ( * records ) [0 ].attrs
868+ got := gotEvents [0 ].attrs
856869 stored , err := st .GetActor (ctx , actorRef )
857870 if err != nil {
858871 t .Fatalf ("GetActor: %v" , err )
@@ -873,27 +886,10 @@ func TestCrashActor_RecordAndCounterAgree(t *testing.T) {
873886 t .Error ("crash record carries no ate.actor.uid; it cannot survive a name reuse" )
874887 }
875888
876- // The OTLP copy is the same record under an event name. One call writes both,
877- // so anything either copy holds alone is a bug in actorevent.Log.
878- gotEvents := events ()
879- if len (gotEvents ) != 1 {
880- t .Fatalf ("got %d crash events, want 1: %v" , len (gotEvents ), gotEvents )
881- }
882- if gotEvents [0 ].name != actorevent .Crashed .Name {
883- t .Errorf ("event name = %q, want %q" , gotEvents [0 ].name , actorevent .Crashed .Name )
884- }
885- if ! maps .Equal (gotEvents [0 ].attrs , got ) {
886- t .Errorf ("crash event attributes = %v, want the stdout record's %v" , gotEvents [0 ].attrs , got )
887- }
888- assertCopiesAgree (t , (* records )[0 ], gotEvents [0 ], actorevent .Crashed )
889-
890889 // Re-crashing an already-crashed actor must move neither signal.
891890 if err := crashActor (ctx , st , actorRef , ateattr .OperationResume , "test crash" ); err != nil {
892891 t .Fatalf ("second crashActor: %v" , err )
893892 }
894- if len (* records ) != 1 {
895- t .Errorf ("got %d crash records after re-crashing, want 1" , len (* records ))
896- }
897893 if gotEvents := events (); len (gotEvents ) != 1 {
898894 t .Errorf ("got %d crash events after re-crashing, want 1" , len (gotEvents ))
899895 }
0 commit comments