File: tracer_test.cc

package info (click to toggle)
opentelemetry-cpp 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,744 kB
  • sloc: cpp: 79,029; sh: 1,640; makefile: 43; python: 31
file content (1185 lines) | stat: -rw-r--r-- 43,935 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <assert.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <initializer_list>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/common/macros.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/context/context.h"
#include "opentelemetry/exporters/memory/in_memory_span_data.h"
#include "opentelemetry/exporters/memory/in_memory_span_exporter.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/id_generator.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/random_id_generator.h"
#include "opentelemetry/sdk/trace/sampler.h"
#include "opentelemetry/sdk/trace/samplers/always_off.h"
#include "opentelemetry/sdk/trace/samplers/always_on.h"
#include "opentelemetry/sdk/trace/samplers/parent.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/sdk/trace/tracer_context.h"
#include "opentelemetry/trace/context.h"
#include "opentelemetry/trace/scope.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/span_metadata.h"
#include "opentelemetry/trace/span_startoptions.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/trace_state.h"
#include "opentelemetry/trace/tracer.h"

using namespace opentelemetry::sdk::trace;
using namespace opentelemetry::sdk::resource;
using opentelemetry::common::SteadyTimestamp;
using opentelemetry::common::SystemTimestamp;
namespace nostd     = opentelemetry::nostd;
namespace common    = opentelemetry::common;
namespace trace_api = opentelemetry::trace;
using opentelemetry::exporter::memory::InMemorySpanData;
using opentelemetry::exporter::memory::InMemorySpanExporter;
using opentelemetry::trace::SpanContext;

/**
 * A mock sampler with ShouldSample returning:
 *  Decision::RECORD_AND_SAMPLE if trace_id is valid
 *  Decision::DROP otherwise.
 */
class MockSampler final : public Sampler
{
public:
  SamplingResult ShouldSample(
      const SpanContext & /*parent_context*/,
      trace_api::TraceId trace_id,
      nostd::string_view /*name*/,
      trace_api::SpanKind /*span_kind*/,
      const opentelemetry::common::KeyValueIterable & /*attributes*/,
      const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/) noexcept override
  {
    // Sample only if valid trace_id ( This is to test Sampler get's valid trace id)
    if (trace_id.IsValid())
    {
      // Return two pairs of attributes. These attributes should be added to the
      // span attributes
      return {Decision::RECORD_AND_SAMPLE,
              nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
                  new const std::map<std::string, opentelemetry::common::AttributeValue>(
                      {{"sampling_attr1", 123}, {"sampling_attr2", "string"}})),
              nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr)};
    }
    else
    {
      // we should never reach here
      assert(false);
      return {Decision::DROP,
              nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
                  nullptr),
              nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr)};
    }
  }

  nostd::string_view GetDescription() const noexcept override { return "MockSampler"; }
};

/**
 * A Mock Custom Id Generator
 */
class MockIdGenerator : public IdGenerator
{
public:
  MockIdGenerator() : IdGenerator(false) {}

  opentelemetry::trace::SpanId GenerateSpanId() noexcept override
  {
    return opentelemetry::trace::SpanId(buf_span);
  }

  opentelemetry::trace::TraceId GenerateTraceId() noexcept override
  {
    return opentelemetry::trace::TraceId(buf_trace);
  }
  uint8_t buf_span[8]   = {1, 2, 3, 4, 5, 6, 7, 8};
  uint8_t buf_trace[16] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
};

namespace
{
std::shared_ptr<opentelemetry::trace::Tracer> initTracer(std::unique_ptr<SpanExporter> &&exporter)
{
  auto processor = std::unique_ptr<SpanProcessor>(new SimpleSpanProcessor(std::move(exporter)));
  std::vector<std::unique_ptr<SpanProcessor>> processors;
  processors.push_back(std::move(processor));
  auto context = std::make_shared<TracerContext>(std::move(processors));
  return std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(context));
}

std::shared_ptr<opentelemetry::trace::Tracer> initTracer(
    std::unique_ptr<SpanExporter> &&exporter,
    // For testing, just shove a pointer over, we'll take it over.
    Sampler *sampler,
    IdGenerator *id_generator = new RandomIdGenerator)
{
  auto processor = std::unique_ptr<SpanProcessor>(new SimpleSpanProcessor(std::move(exporter)));
  std::vector<std::unique_ptr<SpanProcessor>> processors;
  processors.push_back(std::move(processor));
  auto resource = Resource::Create({});
  auto context  = std::make_shared<TracerContext>(std::move(processors), resource,
                                                  std::unique_ptr<Sampler>(sampler),
                                                  std::unique_ptr<IdGenerator>(id_generator));
  return std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(context));
}

}  // namespace

