Class: Aliyun::Log::Protocol

Inherits:
Object
  • Object
show all
Includes:
Common::Logging
Defined in:
lib/aliyun/log/protocol.rb

Constant Summary collapse

DEFAULT_LOG_TIME_SHIFT =
900
INDEX_DEFAULT_TOKEN =
", '\";=()[]{}?@&<>/:\n\t\r".split('')

Constants included from Common::Logging

Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE

Instance Method Summary collapse

Methods included from Common::Logging

log_file=, log_level=, #logger, logger, logger=, logger_level

Constructor Details

#initialize(config) ⇒ Protocol

Returns a new instance of Protocol.



9
10
11
# File 'lib/aliyun/log/protocol.rb', line 9

def initialize(config)
  @http = Request.new(config)
end

Instance Method Details

#build_log_pb(attrs, time = Time.now.to_i) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/aliyun/log/protocol.rb', line 108

def build_log_pb(attrs, time = Time.now.to_i)
  logs = attrs.is_a?(Array) ? attrs : [attrs]
  logs.map do |log_attr|
    contents = log_attr.map { |k, v| { key: k, value: v.to_s } }
    Protobuf::Log.new(time: time, contents: contents)
  end
end

#create_index(project_name, logstore_name, fields) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/aliyun/log/protocol.rb', line 177

def create_index(project_name, logstore_name, fields)
  body = {
    line: {
      token: INDEX_DEFAULT_TOKEN
    },
    keys: {}
  }
  fields.each do |k, v|
    v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].nil?
    body[:keys][k] = v
  end
  @http.post({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
end

#create_index_line(project_name, logstore_name, token = nil) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/aliyun/log/protocol.rb', line 168

def create_index_line(project_name, logstore_name, token = nil)
  body = {
    line: {
      token: token || INDEX_DEFAULT_TOKEN
    }
  }
  @http.post({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
end

#create_logstore(project_name, logstore_name, opt = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aliyun/log/protocol.rb', line 67

def create_logstore(project_name, logstore_name, opt = {})
  body = {
    logstoreName: logstore_name,
    ttl: opt[:ttl] || 365,
    shardCount: opt[:shard_count] || 2,
    autoSplit: opt[:auto_split].nil? ? false : opt[:auto_split],
    maxSplitShard: opt[:max_split_shard],
    enable_tracking: opt[:enable_tracking].nil? ? false : opt[:enable_tracking]
  }.compact
  @http.post({ project: project_name, logstore: '' }, body.to_json)
end

#create_project(project_name, desc) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/aliyun/log/protocol.rb', line 37

def create_project(project_name, desc)
  body = {
    projectName: project_name,
    description: desc
  }
  @http.post({ project: project_name }, body.to_json)
end

#delete_index(project_name, logstore_name) ⇒ Object



205
206
207
208
# File 'lib/aliyun/log/protocol.rb', line 205

def delete_index(project_name, logstore_name)
  body = { logstore_name: logstore_name }
  @http.delete({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
end

#delete_logstore(project_name, logstore_name) ⇒ Object



91
92
93
94
# File 'lib/aliyun/log/protocol.rb', line 91

def delete_logstore(project_name, logstore_name)
  body = { logstore_name: logstore_name }
  @http.delete({ project: project_name, logstore: logstore_name }, body.to_json)
end

#delete_project(project_name) ⇒ Object



53
54
55
56
# File 'lib/aliyun/log/protocol.rb', line 53

def delete_project(project_name)
  body = { projectName: project_name }
  @http.delete({ project: project_name }, body.to_json)
end

#get_histograms(project_name, logstore_name, opt = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/aliyun/log/protocol.rb', line 140

def get_histograms(project_name, logstore_name, opt = {})
  from = opt[:from] || (Time.now - DEFAULT_LOG_TIME_SHIFT).to_i
  to = opt[:to] || Time.now.to_i
  query = {
    type: 'histogram',
    from: from,
    to: to,
    query: opt[:query],
    topic: opt[:topic]
  }.compact
  @http.get({ project: project_name, logstore: logstore_name }, query)
end

#get_index(project_name, logstore_name) ⇒ Object



161
162
163
164
# File 'lib/aliyun/log/protocol.rb', line 161

def get_index(project_name, logstore_name)
  query = { logstore_name: logstore_name }
  @http.get({ project: project_name, logstore: logstore_name, action: 'index' }, query)
end

#get_logs(project_name, logstore_name, opt = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aliyun/log/protocol.rb', line 124

def get_logs(project_name, logstore_name, opt = {})
  from = opt[:from] || (Time.now - DEFAULT_LOG_TIME_SHIFT).to_i
  to = opt[:to] || Time.now.to_i
  query = {
    type: 'log',
    from: from,
    to: to,
    line: opt[:line],
    offset: opt[:offset],
    reverse: opt[:reverse],
    query: opt[:query],
    topic: opt[:topic]
  }.compact
  @http.get({ project: project_name, logstore: logstore_name }, query)
end

#get_logstore(project_name, logstore_name) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/aliyun/log/protocol.rb', line 96

def get_logstore(project_name, logstore_name)
  query = { logstore_name: logstore_name }
  attrs = @http.get({ project: project_name, logstore: logstore_name }, query)
  attrs = JSON.parse(attrs)
  attrs['projectName'] = project_name
  Logstore.from_json(attrs, self)
end

#get_project(project_name) ⇒ Object



30
31
32
33
34
35
# File 'lib/aliyun/log/protocol.rb', line 30

def get_project(project_name)
  query = { projectName: project_name }
  attrs = @http.get({ project: project_name }, query)
  attrs = JSON.parse(attrs)
  Project.from_json(attrs, self)
end

#list_logstores(project_name, size = nil, offset = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/aliyun/log/protocol.rb', line 58

def list_logstores(project_name, size = nil, offset = nil)
  query = {
    offset: offset,
    size: size
  }.compact
  data = @http.get({ project: project_name, logstore: '' }, query)
  JSON.parse(data)
end

#list_projects(size = nil, offset = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aliyun/log/protocol.rb', line 13

def list_projects(size = nil, offset = nil)
  query = {
    offset: offset,
    size: size
  }.compact
  data = @http.get(nil, query)
  data = JSON.parse(data)
  data['projects'] = data['projects'].map do |attrs|
    Project.from_json(attrs, self)
  end
  data
end

#list_topics(project_name, logstore_name, opt = {}) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/aliyun/log/protocol.rb', line 153

def list_topics(project_name, logstore_name, opt = {})
  query = {
    type: 'topic',
    line: opt[:line] || 100
  }
  @http.get({ project: project_name, logstore: logstore_name }, query)
end

#projects(size = nil, offset = nil) ⇒ Object



26
27
28
# File 'lib/aliyun/log/protocol.rb', line 26

def projects(size = nil, offset = nil)
  list_projects(size, offset)['projects']
end

#put_log(project_name, logstore_name, logs, opts = {}) ⇒ Object



116
117
118
119
120
# File 'lib/aliyun/log/protocol.rb', line 116

def put_log(project_name, logstore_name, logs, opts = {})
  logs = build_log_pb(logs, opts[:time] || Time.now.to_i)
  lg_pb = Protobuf::LogGroup.new(logs: logs, topic: opts[:topic])
  @http.post({ project: project_name, logstore: logstore_name, is_pb: true }, lg_pb)
end

#put_logs(project_name, logstore_name, content) ⇒ Object



104
105
106
# File 'lib/aliyun/log/protocol.rb', line 104

def put_logs(project_name, logstore_name, content)
  @http.post({ project: project_name, logstore: logstore_name, is_pb: true }, content)
end

#update_index(project_name, logstore_name, fields) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/aliyun/log/protocol.rb', line 191

def update_index(project_name, logstore_name, fields)
  body = {
    line: {
      token: INDEX_DEFAULT_TOKEN
    },
    keys: {}
  }
  fields.each do |k, v|
    v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].nil?
    body[:keys][k] = v
  end
  @http.put({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
end

#update_logstore(project_name, logstore_name, opt = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aliyun/log/protocol.rb', line 79

def update_logstore(project_name, logstore_name, opt = {})
  body = {
    logstore_name: logstore_name,
    ttl: opt[:ttl] || 365,
    shardCount: opt[:shard_count] || 2,
    autoSplit: opt[:auto_split].nil? ? false : opt[:auto_split],
    maxSplitShard: opt[:max_split_shard],
    enable_tracking: opt[:enable_tracking].nil? ? false : opt[:enable_tracking]
  }.compact
  @http.put({ project: project_name, logstore: logstore_name }, body.to_json)
end

#update_project(project_name, desc) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/aliyun/log/protocol.rb', line 45

def update_project(project_name, desc)
  body = {
    projectName: project_name,
    description: desc
  }
  @http.put({ project: project_name }, body.to_json)
end