अतुल्यकालिक पीजी¶
logfire.instrument_asyncpg() कार्यों का उपयोग किया जा सकता है लॉगफायर पता लगाना asyncpg greSQL ड्राइवर।
स्थापित करें¶
उपयोग asyncpg अतिरिक्त स्थापना logfire यह क्या है?
{{ install_logfire(extras=['asyncpg']) }}
उपयोग¶
आइए डॉकर का उपयोग करके एक greSQL डेटाबेस सेट करें और एक पायथन स्क्रिप्ट चलाएं जो डेटाबेस से जुड़ता है asyncpg का उपयोग करके प्रदर्शित करता है कि कैसे लॉगफायर asyncpg के साथ उपयोग करें।
¶
डॉकर के साथ greSQL डेटाबेस की स्थापना
सबसे पहले, हमें greSQL डेटाबेस को इनिशियलाइज़ करने की आवश्यकता है। यह निम्नलिखित कमांड के साथ डॉकर का उपयोग करके आसानी से किया जा सकता है:
docker run --name postgres \
-e POSTGRES_USER=user \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=database \
-p 5432:5432 -d postgres
यह क्रमबद्ध करना निम्नलिखित कार्यों को पूरा करता है:
-
--name postgres: डॉकर कंटेनर का नाम परिभाषित करें। -
-e POSTGRES_USER=user: greSQL सर्वर के लिए एक उपयोगकर्ता सेट करें। -
-e POSTGRES_PASSWORD=secretgreSQL सर्वर के लिए एक पासवर्ड सेट करें। -
-e POSTGRES_DB=databaseयह "डेटाबेस" नामक एक नया डेटाबेस बनाएगा, जो पायथन स्क्रिप्ट में उपयोग किया जाने वाला एक ही डेटाबेस है। -
-p 5432:5432यह पोर्ट 5432 पर स्थानीय कंप्यूटर पर greSQL उदाहरण उपलब्ध कराता है। -
-d postgres: यह डॉकर छवि का उपयोग करने के लिए इंगित करता है, इस मामले में "पोस्टग्रेस।"
पायथन स्क्रिप्ट चलाएं¶
निम्नलिखित पायथन स्क्रिप्ट greSQL डेटाबेस से जुड़ती है और कुछ SQL प्रश्नों को निष्पादित करती है:
import asyncio
import asyncpg
import logfire
logfire.configure()
logfire.instrument_asyncpg()
async def main():
connection: asyncpg.Connection = await asyncpg.connect(
user='user', password='secret', database='database', host='0.0.0.0', port=5432
)
with logfire.span('Create table and insert data'):
await connection.execute('CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, num integer, data varchar);')
# Insert some data
await connection.execute('INSERT INTO test (num, data) VALUES ($1, $2)', 100, 'abc')
await connection.execute('INSERT INTO test (num, data) VALUES ($1, $2)', 200, 'def')
# Query the data
for record in await connection.fetch('SELECT * FROM test'):
logfire.info('Retrieved {record=}', record=record)
asyncio.run(main())
यदि आप यूआई पर परियोजना पर जाते हैं, तो आप स्क्रिप्ट निर्माण की अवधि देखेंगे।
opentelemetry-asyncpg https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asyncpg/asyncpg.html opentelemetry-asyncpg2 https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asyncpg2/asyncpg2.html [Asyncpg]: https://magicstack.github.io/asyncpg /
本文总阅读量次