File: ostream_span_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (412 lines) | stat: -rw-r--r-- 14,180 bytes parent folder | download | duplicates (3)
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stdint.h>
#include <array>
#include <chrono>
#include <initializer_list>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/key_value_iterable_view.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/exporters/ostream/span_exporter.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/span_metadata.h"
#include "opentelemetry/trace/trace_flags.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/trace_state.h"
#include "ostream_capture.h"

using namespace opentelemetry::exporter::ostream::test;

namespace trace         = opentelemetry::trace;
namespace common        = opentelemetry::common;
namespace nostd         = opentelemetry::nostd;
namespace trace_sdk     = opentelemetry::sdk::trace;
namespace resource      = opentelemetry::sdk::resource;
namespace exportertrace = opentelemetry::exporter::trace;

using Attributes = std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>;

class TestResource : public resource::Resource
{
public:
  TestResource(const resource::ResourceAttributes &attributes = resource::ResourceAttributes())
      : resource::Resource(attributes)
  {}
};

// Testing Shutdown functionality of OStreamSpanExporter, should expect no data to be sent to Stream
TEST(OStreamSpanExporter, Shutdown)
{
  auto exporter  = std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter);
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();
  recordable->SetName("Test Span");

  // Capture the output of cerr
  const auto captured = WithOStreamCapture(std::cerr, [&]() {
    EXPECT_TRUE(processor->Shutdown());
    processor->OnEnd(std::move(recordable));
  });
  std::string err_message =
      "[Ostream Trace Exporter] Exporting 1 span(s) failed, exporter is shutdown";
  EXPECT_TRUE(captured.find(err_message) != std::string::npos);
}

constexpr const char *kDefaultSpanPrinted =
    "{\n"
    "  name          : \n"
    "  trace_id      : 00000000000000000000000000000000\n"
    "  span_id       : 0000000000000000\n"
    "  tracestate    : \n"
    "  parent_span_id: 0000000000000000\n"
    "  start         : 0\n"
    "  duration      : 0\n"
    "  description   : \n"
    "  span kind     : Internal\n"
    "  status        : Unset\n"
    "  attributes    : \n"
    "  events        : \n"
    "  links         : \n"
    "  resources     : \n"
    "  instr-lib     : unknown_service\n"
    "}\n";

// Testing what a default span that is not changed will print out, either all 0's or empty values
TEST(OStreamSpanExporter, PrintDefaultSpan)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  processor->OnEnd(std::move(recordable));

  EXPECT_EQ(output.str(), kDefaultSpanPrinted);
}

TEST(OStreamSpanExporter, PrintSpanWithBasicFields)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  constexpr uint8_t trace_id_buf[]       = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
  constexpr uint8_t span_id_buf[]        = {1, 2, 3, 4, 5, 6, 7, 8};
  constexpr uint8_t parent_span_id_buf[] = {8, 7, 6, 5, 4, 3, 2, 1};
  trace::TraceId trace_id{trace_id_buf};
  trace::SpanId span_id{span_id_buf};
  trace::SpanId parent_span_id{parent_span_id_buf};
  const auto trace_state = trace::TraceState::GetDefault()->Set("state1", "value");
  const trace::SpanContext span_context{
      trace_id, span_id, trace::TraceFlags{trace::TraceFlags::kIsSampled}, true, trace_state};

  recordable->SetIdentity(span_context, parent_span_id);
  recordable->SetName("Test Span");
  common::SystemTimestamp now(std::chrono::system_clock::now());
  recordable->SetStartTime(now);
  recordable->SetDuration(std::chrono::nanoseconds(100));
  recordable->SetStatus(trace::StatusCode::kOk, "Test Description");
  recordable->SetSpanKind(trace::SpanKind::kClient);

  TestResource resource1(resource::ResourceAttributes({{"key1", "val1"}}));
  recordable->SetResource(resource1);

  processor->OnEnd(std::move(recordable));

  std::string start = std::to_string(now.time_since_epoch().count());

  std::string expectedOutput =
      "{\n"
      "  name          : Test Span\n"
      "  trace_id      : 01020304050607080102030405060708\n"
      "  span_id       : 0102030405060708\n"
      "  tracestate    : state1=value\n"
      "  parent_span_id: 0807060504030201\n"
      "  start         : " +
      start +
      "\n"
      "  duration      : 100\n"
      "  description   : Test Description\n"
      "  span kind     : Client\n"
      "  status        : Ok\n"
      "  attributes    : \n"
      "  events        : \n"
      "  links         : \n"
      "  resources     : \n"
      "\tkey1: val1\n"
      "  instr-lib     : unknown_service\n"
      "}\n";
  EXPECT_EQ(output.str(), expectedOutput);
}

