Files
hatchet/sdks/python/examples/opentelemetry_instrumentation/triggers.py
T
Matt Kaye 08f49031be [Python] Feat: Replace REST Client (#1413)
* fix: version

* feat: first pass at new base rest client

* fix: typing

* fix: base client cleanup

* feat: basic runs client

* fix: finally!!

* feat: helper functions for uuid and metadata conversion and api format fix

* fix: patches

* feat: run list apis

* feat: add bulk replay and cancel

* feat: replays and cancels

* feat: result getter

* feat: cron client

* feat: scheduled workflows

* feat: rate limit

* refactor: don't export admin client anymore

* feat: add a bunch more clients and remove the old `rest` thing

* fix: scheduled workflow trigger time

* fix: emptymodel default

* refactor: stop passing pooled workflow run listener around everywhere

* fix: more cleanup of context

* refactor: remove unused stuff from runner

* unwind: keep passing listeners around

* fix: rm some unused stuff

* fix: example

* feat: metrics api

* feat: a couple tests

* feat: more default emptymodels

* fix: tests

* [Docs]: Misc. Python Migration Guide Issues, Rate limits, V1 new features (#1417)

* fix: misc python migration guide

* feat: rate limits docs

* fix: lint

* fix: lint

* feat: skeleton

* feat: add a bunch of docs

* feat: bulk replay and cancel docs

* fix: add task output to example

* fix: otel docs + naming

* fix: lint

* fix: rm timeout

* feat: initial python sdk guide

* fix: raise on dupe on failure or on success

* fix: dags docs

* feat: 1.0.1
2025-03-27 16:32:54 -07:00

152 lines
4.7 KiB
Python

import asyncio
from examples.opentelemetry_instrumentation.client import hatchet
from examples.opentelemetry_instrumentation.tracer import trace_provider
from examples.opentelemetry_instrumentation.worker import otel_workflow
from hatchet_sdk.clients.admin import TriggerWorkflowOptions
from hatchet_sdk.clients.events import BulkPushEventWithMetadata, PushEventOptions
from hatchet_sdk.opentelemetry.instrumentor import (
HatchetInstrumentor,
inject_traceparent_into_metadata,
)
instrumentor = HatchetInstrumentor(tracer_provider=trace_provider)
tracer = trace_provider.get_tracer(__name__)
def create_additional_metadata() -> dict[str, str]:
return inject_traceparent_into_metadata({"hello": "world"})
def create_push_options() -> PushEventOptions:
return PushEventOptions(additional_metadata=create_additional_metadata())
def push_event() -> None:
print("\npush_event")
with tracer.start_as_current_span("push_event"):
hatchet.event.push(
"otel:event",
{"test": "test"},
options=create_push_options(),
)
async def async_push_event() -> None:
print("\nasync_push_event")
with tracer.start_as_current_span("async_push_event"):
await hatchet.event.aio_push(
"otel:event", {"test": "test"}, options=create_push_options()
)
def bulk_push_event() -> None:
print("\nbulk_push_event")
with tracer.start_as_current_span("bulk_push_event"):
hatchet.event.bulk_push(
[
BulkPushEventWithMetadata(
key="otel:event",
payload={"test": "test 1"},
additional_metadata=create_additional_metadata(),
),
BulkPushEventWithMetadata(
key="otel:event",
payload={"test": "test 2"},
additional_metadata=create_additional_metadata(),
),
],
)
async def async_bulk_push_event() -> None:
print("\nasync_bulk_push_event")
with tracer.start_as_current_span("bulk_push_event"):
await hatchet.event.aio_bulk_push(
[
BulkPushEventWithMetadata(
key="otel:event",
payload={"test": "test 1"},
additional_metadata=create_additional_metadata(),
),
BulkPushEventWithMetadata(
key="otel:event",
payload={"test": "test 2"},
additional_metadata=create_additional_metadata(),
),
],
)
def run_workflow() -> None:
print("\nrun_workflow")
with tracer.start_as_current_span("run_workflow"):
otel_workflow.run(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
),
)
async def async_run_workflow() -> None:
print("\nasync_run_workflow")
with tracer.start_as_current_span("async_run_workflow"):
await otel_workflow.aio_run(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
),
)
def run_workflows() -> None:
print("\nrun_workflows")
with tracer.start_as_current_span("run_workflows"):
otel_workflow.run_many(
[
otel_workflow.create_bulk_run_item(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
)
),
otel_workflow.create_bulk_run_item(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
)
),
],
)
async def async_run_workflows() -> None:
print("\nasync_run_workflows")
with tracer.start_as_current_span("async_run_workflows"):
await otel_workflow.aio_run_many(
[
otel_workflow.create_bulk_run_item(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
)
),
otel_workflow.create_bulk_run_item(
options=TriggerWorkflowOptions(
additional_metadata=create_additional_metadata()
)
),
],
)
async def main() -> None:
push_event()
await async_push_event()
bulk_push_event()
await async_bulk_push_event()
run_workflow()
# await async_run_workflow()
run_workflows()
# await async_run_workflows()
if __name__ == "__main__":
asyncio.run(main())