aliyun-log 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0af7f849c9e5e9e6d59647f4b184c994977407f5b970f5d867505062401621c7
4
- data.tar.gz: bfd3fafdac4839ac11d428638d44d15a4126d283195ddd6e5b033238f3e7b034
3
+ metadata.gz: 56b9e06d07673323179dae69adadfc8a6dc8518800181af620b31dfe13e5d11d
4
+ data.tar.gz: 6d17a1f90049d299362b5fa8961b5a2befa56b93e12a07b4fb15fb65bdc914f0
5
5
  SHA512:
6
- metadata.gz: 033ae7fd59ab6e49b40c071942f289623226ca9a506f88cec0eafdce2edabcc56c5b2daecd7f7c8a6e27df39c11ebf62deab06c3d11b175c4b1f7eeef5284f5f
7
- data.tar.gz: f31dc59e00955ecc7b0f6682356005b7ea2b872527818ea352418a99ddd81a8d3b0fd4d66f7fbda9c92073d9e8d51eca38b7af11364f2bf2d359ad565ac0299b
6
+ metadata.gz: 265b850b91106ed251fbc1deda94b4f9138f4c055e703be864b99cbb156e3a90583d6da29b5f15e3a4210fd75e89470b7d5281d52573b6a0dd4d8ec6613006cb
7
+ data.tar.gz: 7aca9990743677faa1863a34f2ce75bf66c357f6f177f8330de45a58ae7280ebaf6411856746b7566c64519f579ae37f5ef4e0ebd52253cf4a222ae348e90e07
@@ -1,4 +1,5 @@
1
- #
1
+ # frozen_string_literal: true
2
+
2
3
  require_relative 'log/common'
3
4
  require_relative 'log/client'
4
5
  require_relative 'log/config'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aliyun
2
4
  module Log
3
5
  class Client
@@ -10,6 +12,10 @@ module Aliyun
10
12
  @protocol.list_projects(size, offset)
11
13
  end
12
14
 
15
+ def projects(size = nil, offset = nil)
16
+ @protocol.projects(size, offset)
17
+ end
18
+
13
19
  def get_project(name)
14
20
  @protocol.get_project(name)
15
21
  end
@@ -1,3 +1,4 @@
1
- #
1
+ # frozen_string_literal: true
2
+
2
3
  require_relative 'common/attr_struct'
3
4
  require_relative 'common/logging'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aliyun
2
4
  module Log
3
5
  module Common
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
 
3
5
  module Aliyun
4
6
  module Log
5
7
  module Common
6
8
  module Logging
7
- # DEFAULT_LOG_FILE = './log/aliyun_log.log'.freeze
8
- DEFAULT_LOG_FILE = STDOUT
9
9
  MAX_NUM_LOG = 100
10
10
  ROTATE_SIZE = 10 * 1024 * 1024
11
11
 
@@ -15,15 +15,32 @@ module Aliyun
15
15
 
16
16
  # level = Logger::DEBUG | Logger::INFO | Logger::ERROR | Logger::FATAL
17
17
  def self.log_level=(level)
18
+ @logger_level = level
18
19
  Logging.logger.level = level
19
20
  end
20
21
 
22
+ def self.logger=(logger)
23
+ @logger = logger
24
+ end
25
+
26
+ def self.log_file=(log_file)
27
+ @logger = Logger.new(
28
+ log_file, MAX_NUM_LOG, ROTATE_SIZE
29
+ )
30
+ @logger.level = Logging.logger_level
31
+ end
32
+
33
+ def self.logger_level
34
+ @logger_level ||= Config.log_level
35
+ @logger_level
36
+ end
37
+
21
38
  def self.logger
22
39
  unless @logger
23
40
  @logger = Logger.new(
24
- @log_file ||= DEFAULT_LOG_FILE, MAX_NUM_LOG, ROTATE_SIZE
41
+ @log_file ||= Config.log_file, MAX_NUM_LOG, ROTATE_SIZE
25
42
  )
26
- @logger.level = Logger::DEBUG
43
+ @logger.level = Logging.logger_level
27
44
  end
28
45
  @logger
29
46
  end
@@ -1,12 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
1
4
  module Aliyun
2
5
  module Log
3
6
  class Config < Common::AttrStruct
4
7
  @endpoint = 'https://cn-beijing.log.aliyuncs.com'
5
8
  @open_timeout = 10
6
9
  @read_timeout = 120
10
+ @log_file = 'aliyun_log.log'
11
+ @log_level = Logger::DEBUG
7
12
  class << self
8
13
  attr_accessor :endpoint, :access_key_id, :access_key_secret,
9
- :open_timeout, :read_timeout
14
+ :open_timeout, :read_timeout, :log_file, :log_level
10
15
 
11
16
  def configure
12
17
  yield self
@@ -28,16 +33,14 @@ module Aliyun
28
33
 
29
34
  private
30
35
 
31
- def normalize_endpoint
32
- uri = URI.parse(endpoint)
33
- uri = URI.parse("http://#{endpoint}") unless uri.scheme
36
+ def normalize_endpoint
37
+ uri = URI.parse(endpoint)
38
+ uri = URI.parse("http://#{endpoint}") unless uri.scheme
34
39
 
35
- if (uri.scheme != 'http') && (uri.scheme != 'https')
36
- raise 'Only HTTP and HTTPS endpoint are accepted.'
37
- end
40
+ raise 'Only HTTP and HTTPS endpoint are accepted.' if (uri.scheme != 'http') && (uri.scheme != 'https')
38
41
 
39
- @endpoint = uri.to_s
40
- end
42
+ @endpoint = uri.to_s
43
+ end
41
44
  end
42
45
  end
43
46
  end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aliyun
2
4
  module Log
3
5
  class Logstore < Common::AttrStruct
4
-
5
6
  attrs :name, :project_name, :ttl, :shared_count, :enable_tracking,
6
7
  :auto_split, :max_split_shard, :create_time, :last_modify_time
7
8
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aliyun
2
4
  module Log
3
5
  class Project < Common::AttrStruct
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'protobuf'
2
4
 
3
5
  module Aliyun
@@ -5,6 +5,7 @@ require 'json'
5
5
  module Aliyun
6
6
  module Log
7
7
  class Protocol
8
+ include Common::Logging
8
9
  def initialize(config)
9
10
  @http = Request.new(config)
10
11
  end
@@ -22,6 +23,10 @@ module Aliyun
22
23
  data
23
24
  end
24
25
 
26
+ def projects(size = nil, offset = nil)
27
+ list_projects(size, offset)['projects']
28
+ end
29
+
25
30
  def get_project(project_name)
26
31
  query = { projectName: project_name }
27
32
  attrs = @http.get({ project: project_name }, query)
@@ -171,9 +176,7 @@ module Aliyun
171
176
  }
172
177
  fields.each do |k, v|
173
178
  body[:keys][k] = v
174
- if %w[text json].include?(v[:type]) && v[:token].blank?
175
- v[:token] = INDEX_DEFAULT_TOKEN
176
- end
179
+ v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type]) && v[:token].blank?
177
180
  end
178
181
  @http.post({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
179
182
  end
@@ -187,9 +190,7 @@ module Aliyun
187
190
  }
188
191
  fields.each do |k, v|
189
192
  body[:keys][k] = v
190
- if v[:type] == 'text' && v[:token].blank?
191
- v[:token] = INDEX_DEFAULT_TOKEN
192
- end
193
+ v[:token] = INDEX_DEFAULT_TOKEN if v[:type] == 'text' && v[:token].blank?
193
194
  end
194
195
  @http.put({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
195
196
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rest-client'
2
4
  require 'base64'
3
5
  require 'openssl'
@@ -72,7 +74,6 @@ module Aliyun
72
74
  payload = Zlib::Deflate.deflate(payload.encode) if resources[:is_pb]
73
75
  request_options[:payload] = payload
74
76
  end
75
- logger.debug(request_options)
76
77
  request = RestClient::Request.new(request_options)
77
78
  response = request.execute do |resp|
78
79
  if resp.code >= 300
@@ -99,11 +100,13 @@ module Aliyun
99
100
  'User-Agent' => "aliyun-log ruby-#{RUBY_VERSION}/#{RUBY_PLATFORM}"
100
101
  }
101
102
  return headers if body.nil?
103
+
102
104
  if is_pb
103
105
  compressed = Zlib::Deflate.deflate(body.encode)
104
106
  headers['Content-Length'] = compressed.bytesize.to_s
105
107
  # 日志内容包含的日志必须小于3MB和4096条。
106
- raise 'content length is larger than 3MB' if headers['Content-Length'].to_i > 3145728
108
+ raise 'content length is larger than 3MB' if headers['Content-Length'].to_i > 3_145_728
109
+
107
110
  headers['Content-MD5'] = Digest::MD5.hexdigest(compressed).upcase
108
111
  headers['Content-Type'] = 'application/x-protobuf'
109
112
  headers['x-log-compresstype'] = 'deflate'
@@ -122,7 +125,6 @@ module Aliyun
122
125
  @config.access_key_secret,
123
126
  string_to_sign(verb, resource, headers, query).chomp
124
127
  )
125
- logger.debug "\n#{string_to_sign(verb, resource, headers, query).chomp}"
126
128
  base64_sign = Base64.strict_encode64(sha1_digest)
127
129
  "LOG #{@config.access_key_id}:#{base64_sign}"
128
130
  end
@@ -133,12 +135,12 @@ module Aliyun
133
135
  #{headers['Content-MD5']}
134
136
  #{headers['Content-Type']}
135
137
  #{headers['Date']}
136
- #{canonicalized_sls_headers(headers)}
138
+ #{canonicalized_headers(headers)}
137
139
  #{canonicalized_resource(resource, query)}
138
140
  DOC
139
141
  end
140
142
 
141
- def canonicalized_sls_headers(headers)
143
+ def canonicalized_headers(headers)
142
144
  h = {}
143
145
  headers.each do |k, v|
144
146
  h[k.downcase] = v if k =~ /x-log-.*/
@@ -151,11 +153,12 @@ module Aliyun
151
153
 
152
154
  def canonicalized_resource(resource = '', query = {})
153
155
  return resource if query.empty?
156
+
154
157
  url = URI.parse(resource)
155
- q_str = query.keys.sort.map do |e|
158
+ sort_str = query.keys.sort.map do |e|
156
159
  "#{e}=#{query[e]}"
157
160
  end.join('&')
158
- "#{url}?#{q_str}"
161
+ "#{url}?#{sort_str}"
159
162
  end
160
163
  end
161
164
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
 
3
5
  module Aliyun
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aliyun
2
4
  module Log
3
- VERSION = "0.1.0"
5
+ VERSION = '0.1.1'
4
6
  end
5
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yingce Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-04 00:00:00.000000000 Z
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protobuf
@@ -86,7 +86,7 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '3.0'
89
- description: Aliyun Log SDK for Ruby
89
+ description: Aliyun Log SDK for Ruby 阿里云日志服务(SLS) Ruby SDK, 目前仅实现基于Restfull部分接口
90
90
  email:
91
91
  - yingce@live.com
92
92
  executables: []