# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from enum import Enum
from typing import Final

from typing_extensions import deprecated

RPC_CONNECT_RPC_ERROR_CODE: Final = "rpc.connect_rpc.error_code"
"""
Deprecated: Replaced by `rpc.response.status_code`.
"""

RPC_CONNECT_RPC_REQUEST_METADATA_TEMPLATE: Final = (
    "rpc.connect_rpc.request.metadata"
)
"""
Deprecated: Replaced by `rpc.request.metadata`.
"""

RPC_CONNECT_RPC_RESPONSE_METADATA_TEMPLATE: Final = (
    "rpc.connect_rpc.response.metadata"
)
"""
Deprecated: Replaced by `rpc.response.metadata`.
"""

RPC_GRPC_REQUEST_METADATA_TEMPLATE: Final = "rpc.grpc.request.metadata"
"""
Deprecated: Replaced by `rpc.request.metadata`.
"""

RPC_GRPC_RESPONSE_METADATA_TEMPLATE: Final = "rpc.grpc.response.metadata"
"""
Deprecated: Replaced by `rpc.response.metadata`.
"""

RPC_GRPC_STATUS_CODE: Final = "rpc.grpc.status_code"
"""
Deprecated: Use string representation of the gRPC status code on the `rpc.response.status_code` attribute.
"""

RPC_JSONRPC_ERROR_CODE: Final = "rpc.jsonrpc.error_code"
"""
Deprecated: Use string representation of the error code on the `rpc.response.status_code` attribute.
"""

RPC_JSONRPC_ERROR_MESSAGE: Final = "rpc.jsonrpc.error_message"
"""
Deprecated: Use the span status description when reporting JSON-RPC spans.
"""

RPC_JSONRPC_REQUEST_ID: Final = "rpc.jsonrpc.request_id"
"""
Deprecated: Replaced by `jsonrpc.request.id`.
"""

RPC_JSONRPC_VERSION: Final = "rpc.jsonrpc.version"
"""
Deprecated: Replaced by `jsonrpc.protocol.version`.
"""

RPC_MESSAGE_COMPRESSED_SIZE: Final = "rpc.message.compressed_size"
"""
Deprecated: Deprecated, no replacement at this time.
"""

RPC_MESSAGE_ID: Final = "rpc.message.id"
"""
Deprecated: Deprecated, no replacement at this time.
"""

RPC_MESSAGE_TYPE: Final = "rpc.message.type"
"""
Deprecated: Deprecated, no replacement at this time.
"""

RPC_MESSAGE_UNCOMPRESSED_SIZE: Final = "rpc.message.uncompressed_size"
"""
Deprecated: Deprecated, no replacement at this time.
"""

RPC_METHOD: Final = "rpc.method"
"""
The fully-qualified logical name of the method from the RPC interface perspective.
Note: The method name MAY have unbounded cardinality in edge or error cases.

Some RPC frameworks or libraries provide a fixed set of recognized methods
for client stubs and server implementations. Instrumentations for such
frameworks MUST set this attribute to the original method name only
when the method is recognized by the framework or library.

When the method is not recognized, for example, when the server receives
a request for a method that is not predefined on the server, or when
instrumentation is not able to reliably detect if the method is predefined,
the attribute MUST be set to `_OTHER`. In such cases, tracing
instrumentations MUST also set `rpc.method_original` attribute to
the original method value.

If the RPC instrumentation could end up converting valid RPC methods to
`_OTHER`, then it SHOULD provide a way to configure the list of recognized
RPC methods.

The `rpc.method` can be different from the name of any implementing
method/function.
The `code.function.name` attribute may be used to record the fully-qualified
method actually executing the call on the server side, or the
RPC client stub method on the client side.
"""

RPC_METHOD_ORIGINAL: Final = "rpc.method_original"
"""
The original name of the method used by the client.
"""

RPC_REQUEST_METADATA_TEMPLATE: Final = "rpc.request.metadata"
"""
RPC request metadata, `<key>` being the normalized RPC metadata key (lowercase), the value being the metadata values.
Note: Instrumentations SHOULD require an explicit configuration of which metadata values are to be captured.
Including all request metadata values can be a security risk - explicit configuration helps avoid leaking sensitive information.

For example, a property `my-custom-key` with value `["1.2.3.4", "1.2.3.5"]` SHOULD be recorded as
`rpc.request.metadata.my-custom-key` attribute with value `["1.2.3.4", "1.2.3.5"]`.
"""

RPC_RESPONSE_METADATA_TEMPLATE: Final = "rpc.response.metadata"
"""
RPC response metadata, `<key>` being the normalized RPC metadata key (lowercase), the value being the metadata values.
Note: Instrumentations SHOULD require an explicit configuration of which metadata values are to be captured.
Including all response metadata values can be a security risk - explicit configuration helps avoid leaking sensitive information.

For example, a property `my-custom-key` with value `["attribute_value"]` SHOULD be recorded as
the `rpc.response.metadata.my-custom-key` attribute with value `["attribute_value"]`.
"""

