Class: LogStash::Outputs::Application_insights::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/application_insights/config.rb

Constant Summary collapse

@@configuration =
{}
@@masked_configuration =
{}

Class Method Summary collapse

Class Method Details

.currentObject



30
31
32
# File 'lib/logstash/outputs/application_insights/config.rb', line 30

def self.current
  @@configuration
end

.mask_configuration(configuration) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/logstash/outputs/application_insights/config.rb', line 227

def self.mask_configuration ( configuration )
  masked_configuration = configuration.dup
   = masked_configuration[:storage_account_name_key]
   = .map { |pair|
    ( name, keys ) = pair
    masked_keys = keys.map { |key| "*****************" }
    [ name, masked_keys ]
  }
  masked_configuration.each_pair do |key, value|
    masked_configuration[key] = "nil" if value.nil?
  end
  masked_configuration[:storage_account_name_key] = 
  masked_configuration
end

.masked_currentObject



34
35
36
# File 'lib/logstash/outputs/application_insights/config.rb', line 34

def self.masked_current
  @@masked_configuration
end

.symbolize_table_properties(properties) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/logstash/outputs/application_insights/config.rb', line 242

def self.symbolize_table_properties ( properties )
  new_properties = {}
  properties.each_pair { |property_name, property_value|
    new_properties[property_name.to_sym] = property_value
  }
  new_properties
end

.validate_and_adjust_configuration(configuration) ⇒ Object



38
39
40
41
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
172
173
174
175
176
177
178
179
180
181
182
183
184
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/logstash/outputs/application_insights/config.rb', line 38