TEST(OStreamSpanExporter, PrintSpanWithAttribute)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  recordable->SetAttribute("attr1", "string");

  processor->OnEnd(std::move(recordable));

  std::string expectedOutput =
      "{\n"
      "  name          : \n"
      "  trace_id      : 00000000000000000000000000000000\n"
      "  span_id       : 0000000000000000\n"
      "  tracestate    : \n"
      "  parent_span_id: 0000000000000000\n"
      "  start         : 0\n"
      "  duration      : 0\n"
      "  description   : \n"
      "  span kind     : Internal\n"
      "  status        : Unset\n"
      "  attributes    : \n"
      "\tattr1: string\n"
      "  events        : \n"
      "  links         : \n"
      "  resources     : \n"
      "  instr-lib     : unknown_service\n"
      "}\n";
  EXPECT_EQ(output.str(), expectedOutput);
}

TEST(OStreamSpanExporter, PrintSpanWithArrayAttribute)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  std::array<int, 3> array1 = {1, 2, 3};
  nostd::span<int> span1{array1.data(), array1.size()};
  recordable->SetAttribute("array1", span1);

  processor->OnEnd(std::move(recordable));

  std::string expectedOutput =
      "{\n"
      "  name          : \n"
      "  trace_id      : 00000000000000000000000000000000\n"
      "  span_id       : 0000000000000000\n"
      "  tracestate    : \n"
      "  parent_span_id: 0000000000000000\n"
      "  start         : 0\n"
      "  duration      : 0\n"
      "  description   : \n"
      "  span kind     : Internal\n"
      "  status        : Unset\n"
      "  attributes    : \n"
      "\tarray1: [1,2,3]\n"
      "  events        : \n"
      "  links         : \n"
      "  resources     : \n"
      "  instr-lib     : unknown_service\n"
      "}\n";
  EXPECT_EQ(output.str(), expectedOutput);
}

TEST(OStreamSpanExporter, PrintSpanWithEvents)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();
  common::SystemTimestamp now(std::chrono::system_clock::now());
  common::SystemTimestamp next(std::chrono::system_clock::now() + std::chrono::seconds(1));

  std::string now_str  = std::to_string(now.time_since_epoch().count());
  std::string next_str = std::to_string(next.time_since_epoch().count());

  recordable->AddEvent("hello", now);
  recordable->AddEvent("world", next,
                       common::KeyValueIterableView<Attributes>({{"attr1", "string"}}));

  processor->OnEnd(std::move(recordable));

  std::string expectedOutput =
      "{\n"
      "  name          : \n"
      "  trace_id      : 00000000000000000000000000000000\n"
      "  span_id       : 0000000000000000\n"
      "  tracestate    : \n"
      "  parent_span_id: 0000000000000000\n"
      "  start         : 0\n"
      "  duration      : 0\n"
      "  description   : \n"
      "  span kind     : Internal\n"
      "  status        : Unset\n"
      "  attributes    : \n"
      "  events        : \n"
      "\t{\n"
      "\t  name          : hello\n"
      "\t  timestamp     : " +
      now_str +
      "\n"
      "\t  attributes    : \n"
      "\t}\n"
      "\t{\n"
      "\t  name          : world\n"
      "\t  timestamp     : " +
      next_str +
      "\n"
      "\t  attributes    : \n"
      "\t\tattr1: string\n"
      "\t}\n"
      "  links         : \n"
      "  resources     : \n"
      "  instr-lib     : unknown_service\n"
      "}\n";
  EXPECT_EQ(output.str(), expectedOutput);
}

