Class: Karafka::Web::Pro::Ui::Controllers::Explorer::MessagesController

Inherits:
BaseController show all
Defined in:
lib/karafka/web/pro/ui/controllers/explorer/messages_controller.rb

Overview

Controller for working with messages While part of messages operations is done via explorer (exploring), this controller handles other cases not related to viewing data

Constant Summary

Constants inherited from Ui::Controllers::BaseController

Ui::Controllers::BaseController::Models

Instance Attribute Summary

Attributes inherited from Ui::Controllers::BaseController

#params, #session

Instance Method Summary collapse

Methods inherited from Ui::Controllers::BaseController

#cache, #initialize

Methods included from Ui::Controllers::Requests::Hookable

included, #run_after_hooks, #run_before_hooks

Constructor Details

This class inherits a constructor from Karafka::Web::Ui::Controllers::BaseController

Instance Method Details

#download(topic_id, partition_id, offset) ⇒ Object

Dispatches the message raw payload to the browser as a file

Parameters:

  • topic_id (String)
  • partition_id (Integer)
  • offset (Integer)

    offset of the message we want to download



112
113
114
115
116
117
118
119
120
121
# File 'lib/karafka/web/pro/ui/controllers/explorer/messages_controller.rb', line 112

def download(topic_id, partition_id, offset)
  message = Models::Message.find(topic_id, partition_id, offset)

  deny! unless visibility_filter.download?(message)

  file(
    message.raw_payload,
    "#{topic_id}_#{partition_id}_#{offset}_payload.msg"
  )
end

#export(topic_id, partition_id, offset) ⇒ Object

Dispatches the message payload first deserialized and then serialized to JSON It differs from the raw payload in cases where raw payload is compressed or binary or contains data that the Web UI user should not see that was altered on the Web UI with the visibility filter.

Parameters:

  • topic_id (String)
  • partition_id (Integer)
  • offset (Integer)

    offset of the message we want to export



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/karafka/web/pro/ui/controllers/explorer/messages_controller.rb', line 131

def export(topic_id, partition_id, offset)
  Lib::PatternsDetector.new.call

  message = Models::Message.find(topic_id, partition_id, offset)

  # Check if exports are allowed
  deny! unless visibility_filter.export?(message)

  # Payload may deserialize correctly but still not be serializable back to JSON,
  # for example when it contains non-UTF-8 byte sequences. In such cases there is
  # no JSON representation that could be exported
  payload_json = Lib::SafeRunner.new { message.payload.to_json }.tap(&:call)

  not_found!(topic_id) unless payload_json.success?

  file(
    payload_json.result,
    "#{topic_id}_#{partition_id}_#{offset}_payload.json"
  )
end

#forward(topic_id, partition_id, offset) ⇒ Object

Renders a form allowing for piping a message to a different topic

Parameters:

  • topic_id (String)
  • partition_id (Integer)
  • offset (Integer)

    offset of the message we want to republish



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/karafka/web/pro/ui/controllers/explorer/messages_controller.rb', line 46

def forward(topic_id, partition_id, offset)
  @message = Models::Message.find(topic_id, partition_id, offset)

  deny! unless visibility_filter.republish?(@message)

  @topic_id = topic_id
  @partition_id = partition_id
  @offset = offset

  @target_topic = @topic_id
  @target_partition = @partition_id

  @topics = Models::ClusterInfo
    .topics
    .sort_by { |topic| topic[:topic_name] }

  unless ::Karafka::Web.config.ui.visibility.internal_topics
    @topics.reject! { |topic| topic[:topic_name].start_with?("__") }
  end

  render
end

#republish(topic_id, partition_id, offset) ⇒ Object

Takes a requested message content and republishes it again

Parameters:

  • topic_id (String)
  • partition_id (Integer)
  • offset (Integer)

    offset of the message we want to republish



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/karafka/web/pro/ui/controllers/explorer/messages_controller.rb', line 74

def republish(topic_id, partition_id, offset)
  forward(topic_id, partition_id, offset)

  dispatch_message = {
    topic: params.str(:target_topic),
    payload: @message.raw_payload,
    headers: @message.headers.dup,
    key: @message.key
  }

  # Add target partition only if it was requested, otherwise it will use either the
  # message key (if present) or will jut round-robin
  unless params.fetch(:target_partition).empty?
    dispatch_message[:partition] = params.int(:target_partition)
  end

  # Include source headers for enhanced debuggability
  if params.bool(:include_source_headers)
    dispatch_message[:headers].merge!(
      "source_topic" => @message.topic,
      "source_partition" => @message.partition.to_s,
      "source_offset" => @message.offset.to_s
    )
  end

  delivery = ::Karafka::Web.producer.produce_sync(dispatch_message)

  redirect(
    :previous,
    success: republished(@message, delivery)
  )
end