def self.validate_and_adjust_configuration ( configuration )
  configuration.each_pair { |config_name, config_value|
    raise ConfigurationError, "#{config_name.to_s} must be defined" unless config_value || BOOLEAN_PROPERTIES.include?( config_name )
    case config_name

    when :logger_level
      logger_level = validate_and_adjust( config_name, config_value, String )
      raise ConfigurationError, "#{config_name.to_s} can be set to only one of the valid log levels" unless LOGGER_LEVEL_MAP[logger_level.upcase.to_sym]
      configuration[config_name] = LOGGER_LEVEL_MAP[logger_level.upcase.to_sym]

    when :logger_files
      config_value = [ config_value ] if config_value.is_a?( String )
      logger_files = validate_and_adjust( config_name, config_value, Array )

      i = 0
      logger_files.map! do |file_name|
        file_name = validate_and_adjust( "#{config_name}[#{i}]", file_name, String )
        i += 1
        if "stdout" == file_name.downcase
          file_name = :stdout
          file = STDOUT
        elsif "stderr" == file_name.downcase
          file_name = :stderr
          file = STDERR
        else
          begin
            file = ::File.open( file_name, "a+" )
          rescue => e
            raise ConfigurationError, "#{config_name}[#{i}] cannot open file #{file_name}, due to error #{e.inspect}"
          end
        end
        [ file_name, file ]
      end
      configuration[config_name] = logger_files

    when :logger_progname
      configuration[config_name] = validate_and_adjust( config_name, config_value, String )

    when :logger_shift_size
      configuration[config_name] = validate_and_adjust_integer( config_name, config_value, MIN_LOGGER_SHIFT_SIZE, MAX_LOGGER_SHIFT_SIZE )

    when :logger_shift_age
      if config_value.is_a?( String )
        config_value = validate_and_adjust( config_name, config_value, String ).downcase
        raise ConfigurationError, "#{config_name.to_s} if string, can be set to only one of the following values: #{VALID_LOGGER_SHIFT_AGES}" unless VALID_LOGGER_SHIFT_AGES. include?( config_value )
      elsif config_value.is_a?( Integer )
        config_value = validate_and_adjust_integer( config_name, config_value, MIN_LOGGER_SHIFT_AGE, MAX_LOGGER_SHIFT_AGE )
      else
        raise ConfigurationError, "#{config_name.to_s} must be either a string or integer"
      end
      configuration[config_name] = config_value

    when :azure_storage_container_prefix
      unless config_value.empty?
        azure_storage_container_prefix = validate_and_adjust( config_name, config_value, String )
        len = 63 - "#{AZURE_STORAGE_CONTAINER_LOGSTASH_PREFIX}--yyyy-mm-dd".length
        validate_max( "azure_storage_container_prefix length", azure_storage_container_prefix.length, len )
        azure_storage_container_prefix = azure_storage_container_prefix.downcase
        container_name = "#{AZURE_STORAGE_CONTAINER_LOGSTASH_PREFIX}-#{azure_storage_container_prefix}-yyyy-mm-dd"
        raise ConfigurationError, "#{config_name.to_s} must have only alphanumeric characters and dash, cannot start or end with a dash, and a dash cannot follow a dash" unless Utils.valid_container_name?( container_name )
        configuration[config_name] = "-#{azure_storage_container_prefix}"
      end

    when :azure_storage_table_prefix
      unless config_value.empty?
        azure_storage_table_prefix = validate_and_adjust( config_name, config_value, String )
        len = 63 - "#{AZURE_STORAGE_TABLE_LOGSTASH_PREFIX}yyyymmdd".length
        validate_max( "azure_storage_table_prefix length", azure_storage_table_prefix.length, len )
        table_name = "#{AZURE_STORAGE_TABLE_LOGSTASH_PREFIX}#{azure_storage_table_prefix}yyyymmdd"
        raise ConfigurationError, "#{config_name} must have only alphanumeric" unless Utils.valid_table_name?( table_name )
        configuration[config_name] = azure_storage_table_prefix
      end

    when :ca_file
      config_value = validate_and_adjust( config_name, config_value, String )
      unless config_value.empty?
        raise ConfigurationError, "#{config_name} must have a valid path name" unless Utils.valid_file_path?( config_value )
      end
      configuration[config_name] = validate_and_adjust( config_name, config_value, String )

    when :azure_storage_host_suffix
      config_value = validate_and_adjust( config_name, config_value, String )
      unless config_value.empty?
        raise ConfigurationError, "#{config_name} must have a valid host DNS address" unless Utils.dns_address?( config_value )
      end
      configuration[config_name] = validate_and_adjust( config_name, config_value, String )

    when :azure_storage_blob_prefix
      unless config_value.empty?
        azure_storage_blob_prefix = validate_and_adjust( config_name, config_value, String )
        len = 1024 - "#{AZURE_STORAGE_BLOB_LOGSTASH_PREFIX}//ikey-#{INSTRUMENTATION_KEY_TEMPLATE}/table-#{TABLE_ID_TEMPLATE}/yyyy-mm-dd-HH-MM-SS-LLL_0000.json".length
        validate_max( "azure_storage_blob_prefix length", azure_storage_blob_prefix.length, len )
        raise ConfigurationError, "#{config_name.to_s} doesn't meet url format" unless Utils.url?( "http://storage/container/#{azure_storage_blob_prefix}_ikey-#{INSTRUMENTATION_KEY_TEMPLATE}_table-#{TABLE_ID_TEMPLATE}.json" )
        configuration[config_name] = "/#{azure_storage_blob_prefix}"
      end

    when :table_id
      configuration[config_name] = validate_and_adjust_guid( config_name, config_value )

    when :blob_max_bytesize
     configuration[config_name] = validate_and_adjust_integer( config_name, config_value, MIN_BLOB_MAX_BYTESIZE, MAX_BLOB_MAX_BYTESIZE )

    when :blob_max_events
     configuration[config_name] = validate_and_adjust_integer( config_name, config_value, MIN_BLOB_MAX_EVENTS, MAX_BLOB_MAX_EVENTS )

    when :blob_retention_time
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_BLOB_RETENTION_TIME, MAX_BLOB_RETENTION_TIME )

    when :blob_access_expiry_time
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_BLOB_ACCESS_EXPIRY_TIME, MAX_BLOB_ACCESS_EXPIRY_TIME )

    when :resurrect_delay
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_STORAGE_RESURRECT_DELAY, MAX_STORAGE_RESURRECT_DELAY )

    when :flow_control_suspend_bytes
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_FLOW_CONTROL_SUSPEND_BYTES, MAX_FLOW_CONTROL_SUSPEND_BYTES )

    when :flow_control_resume_bytes
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_FLOW_CONTROL_RESUME_BYTES, MAX_FLOW_CONTROL_RESUME_BYTES )

    when :flow_control_delay
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_FLOW_CONTROL_DELAY, MAX_FLOW_CONTROL_DELAY )

    when :io_retry_delay
      configuration[config_name] = validate_and_adjust_number( config_name, config_value, MIN_IO_RETRY_DELAY, MAX_IO_RETRY_DELAY )

    when :io_max_retries
      configuration[config_name] = validate_and_adjust_integer( config_name, config_value, MIN_IO_MAX_RETRIES, MAX_IO_MAX_RETRIES )

    when :storage_account_name_key
      config_value = validate_and_adjust( config_name, config_value, Array )
      if config_value.empty?
        raise ConfigurationError, "#{config_name} is empty, at least one storage account name should be defined" unless ENV['AZURE_STORAGE_ACCOUNT']
        raise ConfigurationError, "#{config_name} is empty, at least one storage account access key should be defined" unless ENV['AZURE_STORAGE_ACCESS_KEY']
        config_value = [ ENV['AZURE_STORAGE_ACCOUNT'], ENV['AZURE_STORAGE_ACCESS_KEY'] ]
      end

       = validate_and_adjust( config_name, config_value, Array, :disallow_empty )
      unless [0].is_a?( Array )
        raise ConfigurationError, "#{config_name} property is empty, should contain at least one pair: account_name, key" unless 2 == .length
         = [ [ [0], [1] ]]
      end

      index = 0
      .map! { |pair|
        pair = validate_and_adjust( "#{config_name}[#{index}]", pair, Array, :disallow_empty )
        raise ConfigurationError, "#{config_name}[#{index}] must have two items" unless 2 == pair.length

        ( name, keys ) = pair
        name = validate_and_adjust( "#{config_name}[#{index}]:name", name, String, :disallow_empty )
        raise ConfigurationError, "#{config_name}[#{index}]:name must between 3 to 24 characters" unless (name.length >= 3) && (name.length <= 24)
        raise ConfigurationError, "##{config_name}[#{index}]:name bad format, must have only alphanumeric characters" unless Utils.alphanumeric?( name )

        keys = [ keys ] if keys.is_a?( String )
        keys = validate_and_adjust( "#{config_name}[#{index}]:keys", keys, Array, :disallow_empty )
        keys.each_index do |i|
          key = validate_and_adjust( "#{config_name}[#{index}:keys[#{i}]", keys[i], String, :disallow_empty )
          raise ConfigurationError, "#{config_name}[#{index}:keys[#{i}] must have only valid base64 characters" unless Utils.base64?( key )
        end
        index += 1
        [ name.downcase, keys ]
      }
      configuration[config_name] = 

    when :tables
      tables = validate_and_adjust( config_name, config_value, Hash )
      tables.each_pair { |table_id, properties|
        table_id = validate_and_adjust_guid( "#{config_name}:table_id", table_id )
        info = "#{config_name}[#{table_id}]"
        properties = validate_and_adjust( info, properties, Hash )
        properties = Utils.symbolize_hash_keys( properties )
        validate_and_adjust_table_properties!( properties, configuration, info )
        configuration[config_name][table_id] = properties
      }
    end
  }
  validate_and_adjust_table_properties!( configuration, configuration )

  configuration[:state_table_name] = AZURE_STORAGE_TABLE_LOGSTASH_PREFIX + configuration[:azure_storage_table_prefix] + STATE_TABLE_NAME
  configuration[:test_storage_container] = AZURE_STORAGE_CONTAINER_LOGSTASH_PREFIX + configuration[:azure_storage_container_prefix] + "-" + STORAGE_TEST_CONTAINER_NAME
  configuration[:test_storage_table] = AZURE_STORAGE_TABLE_LOGSTASH_PREFIX + configuration[:azure_storage_table_prefix] + STORAGE_TEST_TABLE_NAME
  configuration[:partition_key_prefix] = configuration[:azure_storage_blob_prefix].gsub( "/", "" )
  configuration[:local_file_prefix] = LOCAL_FS_FILE_PREFIX + configuration[:azure_storage_blob_prefix].gsub( "/", "_" )

  @@masked_configuration = mask_configuration( configuration )

  @@configuration = configuration
