Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom route filter, header propagation, etc #3

Open
garthk opened this issue May 23, 2020 · 0 comments
Open

Custom route filter, header propagation, etc #3

garthk opened this issue May 23, 2020 · 0 comments

Comments

@garthk
Copy link
Contributor

garthk commented May 23, 2020

Splitting this one out of #1

  @doc false
  def handle_start(_, _measurements, %{conn: conn}, _config) do
    # TODO: add config for what paths are traced

Users might want to customise:

  • Which paths are traced and which aren't
  • What sample rate to apply, either through a kludge like Provide interim sampling capability in exporter garthk/opentelemetry_honeycomb#5 or some later API-blessed solution
  • How http.client_ip is derived from headers eg. they need RFC 7239 Forwarded as well as X-Forwarded-For
  • How the trace context is derived from headers eg. they need to handle Honeycomb beeline as well as traceparent
  • Their own attributes derived from the conn
  • Other stuff that hasn't occurred to me

Strikes me we could let them do all that with one config item instead of half a dozen:

  • Add a custom_start callback option
  • Expose our attributes making machinery so they can call it to get most of the heavy lifting done for free

Our code would look something like this:

  # our code
  def handle_start(_, _, %{conn: conn}, %{custom_start: custom_start}) do
    custom_start.(conn)
  rescue
    _ -> :any_plans_for_how_to_stop_one_mistake_from_turning_off_tracing_forever?
  end

  @doc "Call `OpenTelemetry.Tracer.start_span/2` with our tracer, not yours."
  def start_span(span_name, start_opts), do:
    OpenTelemetry.Tracer.start_span(span_name, start_opts)

Not hard, and lets them get as custom as they want:

  # their code
  def my_custom_start(conn, start_span) do
    if my_should_trace?(conn) do
      my_propagate_from(conn.req_headers)
      OpentelemetryPlug.start_span(span_name, %{attributes: my_attributes(conn)})
    end
  end

  defp my_should_trace?(conn) do
    # checks the method or route, whatever
  end

  defp my_propagate_from(conn) do
    # calls :ot_propagation.http_extract/1 if it sees a traceparent
    # ensures equivalent if it sees another header
  end

  defp my_attributes(conn) do
    # calls OpentelemetryPlug.default_attributes/1
    # pipes it through Map.merge/2 with some more attributes
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant