Skip to content

Commit 2b4477f

Browse files
committed
Starting to prep for a new release that uses the SDK.
1 parent 79c78ed commit 2b4477f

File tree

7 files changed

+46
-62
lines changed

7 files changed

+46
-62
lines changed

shard.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ license: "Apache 2.0"
1111
dependencies:
1212
tracer:
1313
github: wyhaines/tracer.cr
14-
opentelemetry-api:
15-
github: wyhaines/opentelemetry-api.cr
14+
opentelemetry-sdk:
15+
github: wyhaines/opentelemetry-sdk.cr
16+
branch: main
1617
defined:
1718
github: wyhaines/defined.cr
1819

spec/instrumentation/crystal_log_spec.cr

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe Log, tags: "Log" do
1515
end
1616

1717
it "can log as normal and send attach the log as an event to a span" do
18+
checkout_config do
1819
memory = IO::Memory.new
1920
OpenTelemetry.configure do |config|
2021
config.service_name = "Crystal OTel Instrumentation - HTTP::Server"
@@ -25,16 +26,16 @@ describe Log, tags: "Log" do
2526
message = "I am a message, and there is an active span."
2627
Log.setup(:info, backend)
2728

28-
trace = OpenTelemetry.trace
29-
trace.in_span("logging test span") do |_span|
29+
OpenTelemetry.trace.in_span("logging test span") do |_span|
3030
Log.info { message }
3131
end
3232

3333
backend.entries.first.message.should eq message
34-
json = JSON.parse(memory.rewind.gets_to_end)
35-
36-
json["spans"][0]["name"].should eq "logging test span"
37-
json["spans"][0]["events"][0]["name"].should eq "Log.INFO"
38-
json["spans"][0]["events"][0]["attributes"]["message"].as_s.should contain(message)
34+
_client_traces, server_traces = FindJson.from_io(memory)
35+
server_traces[0]["spans"][0]["name"].should eq "logging test span"
36+
server_traces[0]["spans"][0]["events"].size.should be > 0
37+
server_traces[0]["spans"][0]["events"][0]["name"].should eq "Log.INFO"
38+
server_traces[0]["spans"][0]["events"][0]["attributes"]["message"].as_s.should contain(message)
3939
end
4040
end
41+
end

spec/log_backend_spec.cr

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,36 @@
11
require "./spec_helper"
22
require "../src/opentelemetry-instrumentation/log_backend"
33

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-
284
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
3714

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)
4017

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
4825
end
49-
end
5026

51-
client_traces, server_traces = FindJson.from_io(memory)
27+
_client_traces, server_traces = FindJson.from_io(memory)
5228

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
5934
end
6035
end
6136
end

spec/spec_helper.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class FindJson
8484
end
8585
end
8686

87+
module Spec
88+
# Make the CLI specified tags actually accessible.
89+
class_getter tags : Set(String)? = nil
90+
end
91+
8792
# This helper macro can be used to selectively run only specific specs by turning on their
8893
# focus in response to an environment variable.
8994
macro it_may_focus_and_it(description, tags = "", &block)

src/opentelemetry-instrumentation.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "defined"
2-
require "opentelemetry-api"
2+
require "opentelemetry-sdk"
33
require "tracer"
44
require "./ext/*"
55
require "./opentelemetry-instrumentation/log_backend"

src/opentelemetry-instrumentation/log_backend.cr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "opentelemetry-api"
1+
require "opentelemetry-sdk"
22
require "log"
33
require "log/backend"
44

@@ -13,6 +13,10 @@ require "log/backend"
1313
# Log.builder.bind("*", Log::Severity::Warn, OpenTelemetry::Instrumentation::LogBackend.new)
1414
# ```
1515
class OpenTelemetry::Instrumentation::LogBackend < ::Log::Backend
16+
def initialize(dispatch_mode = nil)
17+
@dispatcher = ::Log::Dispatcher.for(:sync) # There is no reason for it to ever be anything but sync.
18+
end
19+
1620
def self.apply_log_entry(entry, event)
1721
entry.context.each do |key, value|
1822
if (raw = value.raw).is_a?(ValueTypes)
@@ -39,7 +43,6 @@ class OpenTelemetry::Instrumentation::LogBackend < ::Log::Backend
3943

4044
def write(entry : ::Log::Entry)
4145
if (span = OpenTelemetry::Trace.current_span)
42-
puts "<><><><><><><><><><><><><><><><><><><>"
4346
span.add_event("Log.#{entry.severity.label}#{" - #{entry.source}" unless entry.source.empty?}") do |event|
4447
self.class.apply_log_entry(entry, event)
4548
end

src/opentelemetry/instrumentation/crystal/http_server.cr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ unless_disabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
169169
response.version = request.version
170170
response.headers["Connection"] = "keep-alive" if request.keep_alive?
171171
context = Context.new(request, response)
172-
puts ">> #{context.object_id.to_s(16)}"
173172

174173
if span
175174
span["http.host"] = request.hostname.to_s

0 commit comments

Comments
 (0)