def _send(self, method, project, body, resource, params, headers): if body: headers['Content-Length'] = len(body) headers['Content-MD5'] = Util.cal_md5(body) headers['Content-Type'] = 'application/x-protobuf' else: headers['Content-Length'] = 0 headers["x-sls-bodyrawsize"] = 0 headers['Content-Type'] = '' headers['x-sls-apiversion'] = API_VERSION headers['x-sls-signaturemethod'] = 'hmac-sha1' if self._isRowIp: self._slsConHost = self._slsHost else: self._slsConHost = project + "." + self._slsHost headers['Host'] = project + "." + self._slsHost headers['Date'] = self._getGMT() signature = Util.get_request_authorization(method, resource, self._accessKey, params, headers) headers['Authorization'] = "SLS " + self._accessKeyId + ':' + signature if params: resource += '?' + Util.url_encode(params) return self._sendRequest(method, resource, body, headers)
def put_logs(self, request): """ Put logs to SLS. Unsuccessful opertaion will cause an SLSException. :type request: PutLogsRequest :param request: the PutLogs request parameters class :return: PutLogsResponse :raise: SLSException """ if len(request.get_log_items()) > 4096: raise SLSException( 'InvalidLogSize', "logItems' length exceeds maximum limitation: 4096 lines.") logGroup = LogGroup() logGroup.Topic = request.get_topic() if request.get_source(): logGroup.Source = request.get_source() else: if self._source == '127.0.0.1': self._source = Util.get_host_ip(request.get_project() + '.' + self._slsHost) logGroup.Source = self._source for logItem in request.get_log_items(): log = logGroup.Logs.add() log.Time = logItem.get_time() contents = logItem.get_contents() for key, value in contents: content = log.Contents.add() content.Key = unicode(key, 'utf-8') content.Value = unicode(value, 'utf-8') body = logGroup.SerializeToString() if len(body) > 3 * 1024 * 1024: # 3 MB raise SLSException( 'InvalidLogSize', "logItems' size exceeds maximum limitation: 3 MB.") headers = {} headers['x-sls-bodyrawsize'] = len(body) headers['x-sls-compresstype'] = 'deflate' body = Util.compress_data(body) params = {} logstore = request.get_logstore() project = request.get_project() resource = '/logstores/' + logstore respHeaders = self._send('POST', project, body, resource, params, headers) return PutLogsResponse(respHeaders[1])
def __init__(self, endpoint, accessKeyId, accessKey): self._isRowIp = True self._port = 80 self._setendpoint(endpoint) self._accessKeyId = accessKeyId self._accessKey = accessKey self._timeout = CONNECTION_TIME_OUT self._source = Util.get_host_ip(self._slsHost)
def put_logs(self, request): """ Put logs to SLS. Unsuccessful opertaion will cause an SLSException. :type request: PutLogsRequest :param request: the PutLogs request parameters class :return: PutLogsResponse :raise: SLSException """ if len(request.get_log_items()) > 4096: raise SLSException('InvalidLogSize', "logItems' length exceeds maximum limitation: 4096 lines.") logGroup = LogGroup() logGroup.Topic = request.get_topic() if request.get_source(): logGroup.Source = request.get_source() else: if self._source=='127.0.0.1': self._source = Util.get_host_ip(request.get_project() + '.' + self._slsHost) logGroup.Source = self._source for logItem in request.get_log_items(): log = logGroup.Logs.add() log.Time = logItem.get_time() contents = logItem.get_contents() for key, value in contents: content = log.Contents.add() content.Key = unicode(key, 'utf-8') content.Value = unicode(value, 'utf-8') body = logGroup.SerializeToString() if len(body) > 3 * 1024 * 1024: # 3 MB raise SLSException('InvalidLogSize', "logItems' size exceeds maximum limitation: 3 MB.") headers = {} headers['x-sls-bodyrawsize'] = len(body) headers['x-sls-compresstype'] = 'deflate' body = Util.compress_data(body) params = {} logstore = request.get_logstore() project = request.get_project() resource = '/logstores/' + logstore respHeaders = self._send('POST', project, body, resource, params, headers) return PutLogsResponse(respHeaders[1])
def _setendpoint(self, endpoint): pos = endpoint.find('://') if pos != -1: endpoint = endpoint[pos + 3:] # strip http:// pos = endpoint.find('/') if pos != -1: endpoint = endpoint[:pos] pos = endpoint.find(':') if pos != -1: self._port = int(endpoint[pos + 1:]) endpoint = endpoint[:pos] self._isRowIp = Util.is_row_ip(endpoint) self._slsHost = endpoint self._endpoint = endpoint + ':' + str(self._port)