RPC_RESPONSE_STATUS_CODE: Final = "rpc.response.status_code"
"""
Status code of the RPC returned by the RPC server or generated by the client.
Note: Usually it represents an error code, but may also represent partial success, warning, or differentiate between various types of successful outcomes.
Semantic conventions for individual RPC frameworks SHOULD document what `rpc.response.status_code` means in the context of that system and which values are considered to represent errors.
"""

RPC_SERVICE: Final = "rpc.service"
"""
Deprecated: Value should be included in `rpc.method` which is expected to be a fully-qualified name.
"""

RPC_SYSTEM: Final = "rpc.system"
"""
Deprecated: Replaced by `rpc.system.name`.
"""

RPC_SYSTEM_NAME: Final = "rpc.system.name"
"""
The Remote Procedure Call (RPC) system.
Note: The client and server RPC systems may differ for the same RPC interaction. For example, a client may use Apache Dubbo or Connect RPC to communicate with a server that uses gRPC since both protocols provide compatibility with gRPC.
"""


@deprecated(
    "The attribute rpc.connect_rpc.error_code is deprecated - Replaced by `rpc.response.status_code`"
)
class RpcConnectRpcErrorCodeValues(Enum):
    CANCELLED = "cancelled"
    """cancelled."""
    UNKNOWN = "unknown"
    """unknown."""
    INVALID_ARGUMENT = "invalid_argument"
    """invalid_argument."""
    DEADLINE_EXCEEDED = "deadline_exceeded"
    """deadline_exceeded."""
    NOT_FOUND = "not_found"
    """not_found."""
    ALREADY_EXISTS = "already_exists"
    """already_exists."""
    PERMISSION_DENIED = "permission_denied"
    """permission_denied."""
    RESOURCE_EXHAUSTED = "resource_exhausted"
    """resource_exhausted."""
    FAILED_PRECONDITION = "failed_precondition"
    """failed_precondition."""
    ABORTED = "aborted"
    """aborted."""
    OUT_OF_RANGE = "out_of_range"
    """out_of_range."""
    UNIMPLEMENTED = "unimplemented"
    """unimplemented."""
    INTERNAL = "internal"
    """internal."""
    UNAVAILABLE = "unavailable"
    """unavailable."""
    DATA_LOSS = "data_loss"
    """data_loss."""
    UNAUTHENTICATED = "unauthenticated"
    """unauthenticated."""


@deprecated(
    "The attribute rpc.grpc.status_code is deprecated - Use string representation of the gRPC status code on the `rpc.response.status_code` attribute"
)
class RpcGrpcStatusCodeValues(Enum):
    OK = 0
    """OK."""
    CANCELLED = 1
    """CANCELLED."""
    UNKNOWN = 2
    """UNKNOWN."""
    INVALID_ARGUMENT = 3
    """INVALID_ARGUMENT."""
    DEADLINE_EXCEEDED = 4
    """DEADLINE_EXCEEDED."""
    NOT_FOUND = 5
    """NOT_FOUND."""
    ALREADY_EXISTS = 6
    """ALREADY_EXISTS."""
    PERMISSION_DENIED = 7
    """PERMISSION_DENIED."""
    RESOURCE_EXHAUSTED = 8
    """RESOURCE_EXHAUSTED."""
    FAILED_PRECONDITION = 9
    """FAILED_PRECONDITION."""
    ABORTED = 10
    """ABORTED."""
    OUT_OF_RANGE = 11
    """OUT_OF_RANGE."""
    UNIMPLEMENTED = 12
    """UNIMPLEMENTED."""
    INTERNAL = 13
    """INTERNAL."""
    UNAVAILABLE = 14
    """UNAVAILABLE."""
    DATA_LOSS = 15
    """DATA_LOSS."""
    UNAUTHENTICATED = 16
    """UNAUTHENTICATED."""


@deprecated(
    "The attribute rpc.message.type is deprecated - Deprecated, no replacement at this time"
)
class RpcMessageTypeValues(Enum):
    SENT = "SENT"
    """sent."""
    RECEIVED = "RECEIVED"
    """received."""


@deprecated(
    "The attribute rpc.system is deprecated - Replaced by `rpc.system.name`"
)
class RpcSystemValues(Enum):
    GRPC = "grpc"
    """gRPC."""
    JAVA_RMI = "java_rmi"
    """Java RMI."""
    DOTNET_WCF = "dotnet_wcf"
    """.NET WCF."""
    APACHE_DUBBO = "apache_dubbo"
    """Apache Dubbo."""
    CONNECT_RPC = "connect_rpc"
    """Connect RPC."""
    ONC_RPC = "onc_rpc"
    """[ONC RPC (Sun RPC)](https://datatracker.ietf.org/doc/html/rfc5531)."""
    JSONRPC = "jsonrpc"
    """JSON-RPC."""


class RpcSystemNameValues(Enum):
    GRPC = "grpc"
    """[gRPC](https://grpc.io/)."""
    DUBBO = "dubbo"
    """[Apache Dubbo](https://dubbo.apache.org/)."""
    CONNECTRPC = "connectrpc"
    """[Connect RPC](https://connectrpc.com/)."""
    JSONRPC = "jsonrpc"
    """[JSON-RPC](https://www.jsonrpc.org/)."""
