日志上传API示例
最近更新时间: 2024-10-17 17:10:00
cls_kv_pb2.py
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: cls_kv.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='cls_kv.proto',
package='cls',
serialized_pb=_b('\n\x0c\x63ls_kv.proto\x12\x03\x63ls\"^\n\x03Log\x12\x0c\n\x04time\x18\x01 \x02(\x03\x12\"\n\x08\x63ontents\x18\x02 \x03(\x0b\x32\x10.cls.Log.Content\x1a%\n\x07\x43ontent\x12\x0b\n\x03key\x18\x01 \x02(\t\x12\r\n\x05value\x18\x02 \x02(\t\"$\n\x06LogTag\x12\x0b\n\x03key\x18\x01 \x02(\t\x12\r\n\x05value\x18\x02 \x02(\t\"w\n\x08LogGroup\x12\x16\n\x04logs\x18\x01 \x03(\x0b\x32\x08.cls.Log\x12\x13\n\x0b\x63ontextFlow\x18\x02 \x01(\t\x12\x10\n\x08\x66ilename\x18\x03 \x01(\t\x12\x0e\n\x06source\x18\x04 \x01(\t\x12\x1c\n\x07logTags\x18\x05 \x03(\x0b\x32\x0b.cls.LogTag\"3\n\x0cLogGroupList\x12#\n\x0clogGroupList\x18\x01 \x03(\x0b\x32\r.cls.LogGroup')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
_LOG_CONTENT = _descriptor.Descriptor(
name='Content',
full_name='cls.Log.Content',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='key', full_name='cls.Log.Content.key', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='value', full_name='cls.Log.Content.value', index=1,
number=2, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=78,
serialized_end=115,
)
_LOG = _descriptor.Descriptor(
name='Log',
full_name='cls.Log',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='time', full_name='cls.Log.time', index=0,
number=1, type=3, cpp_type=2, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='contents', full_name='cls.Log.contents', index=1,
number=2, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[_LOG_CONTENT, ],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=21,
serialized_end=115,
)
_LOGTAG = _descriptor.Descriptor(
name='LogTag',
full_name='cls.LogTag',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='key', full_name='cls.LogTag.key', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='value', full_name='cls.LogTag.value', index=1,
number=2, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=117,
serialized_end=153,
)
_LOGGROUP = _descriptor.Descriptor(
name='LogGroup',
full_name='cls.LogGroup',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='logs', full_name='cls.LogGroup.logs', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='contextFlow', full_name='cls.LogGroup.contextFlow', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='filename', full_name='cls.LogGroup.filename', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='source', full_name='cls.LogGroup.source', index=3,
number=4, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='logTags', full_name='cls.LogGroup.logTags', index=4,
number=5, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=155,
serialized_end=274,
)
_LOGGROUPLIST = _descriptor.Descriptor(
name='LogGroupList',
full_name='cls.LogGroupList',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='logGroupList', full_name='cls.LogGroupList.logGroupList', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=276,
serialized_end=327,
)
_LOG_CONTENT.containing_type = _LOG
_LOG.fields_by_name['contents'].message_type = _LOG_CONTENT
_LOGGROUP.fields_by_name['logs'].message_type = _LOG
_LOGGROUP.fields_by_name['logTags'].message_type = _LOGTAG
_LOGGROUPLIST.fields_by_name['logGroupList'].message_type = _LOGGROUP
DESCRIPTOR.message_types_by_name['Log'] = _LOG
DESCRIPTOR.message_types_by_name['LogTag'] = _LOGTAG
DESCRIPTOR.message_types_by_name['LogGroup'] = _LOGGROUP
DESCRIPTOR.message_types_by_name['LogGroupList'] = _LOGGROUPLIST
Log = _reflection.GeneratedProtocolMessageType('Log', (_message.Message,), dict(
Content = _reflection.GeneratedProtocolMessageType('Content', (_message.Message,), dict(
DESCRIPTOR = _LOG_CONTENT,
__module__ = 'cls_kv_pb2'
# @@protoc_insertion_point(class_scope:cls.Log.Content)
))
,
DESCRIPTOR = _LOG,
__module__ = 'cls_kv_pb2'
# @@protoc_insertion_point(class_scope:cls.Log)
))
_sym_db.RegisterMessage(Log)
_sym_db.RegisterMessage(Log.Content)
LogTag = _reflection.GeneratedProtocolMessageType('LogTag', (_message.Message,), dict(
DESCRIPTOR = _LOGTAG,
__module__ = 'cls_kv_pb2'
# @@protoc_insertion_point(class_scope:cls.LogTag)
))
_sym_db.RegisterMessage(LogTag)
LogGroup = _reflection.GeneratedProtocolMessageType('LogGroup', (_message.Message,), dict(
DESCRIPTOR = _LOGGROUP,
__module__ = 'cls_kv_pb2'
# @@protoc_insertion_point(class_scope:cls.LogGroup)
))
_sym_db.RegisterMessage(LogGroup)
LogGroupList = _reflection.GeneratedProtocolMessageType('LogGroupList', (_message.Message,), dict(
DESCRIPTOR = _LOGGROUPLIST,
__module__ = 'cls_kv_pb2'
# @@protoc_insertion_point(class_scope:cls.LogGroupList)
))
_sym_db.RegisterMessage(LogGroupList)
# @@protoc_insertion_point(module_scope)
env.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
SECRETID = ""
SECRETKEY = ""
HOST = ""
PORT = 8028
REGION = ""
signature.py
#!/USR/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import random
import time
import hmac
import binascii
import sys
import urllib
from env import *
import hashlib, hmac, json, os, sys, time
from datetime import datetime
def genV3Sig(timestamp, content_type, payload, host):
# 密钥参数
secret_id = SECRETID
secret_key = SECRETKEY
service = "cls"
region = REGION
version = "2020-10-16"
algorithm = "TC3-HMAC-SHA256"
#timestamp = int(time.time())
#timestamp = 1551113065
date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
#params = {"Limit": 1, "Filters": [{"Name": "instance-name", "Values": [u"未命名"]}]}
# ************* 步骤 1:拼接规范请求串 *************
http_request_method = "POST"
canonical_uri = "/"
canonical_querystring = ""
ct = content_type
#payload = json.dumps(params)
canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
signed_headers = "content-type;host"
hashed_request_payload = hashlib.sha256(payload).hexdigest()
#hashed_request_payload = payload
#print hashed_request_payload
canonical_request = (http_request_method + "\n" +
canonical_uri + "\n" +
canonical_querystring + "\n" +
canonical_headers + "\n" +
signed_headers + "\n" +
hashed_request_payload)
#print(canonical_request)
# ************* 步骤 2:拼接待签名字符串 *************
credential_scope = date + "/" + service + "/" + "tc3_request"
hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
string_to_sign = (algorithm + "\n" +
str(timestamp) + "\n" +
credential_scope + "\n" +
hashed_canonical_request)
#print(string_to_sign)
# ************* 步骤 3:计算签名 *************
# 计算签名摘要函数
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
secret_service = sign(secret_date, service)
secret_signing = sign(secret_service, "tc3_request")
signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
#print(signature)
# ************* 步骤 4:拼接 Authorization *************
authorization = (algorithm + " " +
"Credential=" + secret_id + "/" + credential_scope + ", " +
"SignedHeaders=" + signed_headers + ", " +
"Signature=" + signature)
#print hashed_request_payload
#print(authorization)
return authorization
upload_client.py
#!/bin/python
#-*- coding:utf-8 -*-
from signature import *
import sys
import httplib
import json
import time
import string
import random
import cls_kv_pb2
from env import *
from datetime import datetime, date, timedelta
def push_log(conn, topic_id, body, host):
content_type = "application/x-protobuf"
timestamp = int(time.time())
headers = {
"Content-Type" : content_type,
"Content-Length" : str(len(body)),
"X-TC-Action": "UploadLog",
"X-TC-Region": "ap-chongqing",
"X-TC-Timestamp": timestamp,
"X-TC-Version": "2020-10-16",
"TopicId": topic_id,
"Authorization" : genV3Sig(timestamp, content_type, body, host)
}
print(headers)
conn.request(method="POST", url='http://%s/' % host, body=body, headers=headers)
return conn.getresponse()
def pb_gen(start_index, size):
logGroupList = cls_kv_pb2.LogGroupList()
LogGroup = logGroupList.logGroupList.add()
log = LogGroup.logs.add()
log.time = int(time.time())
for index in range(size):
content = log.contents.add()
key = "name,index,test_name_index,"
content.key = key + ''.join(random.sample(string.ascii_letters + string.digits, 60))
value = "test_" + str(start_index) + "_" + str(index) + "_"
for i in range(2):
content.value = content.value + ''.join(random.sample(string.ascii_letters + string.digits, 60))
data = logGroupList.SerializeToString()
return data
if __name__ == '__main__':
host = HOST
port = PORT
conn = httplib.HTTPConnection(host, port)
if(len(sys.argv) == 1):
print 'invalid command '
print 'python ' + sys.argv[0] + ' pushlog $topic_id $log_size(236B per log) $log_count'
exit()
if(sys.argv[1] == 'pushlog'):
size = sys.argv[3]
index = sys.argv[4]
for i in range(1, int(index)+1):
data = pb_gen(i, int(size))
response = push_log(conn, sys.argv[2], data, host)
print response.getheaders()
print response.read()
print response.status
if (i % 300) == 0:
time.sleep(2)