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_DATA, Ext::Profiling::Transport::HTTP::FORM_FIELD_FORMAT, Ext::Profiling::Transport::HTTP::FORM_FIELD_FORMAT_PPROF, Ext::Profiling::Transport::HTTP::FORM_FIELD_RECORDING_END, Ext::Profiling::Transport::HTTP::FORM_FIELD_RECORDING_START, Ext::Profiling::Transport::HTTP::FORM_FIELD_RUNTIME, Ext::Profiling::Transport::HTTP::FORM_FIELD_RUNTIME_ID, 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::FORM_FIELD_TYPES, Ext::Profiling::Transport::HTTP::FORM_FIELD_TYPES_AUTO, 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
77
78
79
80
81
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 42

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

  form = {
    # NOTE: Redundant w/ 'runtime-id' tag below; may want to remove this later.
    FORM_FIELD_RUNTIME_ID => flush.runtime_id,
    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_DATA => pprof_file,
    FORM_FIELD_RUNTIME => flush.language,
    FORM_FIELD_FORMAT => FORM_FIELD_FORMAT_PPROF
  }

  # Add types
  form[FORM_FIELD_TYPES] = types.join(',')

  # 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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ddtrace/profiling/transport/http/api/endpoint.rb', line 83

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

  # Wrap pprof as a gzipped file
  gzipped_data = Datadog::Utils::Compression.gzip(pprof.data)
  pprof_file = Datadog::Vendor::Multipart::Post::UploadIO.new(
    StringIO.new(gzipped_data),
    HEADER_CONTENT_TYPE_OCTET_STREAM,
    PPROF_DEFAULT_FILENAME
  )

  [pprof_file, [FORM_FIELD_TYPES_AUTO]]
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