TEST(Tracer, ToInMemorySpanExporter)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  auto span_first  = tracer->StartSpan("span 1");
  auto scope_first = tracer->WithActiveSpan(span_first);
  auto span_second = tracer->StartSpan("span 2");

  ASSERT_EQ(0, span_data->GetSpans().size());

  span_second->End();

  auto span2 = span_data->GetSpans();
  ASSERT_EQ(1, span2.size());
  ASSERT_EQ("span 2", span2.at(0)->GetName());
  EXPECT_TRUE(span2.at(0)->GetTraceId().IsValid());
  EXPECT_TRUE(span2.at(0)->GetSpanId().IsValid());
  EXPECT_TRUE(span2.at(0)->GetParentSpanId().IsValid());

  span_first->End();

  auto span1 = span_data->GetSpans();
  ASSERT_EQ(1, span1.size());
  ASSERT_EQ("span 1", span1.at(0)->GetName());
  EXPECT_TRUE(span1.at(0)->GetTraceId().IsValid());
  EXPECT_TRUE(span1.at(0)->GetSpanId().IsValid());
  EXPECT_FALSE(span1.at(0)->GetParentSpanId().IsValid());

  // Verify trace and parent span id propagation
  EXPECT_EQ(span1.at(0)->GetTraceId(), span2.at(0)->GetTraceId());
  EXPECT_EQ(span2.at(0)->GetParentSpanId(), span1.at(0)->GetSpanId());
}

TEST(Tracer, StartSpanSampleOn)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer_on                              = initTracer(std::unique_ptr<SpanExporter>{exporter});

  tracer_on->StartSpan("span 1")->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());

  auto &cur_span_data = spans.at(0);
  ASSERT_LT(std::chrono::nanoseconds(0), cur_span_data->GetStartTime().time_since_epoch());
  ASSERT_LT(std::chrono::nanoseconds(0), cur_span_data->GetDuration());
}

TEST(Tracer, StartSpanSampleOff)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer_off = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOffSampler());

  // This span will not be recorded.
  auto span = tracer_off->StartSpan("span 2");

  // Always generate a valid span-context (span-id)
  auto context = span->GetContext();
  EXPECT_TRUE(context.IsValid());
  EXPECT_FALSE(context.IsSampled());

  span->End();
  // The span doesn't write any span data because the sampling decision is alway
  // DROP.
  ASSERT_EQ(0, span_data->GetSpans().size());
}

TEST(Tracer, StartSpanCustomIdGenerator)
{
  IdGenerator *id_generator                   = new MockIdGenerator();
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer =
      initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOnSampler(), id_generator);

  tracer->StartSpan("span 1")->End();
  auto spans          = span_data->GetSpans();
  auto &cur_span_data = spans.at(0);

  EXPECT_EQ(cur_span_data->GetTraceId(), id_generator->GenerateTraceId());
  EXPECT_EQ(cur_span_data->GetSpanId(), id_generator->GenerateSpanId());
}

TEST(Tracer, StartSpanWithOptionsTime)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  opentelemetry::trace::StartSpanOptions start;
  start.start_system_time = SystemTimestamp(std::chrono::nanoseconds(300));
  start.start_steady_time = SteadyTimestamp(std::chrono::nanoseconds(10));

  opentelemetry::trace::EndSpanOptions end;
  end.end_steady_time = SteadyTimestamp(std::chrono::nanoseconds(40));

  tracer->StartSpan("span 1", start)->End(end);

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());

  auto &cur_span_data = spans.at(0);
  ASSERT_EQ(std::chrono::nanoseconds(300), cur_span_data->GetStartTime().time_since_epoch());
  ASSERT_EQ(std::chrono::nanoseconds(30), cur_span_data->GetDuration());
}

