Class: Fluent::Plugin::GcloudPubSubOutput

Inherits:
Output
  • Object
show all
Includes:
Compressable, Fluent::PluginHelper::Inject
Defined in:
lib/fluent/plugin/out_gcloud_pubsub.rb

Constant Summary collapse

DEFAULT_BUFFER_TYPE =
"memory"
DEFAULT_FORMATTER_TYPE =
"json"

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 53

def configure(conf)
  compat_parameters_convert(conf, :buffer, :formatter)
  super
  placeholder_validate!(:topic, @topic)
  @formatter = formatter_create
  @compress = if @compression == 'gzip'
                method(:gzip_compress)
              else
                method(:no_compress)
              end
end

#format(tag, time, record) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 70

def format(tag, time, record)
  record = inject_values_to_record(tag, time, record)
  attributes = {}
  @attribute_keys.each do |key|
    attributes[key] = record.delete(key)
  end
  attributes.merge! @attribute_key_values
  [@compress.call(@formatter.format(tag, time, record)), attributes].to_msgpack
end

#formatted_to_msgpack_binary?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 80

def formatted_to_msgpack_binary?
  true
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 84

def multi_workers_ready?
  true
end

#startObject



65
66
67
68
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 65

def start
  super
  @publisher = Fluent::GcloudPubSub::Publisher.new @project, @key, @autocreate_topic, @dest_project, @endpoint, @timeout
end

#write(chunk) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fluent/plugin/out_gcloud_pubsub.rb', line 88

def write(chunk)
  topic = extract_placeholders(@topic, chunk.)

  messages = []
  size = 0

  chunk.msgpack_each do |msg, attr|
    msg = Fluent::GcloudPubSub::Message.new(msg, attr)
    if msg.bytesize > @max_message_size
      log.warn 'Drop a message because its size exceeds `max_message_size`', size: msg.bytesize
      next
    end
    if messages.length + 1 > @max_messages || size + msg.bytesize > @max_total_size
      publish(topic, messages)
      messages = []
      size = 0
    end
    messages << msg
    size += msg.bytesize
  end

  if messages.length > 0
    publish(topic, messages)
  end
rescue Fluent::GcloudPubSub::RetryableError => ex
  log.warn "Retryable error occurs. Fluentd will retry.", error_message: ex.to_s, error_class: ex.class.to_s
  raise ex
rescue => ex
  log.error "unexpected error", error_message: ex.to_s, error_class: ex.class.to_s
  log.error_backtrace
  raise ex
end