oracle / oci-python-sdk

Oracle Cloud Infrastructure SDK for Python
https://cloud.oracle.com/cloud-infrastructure
Other
393 stars 281 forks source link

while runnig the usgae api call getting error #694

Open Gopichand-Heeddata opened 3 months ago

Gopichand-Heeddata commented 3 months ago

I am facing an issue while running the usage API call ,I am getting internal server error @adizohar can you help me this issue .

 def discover_compartments(self, start_date, range_end=None):
        """
        The functionality of this API is to filter the data based on
        compartmentName,resourceId by using "group_by" parameter. The limit of
        "group_by" parameter is 4[it is restricted by oci sdk]. Thats why, we
        are calling the oci sdk api to get the values.

        """
        LOG.info(f"Discovering compartment names of resources... initiated time: {datetime.now(timezone.utc)}")
        resources_dict = {}
        end_date_later = datetime.now(timezone.utc) + timedelta(days=1)
        end_date = end_date_later.replace(second=0, microsecond=0, minute=0,hour=0)
        interval = timedelta(days=3*30)
        if not start_date:
            return resources_dict
        current_date = start_date.replace(tzinfo = timezone.utc)
        total_tags=defaultdict(lambda: defaultdict(lambda: {}))
        while current_date < end_date:
            request_summary_details = RequestSummarizedUsagesDetails(
               tenant_id=self._Tenancy_OCID,
               granularity='DAILY',
               query_type='COST',
               group_by=['compartmentName','resourceId','unit'],
               time_usage_started=self.str_from_datetime(current_date),
               time_usage_ended=self.str_from_datetime(min(end_date,current_date+interval)),
               compartment_depth=2
            )

            request_summarized_usages =list_call_get_all_results( 
                self.usage.request_summarized_usages,
                request_summary_details,
                retry_strategy=self.CUSTOM_RETRY_STRATEGY
            )
            current_date += interval
            for item in request_summarized_usages.data:
                resources_dict[item.resource_id] = {'compartment_name':item.compartment_name,'unit':item.unit  }
            request_summary_details.group_by=['resourceId', 'tagNamespace', 'tagKey', 'tagValue']
            request_summarized_tags = list_call_get_all_results( 
                self.usage.request_summarized_usages,
                request_summary_details,
                retry_strategy=self.CUSTOM_RETRY_STRATEGY
            )
            for item in request_summarized_tags.data:
                for tag in item.tags:
                    formated_tag_key = tag.key
                    if tag.namespace:
                        formated_tag_key = f'{tag.namespace}.{formated_tag_key}'
                    total_tags[item.resource_id][self.str_from_datetime(item.time_usage_started)][formated_tag_key]=tag.value

        LOG.info(f"compartments fetched successfully... endtime: {datetime.now(timezone.utc)}")
        return resources_dict,total_tags

Error

Traceback (most recent call last):
  File "/usr/src/app/diworker/diworker/main.py", line 229, in process_task
    self.report_import(body)
  File "/usr/src/app/diworker/diworker/main.py", line 150, in report_import
    importer.import_report()
  File "/usr/src/app/diworker/diworker/importers/base.py", line 544, in import_report
    self.data_import()
  File "/usr/src/app/diworker/diworker/importers/base.py", line 534, in data_import
    self.load_raw_data()
  File "/usr/src/app/diworker/diworker/importers/oci.py", line 86, in load_raw_data
    self._load_usage_data(currency_rate)
  File "/usr/src/app/diworker/diworker/importers/oci.py", line 108, in _load_usage_data
    tags =self.cloud_adapter.discover_compartments(self.period_start)
  File "/usr/src/app/tools/cloud_adapter_handler/clouds/oci.py", line 1468, in discover_compartments
    request_summarized_tags = list_call_get_all_results(
  File "/usr/local/lib/python3.9/site-packages/oci/pagination/pagination_utils.py", line 205, in list_call_get_all_results
    for response in list_call_get_all_results_generator(list_func_ref, 'response', *list_func_args, **list_func_kwargs):
  File "/usr/local/lib/python3.9/site-packages/oci/pagination/pagination_utils.py", line 274, in list_call_get_all_results_generator
    call_result = retry.DEFAULT_RETRY_STRATEGY.make_retrying_call(list_func_ref, *list_func_args, **list_func_kwargs)
  File "/usr/local/lib/python3.9/site-packages/oci/retry/retry.py", line 308, in make_retrying_call
    response = func_ref(*func_args, **func_kwargs)
  File "/usr/local/lib/python3.9/site-packages/oci/usage_api/usageapi_client.py", line 2448, in request_summarized_usages
    return retry_strategy.make_retrying_call(
  File "/usr/local/lib/python3.9/site-packages/oci/retry/retry.py", line 308, in make_retrying_call
    response = func_ref(*func_args, **func_kwargs)
  File "/usr/local/lib/python3.9/site-packages/oci/base_client.py", line 535, in call_api
    response = self.request(request, allow_control_chars, operation_name, api_reference_link)
  File "/usr/local/lib/python3.9/site-packages/circuitbreaker.py", line 159, in wrapper
    return call(function, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/circuitbreaker.py", line 170, in call
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/oci/base_client.py", line 724, in request
    self.raise_transient_service_error(request, response, service_code, message, operation_name, api_reference_link, target_service, request_endpoint, client_version, timestamp, deserialized_data)
  File "/usr/local/lib/python3.9/site-packages/oci/base_client.py", line 906, in raise_transient_service_error
    raise exceptions.TransientServiceError(
oci.exceptions.TransientServiceError: {'target_service': 'usageapi', 'status': 500, 'code': 'InternalServerError', 'opc-request-id': 'FD3C13A38D6F4C2A93A1396CC53FA6F2/96367926D5CB3E6569006A19B55CC701/906B5892D2DE479E703D115AA0959E31', 'message': 'Internal Service Error, please try again', 'operation_name': 'request_summarized_usages', 'timestamp': '2024-08-25T07:47:44.212543+00:00', 'client_version': 'Oracle-PythonSDK/2.125.0', 'request_endpoint': 'POST https://usageapi.ap-mumbai-1.oci.oraclecloud.com/20200107/usage', 'logging_tips': 'To get more info on the failing request, refer to https://docs.oracle.com/en-us/iaas/tools/python/latest/logging.html for ways to log the request/response details.', 'troubleshooting_tips': "See https://docs.oracle.com/iaas/Content/API/References/apierrors.htm#apierrors_500__500_internalservererror for more information about resolving this error. Also see https://docs.oracle.com/iaas/api/#/en/usage/20200107/UsageSummary/RequestSummarizedUsages for details on this operation's requirements. If you are unable to resolve this usageapi issue, please contact Oracle support and provide them this full error message."}
adizohar commented 3 months ago

I would suggest to use smaller time range than 90 days

Gopichand-Heeddata commented 3 months ago

@adizohar even 60 days also ,i have run the code getting same error.

Can you please look this.

adizohar commented 3 months ago

I don't have anyway to check it, I would suggest you to use Oracle Support and open a ticket Post your error and code, support can engage the correct team. to check it. GitHub issues is for SDK issues and you have service issue.