|
1 | 1 | require "./spec_helper" |
2 | 2 | require "../src/opentelemetry-instrumentation/log_backend" |
3 | 3 |
|
4 | | -class Log |
5 | | - class AsyncDispatcher |
6 | | - def initialize(buffer_size = 2048) |
7 | | - puts "AsyncDispatcher.initialize" |
8 | | - previous_def |
9 | | - # @channel = Channel({Entry, Backend}).new(buffer_size) |
10 | | - # @done = Channel(Nil).new |
11 | | - # spawn write_logs |
12 | | - end |
13 | | - |
14 | | - private def write_logs |
15 | | - while msg = @channel.receive? |
16 | | - entry, backend = msg |
17 | | - pp entry |
18 | | - pp backend |
19 | | - pp Fiber.current.current_span |
20 | | - backend.write(entry) |
21 | | - end |
22 | | - |
23 | | - @done.send nil |
24 | | - end |
25 | | - end |
26 | | -end |
27 | | - |
28 | 4 | describe OpenTelemetry::Instrumentation::LogBackend, tags: ["LogBackend"] do |
29 | | - it "should add the log to the existing span" do |
30 | | - checkout_config do |
31 | | - memory = IO::Memory.new |
32 | | - OpenTelemetry.configure do |config| |
33 | | - config.service_name = "Crystal OTel Instrumentation - OpenTelemetry::Instrumentation::LogBackend" |
34 | | - config.service_version = "1.0.0" |
35 | | - config.exporter = OpenTelemetry::Exporter.new(variant: :io, io: memory) |
36 | | - end |
| 5 | + if Spec.tags.try(&.includes?("LogBackend")) |
| 6 | + it "should add the log to the existing span" do |
| 7 | + checkout_config do |
| 8 | + memory = IO::Memory.new |
| 9 | + OpenTelemetry.configure do |config| |
| 10 | + config.service_name = "Crystal OTel Instrumentation - OpenTelemetry::Instrumentation::LogBackend" |
| 11 | + config.service_version = "1.0.0" |
| 12 | + config.exporter = OpenTelemetry::Exporter.new(variant: :io, io: memory) |
| 13 | + end |
37 | 14 |
|
38 | | - random_source = Random::DEFAULT.base64 |
39 | | - ::Log.setup(sources: random_source, level: Log::Severity::Trace, backend: OpenTelemetry::Instrumentation::LogBackend.new) |
| 15 | + random_source = Random::DEFAULT.base64 |
| 16 | + ::Log.setup(sources: random_source, level: Log::Severity::Trace, backend: OpenTelemetry::Instrumentation::LogBackend.new) |
40 | 17 |
|
41 | | - trace = OpenTelemetry.trace |
42 | | - trace.in_span("Fake Span") do |_span| |
43 | | - exception = Exception.new("Oh no!") |
44 | | - ::Log.with_context do |
45 | | - ::Log.context.set(context: 42) |
46 | | - ::Log.context.set(fiber: Fiber.current.current_span.object_id.to_s) |
47 | | - ::Log.for(random_source).warn(exception: exception, &.emit("Oh no!", data: "stuff")) |
| 18 | + OpenTelemetry.trace.in_span("Fake Span") do |_span| |
| 19 | + exception = Exception.new("Oh no!") |
| 20 | + ::Log.with_context do |
| 21 | + ::Log.context.set(context: 42) |
| 22 | + ::Log.context.set(fiber: Fiber.current.current_span.object_id.to_s) |
| 23 | + ::Log.for(random_source).warn(exception: exception, &.emit("Oh no!", data: "stuff")) |
| 24 | + end |
48 | 25 | end |
49 | | - end |
50 | 26 |
|
51 | | - client_traces, server_traces = FindJson.from_io(memory) |
| 27 | + _client_traces, server_traces = FindJson.from_io(memory) |
52 | 28 |
|
53 | | - pp client_traces, server_traces |
54 | | - # The following only works if the regular logging is turned on, NOT the log_backend based logging. |
55 | | - # traces[0]["spans"][0]["kind"].should eq 1 |
56 | | - # traces[0]["spans"][0]["name"].should eq "Fake Span" |
57 | | - # traces[0]["spans"][0]["events"][0]["attributes"]["data"].should eq "stuff" |
58 | | - # traces[0]["spans"][0]["events"][0]["attributes"]["context"].should eq 42 |
| 29 | + server_traces[0]["spans"][0]["kind"].should eq 1 |
| 30 | + server_traces[0]["spans"][0]["name"].should eq "Fake Span" |
| 31 | + server_traces[0]["spans"][0]["events"][0]["attributes"]["data"].should eq "stuff" |
| 32 | + server_traces[0]["spans"][0]["events"][0]["attributes"]["context"].should eq 42 |
| 33 | + end |
59 | 34 | end |
60 | 35 | end |
61 | 36 | end |
0 commit comments