Class: Fluent::Plugin::AzureStorageAppendBlobOut

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_azure-storage-append-blob.rb

Constant Summary collapse

DEFAULT_FORMAT_TYPE =
'out_file'.freeze
AZURE_BLOCK_SIZE_LIMIT =
4 * 1024 * 1024 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAzureStorageAppendBlobOut

Returns a new instance of AzureStorageAppendBlobOut.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 23

def initialize
  super
  @use_msi = false

  # Storage endpoint suffixes for various environments, see https://github.com/Azure/go-autorest/blob/master/autorest/azure/environments.go
  @storage_endpoint_mapping = {
    'AZURECHINACLOUD' => 'core.chinacloudapi.cn',
    'AZUREGERMANCLOUD' => 'core.cloudapi.de',
    'AZUREPUBLICCLOUD' => 'core.windows.net',
    'AZUREUSGOVERNMENTCLOUD' => 'core.usgovcloudapi.net'
  }
end

Instance Attribute Details

#bsObject (readonly)

Returns the value of attribute bs.



64
65
66
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 64

def bs
  @bs
end

Instance Method Details

#configure(conf) ⇒ Object



66
67
68
69
70
71
72
73
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
106
107
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 66

def configure(conf)
  super

  @formatter = formatter_create

  @path_slicer = if @localtime
                   proc do |path|
                     Time.now.strftime(path)
                   end
                 else
                   proc do |path|
                     Time.now.utc.strftime(path)
                   end
                 end

  if @azure_cloud == 'AZURESTACKCLOUD'
    if @azure_storage_dns_suffix.nil?
      raise ConfigError, 'azure_storage_dns_suffix invalid, must not be empty for AZURESTACKCLOUD'
    end
  else
    @azure_storage_dns_suffix = @storage_endpoint_mapping[@azure_cloud]
    if @azure_storage_dns_suffix.nil?
      raise ConfigError, 'azure_cloud invalid, must be either of AZURECHINACLOUD, AZUREGERMANCLOUD, AZUREPUBLICCLOUD, AZUREUSGOVERNMENTCLOUD, AZURESTACKCLOUD'
    end
  end

  if (@azure_storage_access_key.nil? || @azure_storage_access_key.empty?) &&
     (@azure_storage_sas_token.nil? || @azure_storage_sas_token.empty?) &&
     (@azure_storage_connection_string.nil? || @azure_storage_connection_string.empty?)
    log.info 'Using MSI since neither of azure_storage_access_key, azure_storage_sas_token, azure_storage_connection_string was provided.'
    @use_msi = true
  elsif @azure_storage_connection_string.nil? || @azure_storage_connection_string.empty?
    raise ConfigError, 'azure_storage_account needs to be specified' if @azure_storage_account.nil?
    raise ConfigError, 'azure_container needs to be specified' if @azure_container.nil?
  end

  @blob_options = {}

  if !@compute_checksums
    @blob_options[:content_md5] = ''
  end
end

#container_exists?(container) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 241

def container_exists?(container)
  begin
    @bs.get_container_properties(container)
  rescue Azure::Core::Http::HTTPError => e
    if e.status_code == 404 # container does not exist
      return false
    else
      raise
    end
  end
  true
end

#format(tag, time, record) ⇒ Object



173
174
175
176
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 173

def format(tag, time, record)
  r = inject_values_to_record(tag, time, record)
  @formatter.format(tag, time, r)
end

#get_access_tokenObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 113

def get_access_token
  access_key_request = Faraday.new('http://169.254.169.254/metadata/identity/oauth2/token?' \
                                   "api-version=#{@azure_imds_api_version}" \
                                   '&resource=https://storage.azure.com/' \
                                   "#{!azure_msi_client_id.nil? ? "&client_id=#{azure_msi_client_id}" : ''}",
                                   headers: { 'Metadata' => 'true' }).get

  if access_key_request.status == 200
    JSON.parse(access_key_request.body)['access_token']
  else
    raise 'Access token request was not sucssessful. Possibly due to missing azure_msi_client_id config parameter.'
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 109

def multi_workers_ready?
  true
end

#startObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 127

def start
  super
  if @bs
    # nop
  elsif @use_msi
    token_credential = Azure::Storage::Common::Core::TokenCredential.new get_access_token
    token_signer = Azure::Storage::Common::Core::Auth::TokenSigner.new token_credential
    @bs = Azure::Storage::Blob::BlobService.new(
      storage_account_name: @azure_storage_account,
      storage_dns_suffix: @azure_storage_dns_suffix,
      signer: token_signer
    )

    refresh_interval = @azure_token_refresh_interval * 60
    cancelled = false
    renew_token = Thread.new do
      Thread.stop
      until cancelled
        sleep(refresh_interval)

        token_credential.renew_token get_access_token
      end
    end
    sleep 0.1 while renew_token.status != 'sleep'
    renew_token.run
  elsif !@azure_storage_connection_string.nil? && !@azure_storage_connection_string.empty?
    @bs = Azure::Storage::Blob::BlobService.create_from_connection_string(@azure_storage_connection_string)
  else
    @bs_params = { storage_account_name: @azure_storage_account, storage_dns_suffix: @azure_storage_dns_suffix }

    if !@azure_storage_access_key.nil? && !@azure_storage_access_key.empty?
      @bs_params.merge!({ storage_access_key: @azure_storage_access_key })
    elsif !@azure_storage_sas_token.nil? && !@azure_storage_sas_token.empty?
      @bs_params.merge!({ storage_sas_token: @azure_storage_sas_token })
    end

    @bs = Azure::Storage::Blob::BlobService.create(@bs_params)

  end

  ensure_container
  @azure_storage_path = ''
  @last_azure_storage_path = ''
  @current_index = 0
end

#write(chunk) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 213

def write(chunk)
  tmp = Tempfile.new('azure-')
  begin
    if @compress
      write_compress(chunk, tmp)
    else
      chunk.write_to(tmp)
    end

    generate_log_name(chunk, @current_index)
    if @last_azure_storage_path != @azure_storage_path
      @current_index = 0
      generate_log_name(chunk, @current_index)
    end

    content = File.open(tmp.path, 'rb', &:read)

    append_blob(content, chunk)
    @last_azure_storage_path = @azure_storage_path
  ensure
    begin
      tmp.close(true)
    rescue StandardError
      nil
    end
  end
end

#write_compress(chunk, tmp) ⇒ Object



178
179
180
181
182
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 178

def write_compress(chunk, tmp)
  tmp.binmode
  write_compress_helper(chunk, tmp)
  tmp.rewind
end

#write_compress_helper(chunk, tmp) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/fluent/plugin/out_azure-storage-append-blob.rb', line 185

def write_compress_helper(chunk, tmp)
  chunk_is_file = @buffer_type == 'file'
  path = if chunk_is_file
           chunk.path
         else
           w = Tempfile.new("chunk-gzip-tmp")
           w.binmode
           chunk.write_to(w)
           w.close
           w.path
         end

  res = system "gzip -c #{path} > #{tmp.path}"
  unless res
    log.warn "failed to execute gzip command. Fallback to GzipWriter. status = #{$?}"
    begin
      tmp.truncate(0)
      gw = Zlib::GzipWriter.new(tmp)
      chunk.write_to(gw)
      gw.close
    ensure
      gw.close rescue nil
    end
  end
ensure
  w.close(true) rescue nil
end