open-telemetry / opentelemetry-erlang-api

Erlang/Elixir OpenTelemetry API
Apache License 2.0
60 stars 13 forks source link

** (UndefinedFunctionError) function :ot_span.add_event/3 is undefined or private #48

Closed garthk closed 4 years ago

garthk commented 4 years ago

OpenTelemetry.Span.add_event/1` doesn't work.

I'm trying the example code in lib/open_telemetry.ex. Roughly adapted:

require OpenTelemetry.Tracer
require OpenTelemetry.Span

OpenTelemetry.register_application_tracer(:this_otp_app)
OpenTelemetry.Tracer.start_span("some-span")
:...
ecto_event = OpenTelemetry.event("ecto.query", [{"query", "ahem"}, {"total_time", 1}])
OpenTelemetry.Span.add_event(ecto_event)
:...
OpenTelemetry.Tracer.end_span()

The call to OpenTelemetry.Span.add_event/1 is failing because:

** (UndefinedFunctionError) function :ot_span.add_event/3 is undefined or private. Did you mean one of:

      * add_event/4

The calling code is:

  @doc """
  Add an event to the currently active Span.
  """
  @spec add_event(OpenTelemetry.event()) :: boolean()
  defmacro add_event(event) do
    quote do
      tracer = :opentelemetry.get_tracer(__MODULE__)
      :ot_span.add_event(tracer, :ot_tracer.current_span_ctx(tracer), unquote(event))
    end
  end

That argument order was present in the first version of :ot_span.add_events/3 but hasn't been seen since we switched to tracer, events, context in November.

yatender-oktalk commented 4 years ago

Yes, I also faced the same issue as existing event-based tracking code is not correct. I can give PR on this issue. Meanwhile, we can use the following code.

instead of sending the event type to OpenTelemetry.Span.add_event/1 call the custom API which directly calls the erlang API

Follow this process.

define your attributes and event_name Once you define the event_name and event_attributes

require OpenTelemetry.Tracer
require OpenTelemetry.Span

OpenTelemetry.register_application_tracer(:this_otp_app)
OpenTelemetry.Tracer.start_span("some-span")

event_attributes = [{"query", query}, {"total_time", total_time}]
event_name = "ecto.query"

add_event(event_name, event_attributes)

OpenTelemetry.Tracer.end_span()

Custom API to push the event to current span

  defp add_event(event_name, attributes) do
    tracer = :opentelemetry.get_tracer(__MODULE__)
    span_ctx = OpenTelemetry.Tracer.current_span_ctx()
    # Elixir API is having issues so directly using erlang API
    :ot_span.add_event(tracer, span_ctx, event_name, attributes)
  end
tsloughter commented 4 years ago

Thanks, not sure how I let this issue slip by me. I'll try to resolve it today.

yatender-oktalk commented 4 years ago

@tsloughter Hey I have given a PR can you have a look and tell me what all is needed in case it's having some issues.

tolbrino commented 4 years ago

@tsloughter This should be closed since its fixed in #56