OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Querying loki logs using python

  • Thread starter Thread starter Nitz
  • Start date Start date
N

Nitz

Guest
I am using the following script to query logs from loki which is running on some vm.I want to get the logs on multiple boxes and do some operation on them. so i am using loki as a source to get all the logs collectively.

Code:
import requests
import time
import requests
import urllib3
import ssl
import urllib.parse


class CustomHttpAdapter (requests.adapters.HTTPAdapter):
    # "Transport adapter" that allows us to use custom ssl_context.

    def __init__(self, ssl_context=None, **kwargs):
        self.ssl_context = ssl_context
        super().__init__(**kwargs)

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = urllib3.poolmanager.PoolManager(
            num_pools=connections, maxsize=maxsize,
            block=block, ssl_context=self.ssl_context)


def get_legacy_session():
    ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
    ctx.options |= 0x4  # OP_LEGACY_SERVER_CONNECT
    session = requests.session()
    session.mount('https://', CustomHttpAdapter(ctx))
    return session


loki_url = "https://url-for-loki"


query = '{instance="some-instance"}'
# query = urllib.parse.quote(query)
# print(query)
range_seconds = 60


limit = 10000

step = 60

headers = {
    "Content-Type": "application/json",
    "X-Scope-OrgID": "tenant-id",
}




while True:

    payload = {
        "query": query,
        "range": {
            "from": f"now-{range_seconds}s",
            "to": "now"
        },
        "limit": limit,
        "step": step,
    }


    print(payload)
    response = get_legacy_session().get(
        f"{loki_url}/loki/api/v1/query", json=payload, headers=headers)

    # print(response.json())
    print("text:-", response.text)

    if response.status_code != 200:
        print(f"Error: {response.status_code}")
        exit()

    for log in response.json()["data"]["result"]:
        print(log["values"][0][1])

    time.sleep(60)

But i get a syntax error. text:- parse error : syntax error: unexpected $end Error: 400

what am i doing wrong here?
<p>I am using the following script to query logs from loki which is running on some vm.I want to get the logs on multiple boxes and do some operation on them. so i am using loki as a source to get all the logs collectively.</p>
<pre><code>import requests
import time
import requests
import urllib3
import ssl
import urllib.parse


class CustomHttpAdapter (requests.adapters.HTTPAdapter):
# "Transport adapter" that allows us to use custom ssl_context.

def __init__(self, ssl_context=None, **kwargs):
self.ssl_context = ssl_context
super().__init__(**kwargs)

def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = urllib3.poolmanager.PoolManager(
num_pools=connections, maxsize=maxsize,
block=block, ssl_context=self.ssl_context)


def get_legacy_session():
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ctx.options |= 0x4 # OP_LEGACY_SERVER_CONNECT
session = requests.session()
session.mount('https://', CustomHttpAdapter(ctx))
return session


loki_url = "https://url-for-loki"


query = '{instance="some-instance"}'
# query = urllib.parse.quote(query)
# print(query)
range_seconds = 60


limit = 10000

step = 60

headers = {
"Content-Type": "application/json",
"X-Scope-OrgID": "tenant-id",
}




while True:

payload = {
"query": query,
"range": {
"from": f"now-{range_seconds}s",
"to": "now"
},
"limit": limit,
"step": step,
}


print(payload)
response = get_legacy_session().get(
f"{loki_url}/loki/api/v1/query", json=payload, headers=headers)

# print(response.json())
print("text:-", response.text)

if response.status_code != 200:
print(f"Error: {response.status_code}")
exit()

for log in response.json()["data"]["result"]:
print(log["values"][0][1])

time.sleep(60)
</code></pre>
<p>But i get a syntax error.
text:- parse error : syntax error: unexpected $end
Error: 400</p>
<p>what am i doing wrong here?</p>
 
Top