The framework offers a facade which lets you determine the current context. For this class /IWBEP/CL_DIAGNOSTICS_FACADE is available with several methods:
IS_GATEWAY_SERVICE: Returns TRUE if an OData service is being processed
GET_CURRENT_GATEWAY_SERVICE: Retrieves the OData service which is currently being processed
You can use this as follows:
Option 1 only returns a flag indicating that you are in the context of an OData service.
DATA: lv_is_gateway_service TYPE abap_bool. lv_is_gateway_service = /iwbep/cl_diagnostics_facade=>is_gateway_service( ).
Option 2 returns the name of the OData service in whose context you are currently in.
DATA: lv_service_name TYPE string. lv_service_name = /iwbep/cl_diagnostics_facade=>get_current_gateway_service( ).
This works similar to the ABAP language element SY-TDOCE: SY-TDOCE returns the ABAP transaction code which has called the current coding.