@@ -792,6 +792,82 @@ public void SegmentEndWithExceptionCapturesErrorAttributes()
792
792
SpanAssertions . HasAttributes ( expectedSpanErrorAttributes , AttributeClassification . AgentAttributes , spanWithError ) ;
793
793
}
794
794
795
+ [ Test ]
796
+ public void SegmentEnd_StackExchangeRedis_EndCreatesSegment ( )
797
+ {
798
+ var tx = _agent . CreateTransaction (
799
+ isWeb : false ,
800
+ category : "testing" ,
801
+ transactionDisplayName : "test" ,
802
+ doNotTrackAsUnitOfWork : true ) ;
803
+ var segment = _agent . StartStackExchangeRedisDatastoreRequestSegmentOrThrow ( "set" , DatastoreVendor . Redis , TimeSpan . Zero , TimeSpan . FromMilliseconds ( 1 ) ) ;
804
+ segment . EndStackExchangeRedis ( ) ;
805
+ tx . End ( ) ;
806
+
807
+ _compositeTestAgent . Harvest ( ) ;
808
+
809
+ var spanEvents = _compositeTestAgent . SpanEvents . ToArray ( ) ;
810
+ Assert . AreEqual ( 2 , spanEvents . Length ) ;
811
+
812
+ var expectedSpanErrorAttributes = new List < ExpectedAttribute >
813
+ {
814
+ new ExpectedAttribute { Key = "name" , Value = "Datastore/operation/Redis/set" } ,
815
+ new ExpectedAttribute { Key = "category" , Value = "datastore" } ,
816
+ } ;
817
+
818
+ var datastoreSpan = spanEvents [ 1 ] ;
819
+ SpanAssertions . HasAttributes ( expectedSpanErrorAttributes , AttributeClassification . Intrinsics , datastoreSpan ) ;
820
+ }
821
+
822
+ [ Test ]
823
+ public void SegmentEnd_StackExchangeRedis_SessionCacheIsNull_HarvestNotCalled ( )
824
+ {
825
+ _agent . StackExchangeRedisCache = null ;
826
+ var tx = _agent . CreateTransaction (
827
+ isWeb : false ,
828
+ category : "testing" ,
829
+ transactionDisplayName : "test" ,
830
+ doNotTrackAsUnitOfWork : true ) ;
831
+ var segment = _agent . StartCustomSegmentOrThrow ( "parentSegment" ) ;
832
+
833
+ segment . End ( ) ;
834
+ tx . End ( ) ;
835
+
836
+ _compositeTestAgent . Harvest ( ) ;
837
+
838
+ var spanEvents = _compositeTestAgent . SpanEvents . ToArray ( ) ;
839
+ Assert . AreEqual ( 2 , spanEvents . Length ) ;
840
+ }
841
+
842
+ [ Test ]
843
+ public void SegmentEnd_StackExchangeRedis_SessionCacheNotNull_HarvestCalled ( )
844
+ {
845
+ _agent . StackExchangeRedisCache = new StackExchangeRedisCacheMock ( _agent ) ;
846
+ var tx = _agent . CreateTransaction (
847
+ isWeb : false ,
848
+ category : "testing" ,
849
+ transactionDisplayName : "test" ,
850
+ doNotTrackAsUnitOfWork : true ) ;
851
+ var segment = _agent . StartCustomSegmentOrThrow ( "parentSegment" ) ;
852
+
853
+ segment . End ( ) ;
854
+ tx . End ( ) ;
855
+
856
+ _compositeTestAgent . Harvest ( ) ;
857
+
858
+ var spanEvents = _compositeTestAgent . SpanEvents . ToArray ( ) ;
859
+ Assert . AreEqual ( 3 , spanEvents . Length ) ;
860
+
861
+ var expectedSpanErrorAttributes = new List < ExpectedAttribute >
862
+ {
863
+ new ExpectedAttribute { Key = "name" , Value = "Datastore/operation/Redis/set" } ,
864
+ new ExpectedAttribute { Key = "category" , Value = "datastore" } ,
865
+ } ;
866
+
867
+ var datastoreSpan = spanEvents [ 2 ] ;
868
+ SpanAssertions . HasAttributes ( expectedSpanErrorAttributes , AttributeClassification . Intrinsics , datastoreSpan ) ;
869
+ }
870
+
795
871
#region Helper methods
796
872
797
873
private static TransactionTraceSegment GetFirstSegmentOrThrow ( )
@@ -810,4 +886,26 @@ private static TransactionTraceSegment GetFirstSegmentOrThrow()
810
886
811
887
#endregion Helper methods
812
888
}
889
+
890
+ public class StackExchangeRedisCacheMock : NewRelic . Agent . Extensions . Helpers . IStackExchangeRedisCache
891
+ {
892
+ private IAgent _agent ;
893
+
894
+ public StackExchangeRedisCacheMock ( IAgent agent )
895
+ {
896
+ _agent = agent ;
897
+ }
898
+
899
+ public void Dispose ( )
900
+ {
901
+ // Do nothing
902
+ }
903
+
904
+ public void Harvest ( ISegment segment )
905
+ {
906
+ // Create a segment if callled so we can test it
907
+ var testSegment = _agent . StartStackExchangeRedisDatastoreRequestSegmentOrThrow ( "set" , DatastoreVendor . Redis , TimeSpan . Zero , TimeSpan . FromMilliseconds ( 1 ) ) ;
908
+ testSegment . EndStackExchangeRedis ( ) ;
909
+ }
910
+ }
813
911
}
0 commit comments