end

.validate_and_adjust_table_properties!(properties, configuration, base_info = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/logstash/outputs/application_insights/config.rb', line 250

def self.validate_and_adjust_table_properties! ( properties, configuration, base_info = nil )

  properties.each_pair { |property_name, property_value|
    info = ( base_info.nil? ? "#{property_name}" : "#{base_info}[#{property_name}]" )
    raise ConfigurationError, "#{info} must be defined" unless property_value || BOOLEAN_PROPERTIES.include?( property_name )

    case property_name.downcase

    when :instrumentation_key
      properties[:instrumentation_key] = validate_and_adjust_guid( info, property_value )
    when :blob_serialization
      property_value = property_value.downcase
      raise ConfigurationError, "#{info}, can be set to only one of the following values: #{VALID_EXT_EVENT_FORMAT}" unless VALID_EXT_EVENT_FORMAT.include?( property_value )
      properties[:blob_serialization] = validate_and_adjust_ext( info ,property_value, configuration[:azure_storage_blob_prefix] ) # be careful depends on order
    when :csv_default_value
      properties[:csv_default_value] = validate_and_adjust( info, property_value, String )
    when :csv_separator
      properties[:csv_separator] = validate_and_adjust( info, property_value, String )
    when :blob_max_delay
      properties[:blob_max_delay] = validate_and_adjust_number( info, property_value, MIN_BLOB_MAX_DELAY, MAX_BLOB_MAX_DELAY )
    when :event_separator
      properties[:event_separator] = validate_and_adjust( info, property_value, String )
    when :serialized_event_field
      properties[:serialized_event_field] = ( property_value.nil? ? nil : validate_and_adjust( info, property_value, String ) )
    when :case_insensitive_columns
      properties[:case_insensitive_columns] = validate_and_adjust_boolean( info, property_value )
    when :table_columns
      if property_value.nil?
        properties[:table_columns] = property_value
      else
        table_columns = property_value
        table_columns = [ table_columns ] if table_columns.is_a?( String )
        table_columns = validate_and_adjust( info, table_columns, Array)
        new_table_columns = []
        index = 0
        table_columns.each do |column|
          new_column = {}

          column = { COLUMN_PROPERTY_NAME => column } if column.is_a?( String )
          column = validate_and_adjust( "#{info}[#{index}]", column, Hash, :disallow_empty )
          raise ConfigurationError, "#{info}[#{index}][#{COLUMN_PROPERTY_NAME}] must be defined" unless column[COLUMN_PROPERTY_NAME]
          new_column[:name] = validate_and_adjust( "#{info}[#{index}][#{COLUMN_PROPERTY_NAME}]", column[COLUMN_PROPERTY_NAME], String )

          if column[COLUMN_PROPERTY_DEFAULT]
            new_column[:default] = validate_and_adjust( "#{info}[#{index}][#{COLUMN_PROPERTY_DEFAULT}]", column[COLUMN_PROPERTY_DEFAULT], String )
          end

          if column[COLUMN_PROPERTY_TYPE]
            new_column[:type] = validate_and_adjust( "#{info}[#{index}][#{COLUMN_PROPERTY_TYPE}]", column[COLUMN_PROPERTY_TYPE], String ).downcase
            raise ConfigurationError, "#{info}[#{index}][#{COLUMN_PROPERTY_TYPE}] can be only one of the following values: #{VALID_FIELDS_MAP_TYPES}" unless VALID_FIELDS_MAP_TYPES.any? {|type| type == new_column[:type]}
            new_column[:type] = new_column[:type].to_sym
          end
          new_table_columns << new_column
          index += 1
        end
        properties[:table_columns] = new_table_columns
      end

    else
    end
  }
end