TEST(Tracer, StartSpanWithAttributes)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  // Start a span with all supported scalar attribute types.
  tracer
      ->StartSpan("span 1", {{"attr1", "string"},
                             {"attr2", false},
                             {"attr1", 314159},
                             {"attr3", static_cast<unsigned int>(314159)},
                             {"attr4", static_cast<int32_t>(-20)},
                             {"attr5", static_cast<uint32_t>(20)},
                             {"attr6", static_cast<int64_t>(-20)},
                             {"attr7", static_cast<uint64_t>(20)},
                             {"attr8", 3.1},
                             {"attr9", "string"}})
      ->End();

  // Start a span with all supported array attribute types.
  int listInt[]                       = {1, 2, 3};
  unsigned int listUInt[]             = {1, 2, 3};
  int32_t listInt32[]                 = {1, -2, 3};
  uint32_t listUInt32[]               = {1, 2, 3};
  int64_t listInt64[]                 = {1, -2, 3};
  uint64_t listUInt64[]               = {1, 2, 3};
  double listDouble[]                 = {1.1, 2.1, 3.1};
  bool listBool[]                     = {true, false};
  nostd::string_view listStringView[] = {"a", "b"};
  std::map<std::string, common::AttributeValue> m;
  m["attr1"] = nostd::span<int>(listInt);
  m["attr2"] = nostd::span<unsigned int>(listUInt);
  m["attr3"] = nostd::span<int32_t>(listInt32);
  m["attr4"] = nostd::span<uint32_t>(listUInt32);
  m["attr5"] = nostd::span<int64_t>(listInt64);
  m["attr6"] = nostd::span<uint64_t>(listUInt64);
  m["attr7"] = nostd::span<double>(listDouble);
  m["attr8"] = nostd::span<bool>(listBool);
  m["attr9"] = nostd::span<nostd::string_view>(listStringView);

  tracer->StartSpan("span 2", m)->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(2, spans.size());

  auto &cur_span_data = spans.at(0);
  ASSERT_EQ(9, cur_span_data->GetAttributes().size());
  ASSERT_EQ(314159, nostd::get<int32_t>(cur_span_data->GetAttributes().at("attr1")));
  ASSERT_EQ(false, nostd::get<bool>(cur_span_data->GetAttributes().at("attr2")));
  ASSERT_EQ(static_cast<uint32_t>(314159),
            nostd::get<uint32_t>(cur_span_data->GetAttributes().at("attr3")));
  ASSERT_EQ(-20, nostd::get<int32_t>(cur_span_data->GetAttributes().at("attr4")));
  ASSERT_EQ(static_cast<uint32_t>(20),
            nostd::get<uint32_t>(cur_span_data->GetAttributes().at("attr5")));
  ASSERT_EQ(-20, nostd::get<int64_t>(cur_span_data->GetAttributes().at("attr6")));
  ASSERT_EQ(static_cast<uint64_t>(20),
            nostd::get<uint64_t>(cur_span_data->GetAttributes().at("attr7")));
  ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("attr8")));
  ASSERT_EQ("string", nostd::get<std::string>(cur_span_data->GetAttributes().at("attr9")));

  auto &cur_span_data2 = spans.at(1);
  ASSERT_EQ(9, cur_span_data2->GetAttributes().size());
  ASSERT_EQ(std::vector<int32_t>({1, 2, 3}),
            nostd::get<std::vector<int32_t>>(cur_span_data2->GetAttributes().at("attr1")));
  ASSERT_EQ(std::vector<uint32_t>({1, 2, 3}),
            nostd::get<std::vector<uint32_t>>(cur_span_data2->GetAttributes().at("attr2")));
  ASSERT_EQ(std::vector<int32_t>({1, -2, 3}),
            nostd::get<std::vector<int32_t>>(cur_span_data2->GetAttributes().at("attr3")));
  ASSERT_EQ(std::vector<uint32_t>({1, 2, 3}),
            nostd::get<std::vector<uint32_t>>(cur_span_data2->GetAttributes().at("attr4")));
  ASSERT_EQ(std::vector<int64_t>({1, -2, 3}),
            nostd::get<std::vector<int64_t>>(cur_span_data2->GetAttributes().at("attr5")));
  ASSERT_EQ(std::vector<uint64_t>({1, 2, 3}),
            nostd::get<std::vector<uint64_t>>(cur_span_data2->GetAttributes().at("attr6")));
  ASSERT_EQ(std::vector<double>({1.1, 2.1, 3.1}),
            nostd::get<std::vector<double>>(cur_span_data2->GetAttributes().at("attr7")));
  ASSERT_EQ(std::vector<bool>({true, false}),
            nostd::get<std::vector<bool>>(cur_span_data2->GetAttributes().at("attr8")));
  ASSERT_EQ(std::vector<std::string>({"a", "b"}),
            nostd::get<std::vector<std::string>>(cur_span_data2->GetAttributes().at("attr9")));
}