TEST(OStreamSpanExporter, PrintSpanWithLinks)
{
  std::stringstream output;
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(output));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  // produce valid SpanContext with pseudo span and trace Id.
  uint8_t span_id_buf[trace::SpanId::kSize] = {
      1,
  };
  trace::SpanId span_id{span_id_buf};
  uint8_t trace_id_buf[trace::TraceId::kSize] = {
      2,
  };
  trace::TraceId trace_id{trace_id_buf};
  const auto span_context =
      trace::SpanContext(trace_id, span_id, trace::TraceFlags{trace::TraceFlags::kIsSampled}, true);

  // and another to check preserving order.
  uint8_t span_id_buf2[trace::SpanId::kSize] = {
      3,
  };
  trace::SpanId span_id2{span_id_buf2};
  const auto span_context2 =
      trace::SpanContext(trace_id, span_id2, trace::TraceFlags{trace::TraceFlags::kIsSampled}, true,
                         trace::TraceState::FromHeader("state1=value"));

  recordable->AddLink(span_context);
  recordable->AddLink(span_context2,
                      common::KeyValueIterableView<Attributes>({{"attr1", "string"}}));

  processor->OnEnd(std::move(recordable));

  std::string expectedOutput =
      "{\n"
      "  name          : \n"
      "  trace_id      : 00000000000000000000000000000000\n"
      "  span_id       : 0000000000000000\n"
      "  tracestate    : \n"
      "  parent_span_id: 0000000000000000\n"
      "  start         : 0\n"
      "  duration      : 0\n"
      "  description   : \n"
      "  span kind     : Internal\n"
      "  status        : Unset\n"
      "  attributes    : \n"
      "  events        : \n"
      "  links         : \n"
      "\t{\n"
      "\t  trace_id      : 02000000000000000000000000000000\n"
      "\t  span_id       : 0100000000000000\n"
      "\t  tracestate    : \n"
      "\t  attributes    : \n"
      "\t}\n"
      "\t{\n"
      "\t  trace_id      : 02000000000000000000000000000000\n"
      "\t  span_id       : 0300000000000000\n"
      "\t  tracestate    : state1=value\n"
      "\t  attributes    : \n"
      "\t\tattr1: string\n"
      "\t}\n"
      "  resources     : \n"
      "  instr-lib     : unknown_service\n"
      "}\n";
  EXPECT_EQ(output.str(), expectedOutput);
}

// Test with the three common ostreams, tests are more of a sanity check and usage examples.
TEST(OStreamSpanExporter, PrintSpanToCout)
{
  auto exporter  = std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter);
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  const auto captured =
      WithOStreamCapture(std::cout, [&]() { processor->OnEnd(std::move(recordable)); });

  EXPECT_EQ(captured, kDefaultSpanPrinted);
}

TEST(OStreamSpanExporter, PrintSpanToCerr)
{
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(std::cerr));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  const auto captured =
      WithOStreamCapture(std::cerr, [&]() { processor->OnEnd(std::move(recordable)); });

  EXPECT_EQ(captured, kDefaultSpanPrinted);
}

TEST(OStreamSpanExporter, PrintSpanToClog)
{
  auto exporter =
      std::unique_ptr<trace_sdk::SpanExporter>(new exportertrace::OStreamSpanExporter(std::clog));
  auto processor = std::shared_ptr<trace_sdk::SpanProcessor>(
      new trace_sdk::SimpleSpanProcessor(std::move(exporter)));

  auto recordable = processor->MakeRecordable();

  const auto captured =
      WithOStreamCapture(std::clog, [&]() { processor->OnEnd(std::move(recordable)); });

  EXPECT_EQ(captured, kDefaultSpanPrinted);
}