[docs]classDatadogLogsLoader(BaseLoader):"""Load `Datadog` logs. Logs are written into the `page_content` and into the `metadata`. """
[docs]def__init__(self,query:str,api_key:str,app_key:str,from_time:Optional[int]=None,to_time:Optional[int]=None,limit:int=100,)->None:"""Initialize Datadog document loader. Requirements: - Must have datadog_api_client installed. Install with `pip install datadog_api_client`. Args: query: The query to run in Datadog. api_key: The Datadog API key. app_key: The Datadog APP key. from_time: Optional. The start of the time range to query. Supports date math and regular timestamps (milliseconds) like '1688732708951' Defaults to 20 minutes ago. to_time: Optional. The end of the time range to query. Supports date math and regular timestamps (milliseconds) like '1688732708951' Defaults to now. limit: The maximum number of logs to return. Defaults to 100. """# noqa: E501try:fromdatadog_api_clientimportConfigurationexceptImportErrorasex:raiseImportError("Could not import datadog_api_client python package. ""Please install it with `pip install datadog_api_client`.")fromexself.query=queryconfiguration=Configuration()configuration.api_key["apiKeyAuth"]=api_keyconfiguration.api_key["appKeyAuth"]=app_keyself.configuration=configurationself.from_time=from_timeself.to_time=to_timeself.limit=limit
[docs]defload(self)->List[Document]:""" Get logs from Datadog. Returns: A list of Document objects. - page_content - metadata - id - service - status - tags - timestamp """try:fromdatadog_api_clientimportApiClientfromdatadog_api_client.v2.api.logs_apiimportLogsApifromdatadog_api_client.v2.model.logs_list_requestimportLogsListRequestfromdatadog_api_client.v2.model.logs_list_request_pageimport(LogsListRequestPage,)fromdatadog_api_client.v2.model.logs_query_filterimportLogsQueryFilterfromdatadog_api_client.v2.model.logs_sortimportLogsSortexceptImportErrorasex:raiseImportError("Could not import datadog_api_client python package. ""Please install it with `pip install datadog_api_client`.")fromexnow=datetime.now()twenty_minutes_before=now-timedelta(minutes=20)now_timestamp=int(now.timestamp()*1000)twenty_minutes_before_timestamp=int(twenty_minutes_before.timestamp()*1000)_from=(self.from_timeifself.from_timeisnotNoneelsetwenty_minutes_before_timestamp)body=LogsListRequest(filter=LogsQueryFilter(query=self.query,_from=_from,to=f"{self.to_timeifself.to_timeisnotNoneelsenow_timestamp}",),sort=LogsSort.TIMESTAMP_ASCENDING,page=LogsListRequestPage(limit=self.limit,),)withApiClient(configuration=self.configuration)asapi_client:api_instance=LogsApi(api_client)response=api_instance.list_logs(body=body).to_dict()docs:List[Document]=[]forrowinresponse["data"]:docs.append(self.parse_log(row))returndocs