TEST(Tracer, StartSpanWithAttributesCopy)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  {
    std::unique_ptr<std::vector<int64_t>> numbers(new std::vector<int64_t>);
    numbers->push_back(1);
    numbers->push_back(2);
    numbers->push_back(3);

    std::unique_ptr<std::vector<nostd::string_view>> strings(new std::vector<nostd::string_view>);
    std::string s1("a");
    std::string s2("b");
    std::string s3("c");
    strings->push_back(s1);
    strings->push_back(s2);
    strings->push_back(s3);
    tracer
        ->StartSpan("span 1",
                    {{"attr1", *numbers}, {"attr2", nostd::span<nostd::string_view>(*strings)}})
        ->End();
  }

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());

  auto &cur_span_data = spans.at(0);
  ASSERT_EQ(2, cur_span_data->GetAttributes().size());

  auto numbers = nostd::get<std::vector<int64_t>>(cur_span_data->GetAttributes().at("attr1"));
  ASSERT_EQ(3, numbers.size());
  ASSERT_EQ(1, numbers[0]);
  ASSERT_EQ(2, numbers[1]);
  ASSERT_EQ(3, numbers[2]);

  auto strings = nostd::get<std::vector<std::string>>(cur_span_data->GetAttributes().at("attr2"));
  ASSERT_EQ(3, strings.size());
  ASSERT_EQ("a", strings[0]);
  ASSERT_EQ("b", strings[1]);
  ASSERT_EQ("c", strings[2]);
}

TEST(Tracer, GetSampler)
{
  auto resource = Resource::Create({});
  // Create a Tracer with a default AlwaysOnSampler
  auto tracer_on = initTracer(nullptr);

#ifdef OPENTELEMETRY_RTTI_ENABLED
  auto &t1 = std::dynamic_pointer_cast<Tracer>(tracer_on)->GetSampler();
#else
  auto &t1 = std::static_pointer_cast<Tracer>(tracer_on)->GetSampler();
#endif
  ASSERT_EQ("AlwaysOnSampler", t1.GetDescription());

  // Create a Tracer with a AlwaysOffSampler
  auto tracer_off = initTracer(nullptr, new AlwaysOffSampler());

#ifdef OPENTELEMETRY_RTTI_ENABLED
  auto &t2 = std::dynamic_pointer_cast<Tracer>(tracer_off)->GetSampler();
#else
  auto &t2 = std::static_pointer_cast<Tracer>(tracer_off)->GetSampler();
#endif
  ASSERT_EQ("AlwaysOffSampler", t2.GetDescription());
}

TEST(Tracer, SpanSetAttribute)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  auto span = tracer->StartSpan("span 1");

  span->SetAttribute("abc", 3.1);

  span->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());
  auto &cur_span_data = spans.at(0);
  ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("abc")));
}

TEST(Tracer, TestAfterEnd)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});
  auto span                                   = tracer->StartSpan("span 1");
  span->SetAttribute("abc", 3.1);

  span->End();

  // test after end
  span->SetAttribute("testing null recordable", 3.1);
  span->AddEvent("event 1");
  span->AddEvent("event 2", std::chrono::system_clock::now());
  span->AddEvent("event 3", std::chrono::system_clock::now(), {{"attr1", 1}});
  std::string new_name{"new name"};
  span->UpdateName(new_name);
  span->SetAttribute("attr1", 3.1);
  std::string description{"description"};
  span->SetStatus(opentelemetry::trace::StatusCode::kError, description);
  span->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());
  auto &cur_span_data = spans.at(0);
  ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("abc")));
}

TEST(Tracer, SpanSetEvents)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  auto span = tracer->StartSpan("span 1");
  span->AddEvent("event 1");
  span->AddEvent("event 2", std::chrono::system_clock::now());
  span->AddEvent("event 3", std::chrono::system_clock::now(), {{"attr1", 1}});
  span->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());

  auto &span_data_events = spans.at(0)->GetEvents();
  ASSERT_EQ(3, span_data_events.size());
  ASSERT_EQ("event 1", span_data_events[0].GetName());
  ASSERT_EQ("event 2", span_data_events[1].GetName());
  ASSERT_EQ("event 3", span_data_events[2].GetName());
  ASSERT_EQ(0, span_data_events[0].GetAttributes().size());
  ASSERT_EQ(0, span_data_events[1].GetAttributes().size());
  ASSERT_EQ(1, span_data_events[2].GetAttributes().size());
}

