Class: Datadog::Profiling::Transport::HTTP::API::Endpoint

Inherits:
Transport::HTTP::API::Endpoint show all
Includes:
Ext::Profiling::Transport::HTTP
Defined in:
lib/ddtrace/profiling/transport/http/api/endpoint.rb

Overview

Datadog API endpoint for profiling

Constant Summary

Constants included from Ext::Profiling::Transport::HTTP

Ext::Profiling::Transport::HTTP::FORM_FIELD_FAMILY, Ext::Profiling::Transport::HTTP::FORM_FIELD_INTAKE_VERSION, Ext::Profiling::Transport::HTTP::FORM_FIELD_PPROF_DATA, Ext::Profiling::Transport::HTTP::FORM_FIELD_RECORDING_END, Ext::Profiling::Transport::HTTP::FORM_FIELD_RECORDING_START, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAGS, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_ENV, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_HOST, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_LANGUAGE, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_PID, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_PROFILER_VERSION, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_RUNTIME, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_RUNTIME_ENGINE, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_RUNTIME_ID, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_RUNTIME_PLATFORM, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_RUNTIME_VERSION, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_SERVICE, Ext::Profiling::Transport::HTTP::FORM_FIELD_TAG_VERSION, Ext::Profiling::Transport::HTTP::HEADER_CONTENT_TYPE, Ext::Profiling::Transport::HTTP::HEADER_CONTENT_TYPE_OCTET_STREAM, Ext::Profiling::Transport::HTTP::PPROF_DEFAULT_FILENAME, Ext::Profiling::Transport::HTTP::URI_TEMPLATE_DD_API

Instance Attribute Summary collapse

Attributes inherited from Transport::HTTP::API::Endpoint

#path, #verb

Instance Method Summary collapse

Constructor Details

#initialize(path, encoder) ⇒ Endpoint

Returns a new instance of Endpoint.



26
27
28
29
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 26

def initialize(path, encoder)
  super(:post, path)
  @encoder = encoder
end

Instance Attribute Details

#encoderObject (readonly)

Returns the value of attribute encoder.



23
24
25
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 23

def encoder
  @encoder
end

Instance Method Details

#build_form(env) ⇒ Object



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
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 42

def build_form(env)
  flush = env.request.parcel.data
  pprof_file = build_pprof(flush)

  form = {
    FORM_FIELD_INTAKE_VERSION => '3', # Aka 1.3 intake format
    FORM_FIELD_RECORDING_START => flush.start.utc.iso8601,
    FORM_FIELD_RECORDING_END => flush.finish.utc.iso8601,
    FORM_FIELD_TAGS => [
      "#{FORM_FIELD_TAG_RUNTIME}:#{flush.language}",
      "#{FORM_FIELD_TAG_RUNTIME_ID}:#{flush.runtime_id}",
      "#{FORM_FIELD_TAG_RUNTIME_ENGINE}:#{flush.runtime_engine}",
      "#{FORM_FIELD_TAG_RUNTIME_PLATFORM}:#{flush.runtime_platform}",
      "#{FORM_FIELD_TAG_RUNTIME_VERSION}:#{flush.runtime_version}",
      "#{FORM_FIELD_TAG_PID}:#{Process.pid}",
      "#{FORM_FIELD_TAG_PROFILER_VERSION}:#{flush.profiler_version}",
      # NOTE: Redundant w/ 'runtime'; may want to remove this later.
      "#{FORM_FIELD_TAG_LANGUAGE}:#{flush.language}",
      "#{FORM_FIELD_TAG_HOST}:#{flush.host}",
      *flush
        .tags
        .reject { |tag_key| TAGS_TO_IGNORE_IN_TAGS_HASH.include?(tag_key) }
        .map { |tag_key, tag_value| "#{tag_key}:#{tag_value}" }
    ],
    FORM_FIELD_PPROF_DATA => pprof_file,
    FORM_FIELD_FAMILY => flush.language,
  }

  # Optional fields
  form[FORM_FIELD_TAGS] << "#{FORM_FIELD_TAG_SERVICE}:#{flush.service}" unless flush.service.nil?
  form[FORM_FIELD_TAGS] << "#{FORM_FIELD_TAG_ENV}:#{flush.env}" unless flush.env.nil?
  form[FORM_FIELD_TAGS] << "#{FORM_FIELD_TAG_VERSION}:#{flush.version}" unless flush.version.nil?

  form
end

#build_pprof(flush) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 78

def build_pprof(flush)
  pprof = encoder.encode(flush)

  gzipped_pprof_data = Datadog::Utils::Compression.gzip(pprof.data)

  Datadog::Vendor::Multipart::Post::UploadIO.new(
    StringIO.new(gzipped_pprof_data),
    HEADER_CONTENT_TYPE_OCTET_STREAM,
    PPROF_DEFAULT_FILENAME
  )
end

#call(env, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 31

def call(env, &block)
  # Build request
  env.form = build_form(env)

  # Send request
  http_response = super(env, &block)

  # Build response
  Profiling::Transport::HTTP::Response.new(http_response)
end