Class: AliyunSls::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun_sls/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(project, region, access_key_secret, aliyun_access_key, opts = {}) ⇒ Connection

深圳:cn-shenzhen.sls.aliyuncs.com



39
40
41
42
43
44
45
46
47
48
# File 'lib/aliyun_sls/connection.rb', line 39

def initialize(project, region, access_key_secret, aliyun_access_key, opts={})
    default_headers = {
        "x-sls-apiversion" => "0.4.0",
        "x-sls-signaturemethod" => "hmac-sha1"
    }
    @headers = default_headers.update opts
    @aliyun_access_key = aliyun_access_key
    @access_key_secret = access_key_secret
    @host = "#{project}.#{region}"
end

Instance Method Details

#get_histograms(logstorename, opts = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/aliyun_sls/connection.rb', line 113

def get_histograms(logstorename, opts={})
    default_opts = {
        :type => "histogram",
        :from => Time.now.to_i - 60*5,#默认是五分钟前
        :to => Time.now.to_i,
        :topic => "",
        :query => "",
    }
    opts = default_opts.update opts
    headers = compact_headers(nil, nil)
    headers["Authorization"] = signature("GET", logstorename, headers, nil, opts)

    u = Addressable::URI.parse("http://#{@host}/logstores/#{logstorename}")
    headers["Referer"] = u.to_s
    u.query_values = opts
    rsp = RestClient.get u.to_s, headers
    parse_response(rsp)
end

#get_logs(logstorename, opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aliyun_sls/connection.rb', line 74

def get_logs(logstorename, opts={})
    default_opts = {
        :type => "log",
        :from => Time.now.to_i - 60*5,#默认是五分钟前
        :to => Time.now.to_i,
        :line => 100,
        :offset => 0,
        :reverse => false
    }
    opts = default_opts.update opts
    headers = compact_headers(nil, nil)
    headers["Authorization"] = signature("GET", logstorename, headers, nil, opts)

    u = Addressable::URI.parse("http://#{@host}/logstores/#{logstorename}")
    headers["Referer"] = u.to_s
    u.query_values = opts
    rsp = RestClient.get u.to_s, headers
    parse_response(rsp)
end

#list_logstoresObject



63
64
65
66
67
68
69
70
71
# File 'lib/aliyun_sls/connection.rb', line 63

def list_logstores
    headers = compact_headers(nil, nil)
    headers["Authorization"] = signature("GET", nil, headers, nil, {})

    u = URI.parse("http://#{@host}/logstores")
    headers["Referer"] = u.to_s
    rsp = RestClient.get u.to_s, headers
    parse_response(rsp)
end

#list_topics(logstorename, opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aliyun_sls/connection.rb', line 95

def list_topics(logstorename, opts={})
    default_opts = {
        :type => "topic",
        :line => 100,
        :toke => ""
    }
    opts = default_opts.update opts
    headers = compact_headers(nil, nil)
    headers["Authorization"] = signature("GET", logstorename, headers, nil, opts)

    u = Addressable::URI.parse("http://#{@host}/logstores/#{logstorename}")
    headers["Referer"] = u.to_s
    u.query_values = opts
    rsp = RestClient.get u.to_s, headers
    parse_response(rsp)
end

#puts_logs(logstorename, content) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/aliyun_sls/connection.rb', line 51

def puts_logs(logstorename, content)
    # 压缩content数据
    compressed = Zlib::Deflate.deflate(content.encode.to_s)
    headers = compact_headers(content, compressed)
    headers["Authorization"] = signature("POST", logstorename, headers, content, {})

    u = URI.parse("http://#{@host}/logstores/#{logstorename}")
    rsp = RestClient.post u.to_s, compressed, headers
    parse_response(rsp)
end