TEST(Tracer, SpanSetLinks)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  {

    // Single span link passed through Initialization list
    tracer->StartSpan("efg", {{"attr1", 1}}, {{SpanContext(false, false), {{"attr2", 2}}}})->End();
    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(1, span_data_links.size());
    auto link = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link.GetAttributes().at("attr2")), 2);
  }
  {

    // Multiple span links passed through Initialization list
    tracer
        ->StartSpan("efg", {{"attr1", 1}},
                    {{SpanContext(false, false), {{"attr2", 2}}},
                     {SpanContext(false, false), {{"attr3", 3}}}})
        ->End();
    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr3")), 3);
  }

  {

    // Multiple links, each with multiple attributes passed through Initialization list
    tracer
        ->StartSpan("efg", {{"attr1", 1}},
                    {{SpanContext(false, false), {{"attr2", 2}, {"attr3", 3}}},
                     {SpanContext(false, false), {{"attr4", 4}}}})
        ->End();
    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr3")), 3);
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr4")), 4);
  }

  {
    std::map<std::string, std::string> attrs1 = {{"attr1", "1"}, {"attr2", "2"}};
    std::map<std::string, std::string> attrs2 = {{"attr3", "3"}, {"attr4", "4"}};

    std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> links = {
        {SpanContext(false, false), attrs1}, {SpanContext(false, false), attrs2}};
    tracer->StartSpan("efg", attrs1, links)->End();
    auto spans = span_data->GetSpans();

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr1")), "1");
    ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr2")), "2");
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr3")), "3");
    ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr4")), "4");
  }
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
TEST(Tracer, SpanAddLinkAbiv2)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  {
    auto span = tracer->StartSpan("span");
    SpanContext target(false, false);
    // Single link attribute passed through Initialization list
    span->AddLink(target, {{"attr2", 2}});
    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(1, span_data_links.size());
    auto link  = span_data_links.at(0);
    auto attrs = link.GetAttributes();
    ASSERT_EQ(nostd::get<int>(attrs.at("attr2")), 2);
  }

  {
    auto span = tracer->StartSpan("span");
    SpanContext target(false, false);
    // Multiple link attributes passed through Initialization list
    span->AddLink(target, {{"attr2", 2}, {"attr3", 3}});
    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(1, span_data_links.size());
    auto link  = span_data_links.at(0);
    auto attrs = link.GetAttributes();
    ASSERT_EQ(nostd::get<int>(attrs.at("attr2")), 2);
    ASSERT_EQ(nostd::get<int>(attrs.at("attr3")), 3);
  }

  {
    std::map<std::string, std::string> attrs_map = {{"attr1", "1"}, {"attr2", "2"}};

    auto span = tracer->StartSpan("span");
    SpanContext target(false, false);
    span->AddLink(target, attrs_map);
    span->End();

    auto spans = span_data->GetSpans();

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(1, span_data_links.size());
    auto link  = span_data_links.at(0);
    auto attrs = link.GetAttributes();
    ASSERT_EQ(nostd::get<std::string>(attrs.at("attr1")), "1");
    ASSERT_EQ(nostd::get<std::string>(attrs.at("attr2")), "2");
  }

  {
    auto span = tracer->StartSpan("span");
    SpanContext target(false, false);

    // Single link attribute passed through Initialization list
    span->AddLink(target, {{"attr1", 1}});

    // Multiple link attributes passed through Initialization list
    span->AddLink(target, {{"attr2", 2}, {"attr3", 3}});

    std::map<std::string, std::string> attrs_map = {{"attr4", "4"}, {"attr5", "5"}};
    span->AddLink(target, attrs_map);

    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(3, span_data_links.size());

    {
      auto link  = span_data_links.at(0);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr1")), 1);
    }

    {
      auto link  = span_data_links.at(1);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr2")), 2);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr3")), 3);
    }

    {
      auto link  = span_data_links.at(2);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attr4")), "4");
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attr5")), "5");
    }
  }
}

TEST(Tracer, SpanAddLinksAbiv2)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  {
    auto span = tracer->StartSpan("span");
    // Single span link passed through Initialization list
    span->AddLinks({{SpanContext(false, false), {{"attr2", 2}}}});
    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(1, span_data_links.size());
    auto link = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link.GetAttributes().at("attr2")), 2);
  }

  {
    auto span = tracer->StartSpan("span");
    // Multiple span links passed through Initialization list
    span->AddLinks(
        {{SpanContext(false, false), {{"attr2", 2}}}, {SpanContext(false, false), {{"attr3", 3}}}});
    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr3")), 3);
  }

  {
    auto span = tracer->StartSpan("span");
    // Multiple links, each with multiple attributes passed through Initialization list
    span->AddLinks({{SpanContext(false, false), {{"attr2", 2}, {"attr3", 3}}},
                    {SpanContext(false, false), {{"attr4", 4}}}});
    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
    ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr3")), 3);
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr4")), 4);
  }

  {
    std::map<std::string, std::string> attrs1 = {{"attr1", "1"}, {"attr2", "2"}};
    std::map<std::string, std::string> attrs2 = {{"attr3", "3"}, {"attr4", "4"}};

    std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> links = {
        {SpanContext(false, false), attrs1}, {SpanContext(false, false), attrs2}};

    auto span = tracer->StartSpan("span");
    span->AddLinks(links);
    span->End();

    auto spans = span_data->GetSpans();

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(2, span_data_links.size());
    auto link1 = span_data_links.at(0);
    ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr1")), "1");
    ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr2")), "2");
    auto link2 = span_data_links.at(1);
    ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr3")), "3");
    ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr4")), "4");
  }

  {
    auto span = tracer->StartSpan("span");

    // Single span link passed through Initialization list
    span->AddLinks({{SpanContext(false, false), {{"attr10", 10}}}});
    span->AddLinks({{SpanContext(false, false), {{"attr11", 11}}}});

    // Multiple span links passed through Initialization list
    span->AddLinks({{SpanContext(false, false), {{"attr12", 12}}},
                    {SpanContext(false, false), {{"attr13", 13}}}});
    span->AddLinks({{SpanContext(false, false), {{"attr14", 14}}},
                    {SpanContext(false, false), {{"attr15", 15}}}});

    // Multiple links, each with multiple attributes passed through Initialization list
    span->AddLinks({{SpanContext(false, false), {{"attr16", 16}, {"attr17", 17}}},
                    {SpanContext(false, false), {{"attr18", 18}}}});
    span->AddLinks({{SpanContext(false, false), {{"attr19", 19}, {"attr20", 20}}},
                    {SpanContext(false, false), {{"attr21", 21}}}});

    std::map<std::string, std::string> attrsa1 = {{"attra1", "1"}, {"attra2", "2"}};
    std::map<std::string, std::string> attrsa2 = {{"attra3", "3"}, {"attra4", "4"}};

    std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> linksa = {
        {SpanContext(false, false), attrsa1}, {SpanContext(false, false), attrsa2}};

    span->AddLinks(linksa);

    std::map<std::string, std::string> attrsb1 = {{"attrb1", "1"}, {"attrb2", "2"}};
    std::map<std::string, std::string> attrsb2 = {{"attrb3", "3"}, {"attrb4", "4"}};

    std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> linksb = {
        {SpanContext(false, false), attrsb1}, {SpanContext(false, false), attrsb2}};

    span->AddLinks(linksb);

    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(14, span_data_links.size());

    {
      auto link  = span_data_links.at(0);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr10")), 10);
    }

    {
      auto link  = span_data_links.at(1);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr11")), 11);
    }

    {
      auto link  = span_data_links.at(2);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr12")), 12);
    }

    {
      auto link  = span_data_links.at(3);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr13")), 13);
    }

    {
      auto link  = span_data_links.at(4);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr14")), 14);
    }

    {
      auto link  = span_data_links.at(5);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr15")), 15);
    }

    {
      auto link  = span_data_links.at(6);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr16")), 16);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr17")), 17);
    }

    {
      auto link  = span_data_links.at(7);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr18")), 18);
    }

    {
      auto link  = span_data_links.at(8);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr19")), 19);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr20")), 20);
    }

    {
      auto link  = span_data_links.at(9);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr21")), 21);
    }

    {
      auto link  = span_data_links.at(10);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attra1")), "1");
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attra2")), "2");
    }

    {
      auto link  = span_data_links.at(11);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attra3")), "3");
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attra4")), "4");
    }

    {
      auto link  = span_data_links.at(12);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb1")), "1");
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb2")), "2");
    }

    {
      auto link  = span_data_links.at(13);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 2);
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb3")), "3");
      ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb4")), "4");
    }
  }
}

TEST(Tracer, SpanMixLinksAbiv2)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  {
    // Link 1 added at StartSpan
    auto span =
        tracer->StartSpan("span", {{"attr1", 1}}, {{SpanContext(false, false), {{"attr2", 2}}}});

    SpanContext target(false, false);
    // Link 2 added with AddLink
    span->AddLink(target, {{"attr3", 3}});

    // Link 3 added with AddLinks
    span->AddLinks({{SpanContext(false, false), {{"attr4", 4}}}});

    span->End();

    auto spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());

    auto &span_data_links = spans.at(0)->GetLinks();
    ASSERT_EQ(3, span_data_links.size());

    {
      auto link  = span_data_links.at(0);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr2")), 2);
    }

    {
      auto link  = span_data_links.at(1);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr3")), 3);
    }

    {
      auto link  = span_data_links.at(2);
      auto attrs = link.GetAttributes();
      ASSERT_EQ(attrs.size(), 1);
      ASSERT_EQ(nostd::get<int>(attrs.at("attr4")), 4);
    }
  }
}
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */

TEST(Tracer, TestAlwaysOnSampler)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer_on                              = initTracer(std::unique_ptr<SpanExporter>{exporter});

  // Testing AlwaysOn sampler.
  // Create two spans for each tracer. Check the exported result.
  auto span_on_1 = tracer_on->StartSpan("span 1");
  auto span_on_2 = tracer_on->StartSpan("span 2");
  span_on_2->End();
  span_on_1->End();

  auto spans = span_data->GetSpans();
  ASSERT_EQ(2, spans.size());
  ASSERT_EQ("span 2", spans.at(0)->GetName());  // span 2 ends first.
  ASSERT_EQ("span 1", spans.at(1)->GetName());
}

TEST(Tracer, TestAlwaysOffSampler)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer_off = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOffSampler());
  auto span_off_1 = tracer_off->StartSpan("span 1");
  auto span_off_2 = tracer_off->StartSpan("span 2");

  span_off_1->SetAttribute("attr1", 3.1);  // Not recorded.

  span_off_2->End();
  span_off_1->End();

  // The tracer export nothing with an AlwaysOff sampler
  ASSERT_EQ(0, span_data->GetSpans().size());
}

TEST(Tracer, TestParentBasedSampler)
{
  // Current ShouldSample always pass an empty ParentContext,
  // so this sampler will work as an AlwaysOnSampler.
  InMemorySpanExporter *exporter                        = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data_parent_on = exporter->GetData();
  auto tracer_parent_on = initTracer(std::unique_ptr<SpanExporter>{exporter},
                                     new ParentBasedSampler(std::make_shared<AlwaysOnSampler>()));

  auto span_parent_on_1 = tracer_parent_on->StartSpan("span 1");
  auto span_parent_on_2 = tracer_parent_on->StartSpan("span 2");

  span_parent_on_1->SetAttribute("attr1", 3.1);

  span_parent_on_2->End();
  span_parent_on_1->End();

  auto spans = span_data_parent_on->GetSpans();
  ASSERT_EQ(2, spans.size());
  ASSERT_EQ("span 2", spans.at(0)->GetName());
  ASSERT_EQ("span 1", spans.at(1)->GetName());

  // Current ShouldSample always pass an empty ParentContext,
  // so this sampler will work as an AlwaysOnSampler.
  InMemorySpanExporter *exporter2                        = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data_parent_off = exporter2->GetData();
  auto tracer_parent_off =
      initTracer(std::unique_ptr<SpanExporter>{exporter2},
                 // Add this to avoid different results for old and new version of clang-format
                 new ParentBasedSampler(std::make_shared<AlwaysOffSampler>()));

  auto span_parent_off_1 = tracer_parent_off->StartSpan("span 1");
  auto span_parent_off_2 = tracer_parent_off->StartSpan("span 2");

  span_parent_off_1->SetAttribute("attr1", 3.1);

  span_parent_off_1->End();
  span_parent_off_2->End();
  ASSERT_EQ(0, span_data_parent_off->GetSpans().size());
}

TEST(Tracer, WithActiveSpan)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});
  auto spans                                  = span_data.get()->GetSpans();

  ASSERT_EQ(0, spans.size());

  {
    auto span_first  = tracer->StartSpan("span 1");
    auto scope_first = tracer->WithActiveSpan(span_first);

    {
      auto span_second  = tracer->StartSpan("span 2");
      auto scope_second = tracer->WithActiveSpan(span_second);

      spans = span_data->GetSpans();
      ASSERT_EQ(0, spans.size());

      span_second->End();
    }

    spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());
    EXPECT_EQ("span 2", spans.at(0)->GetName());
    EXPECT_EQ(spans.at(0).get()->GetParentSpanId(), span_first->GetContext().span_id());
    EXPECT_EQ(spans.at(0).get()->GetTraceId(), span_first->GetContext().trace_id());

    {
      trace_api::StartSpanOptions options;
      opentelemetry::context::Context c1;
      c1             = c1.SetValue(opentelemetry::trace::kIsRootSpanKey, true);
      options.parent = c1;
      auto root_span = tracer->StartSpan("span root", options);

      spans = span_data->GetSpans();
      ASSERT_EQ(0, spans.size());

      root_span->End();
    }
    spans = span_data->GetSpans();
    ASSERT_EQ(1, spans.size());
    EXPECT_EQ("span root", spans.at(0)->GetName());
    EXPECT_EQ(spans.at(0).get()->GetParentSpanId(), opentelemetry::trace::SpanId());
    EXPECT_NE(spans.at(0).get()->GetTraceId(), span_first->GetContext().trace_id());

    span_first->End();
  }

  spans = span_data->GetSpans();
  ASSERT_EQ(1, spans.size());
  EXPECT_EQ("span 1", spans.at(0)->GetName());
}

TEST(Tracer, ExpectParent)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});
  auto spans                                  = span_data.get()->GetSpans();

  ASSERT_EQ(0, spans.size());

  auto span_first = tracer->StartSpan("span 1");

  trace_api::StartSpanOptions options;
  options.parent   = span_first->GetContext();
  auto span_second = tracer->StartSpan("span 2", options);

  options.parent  = span_second->GetContext();
  auto span_third = tracer->StartSpan("span 3", options);

  span_third->End();
  span_second->End();
  span_first->End();

  spans = span_data->GetSpans();
  ASSERT_EQ(3, spans.size());
  auto spandata_first  = std::move(spans.at(2));
  auto spandata_second = std::move(spans.at(1));
  auto spandata_third  = std::move(spans.at(0));
  EXPECT_EQ("span 1", spandata_first->GetName());
  EXPECT_EQ("span 2", spandata_second->GetName());
  EXPECT_EQ("span 3", spandata_third->GetName());

  EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
  EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}

TEST(Tracer, ExpectParentAsContext)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});
  auto spans                                  = span_data.get()->GetSpans();

  ASSERT_EQ(0, spans.size());

  auto span_first = tracer->StartSpan("span 1");

  opentelemetry::context::Context c1;
  auto c2 = trace_api::SetSpan(c1, span_first);
  trace_api::StartSpanOptions options;
  options.parent   = c2;
  auto span_second = tracer->StartSpan("span 2", options);

  auto c3         = trace_api::SetSpan(c2, span_second);
  options.parent  = c3;
  auto span_third = tracer->StartSpan("span 3", options);

  span_third->End();
  span_second->End();
  span_first->End();

  spans = span_data->GetSpans();
  ASSERT_EQ(3, spans.size());
  auto spandata_first  = std::move(spans.at(2));
  auto spandata_second = std::move(spans.at(1));
  auto spandata_third  = std::move(spans.at(0));
  EXPECT_EQ("span 1", spandata_first->GetName());
  EXPECT_EQ("span 2", spandata_second->GetName());
  EXPECT_EQ("span 3", spandata_third->GetName());

  EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
  EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}

TEST(Tracer, ValidTraceIdToSampler)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer                                 = initTracer(std::unique_ptr<SpanExporter>{exporter});

  auto span = tracer->StartSpan("span 1");
  // sampler was fed with valid trace_id, so span shouldn't be NoOp Span.
  EXPECT_TRUE(span->IsRecording());
  EXPECT_TRUE(span->GetContext().IsValid());
}

TEST(Tracer, SpanCleanupWithScope)
{
  InMemorySpanExporter *exporter              = new InMemorySpanExporter();
  std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
  auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new MockSampler());
  {
    auto span0 = tracer->StartSpan("Span0");
    auto span1 = tracer->StartSpan("span1");
    {
      trace_api::Scope scope1(span1);
      auto span2 = tracer->StartSpan("span2");
      {
        trace_api::Scope scope2(span2);
        auto span3 = tracer->StartSpan("span3");
      }
    }
  }
  EXPECT_EQ(4, span_data->GetSpans().size());
}