@@ -68,8 +68,8 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
68
68
def __init__ (self , servicer_context , active_span ):
69
69
self ._servicer_context = servicer_context
70
70
self ._active_span = active_span
71
- self .code = grpc .StatusCode .OK
72
- self .details = None
71
+ self ._code = grpc .StatusCode .OK
72
+ self ._details = None
73
73
super ().__init__ ()
74
74
75
75
def __getattr__ (self , attr ):
@@ -118,45 +118,68 @@ def trailing_metadata(self):
118
118
return self ._servicer_context .trailing_metadata ()
119
119
120
120
def abort (self , code , details ):
121
- self .code = code
122
- self .details = details
121
+ if not hasattr (self ._servicer_context , "abort" ):
122
+ raise RuntimeError (
123
+ "abort() is not supported with the installed version of grpcio"
124
+ )
125
+ self ._code = code
126
+ self ._details = details
123
127
self ._active_span .set_attribute (
124
128
SpanAttributes .RPC_GRPC_STATUS_CODE , code .value [0 ]
125
129
)
126
130
self ._active_span .set_status (
127
131
Status (
128
132
status_code = StatusCode .ERROR ,
129
- description = f"{ code } :{ details } " ,
133
+ description = f"{ code } : { details } " ,
130
134
)
131
135
)
132
136
return self ._servicer_context .abort (code , details )
133
137
134
138
def abort_with_status (self , status ):
139
+ if not hasattr (self ._servicer_context , "abort_with_status" ):
140
+ raise RuntimeError (
141
+ "abort_with_status() is not supported with the installed version of grpcio"
142
+ )
135
143
return self ._servicer_context .abort_with_status (status )
136
144
145
+ def code (self ):
146
+ if not hasattr (self ._servicer_context , "code" ):
147
+ raise RuntimeError (
148
+ "code() is not supported with the installed version of grpcio"
149
+ )
150
+ return self ._servicer_context .code ()
151
+
137
152
def set_code (self , code ):
138
- self .code = code
153
+ self ._code = code
139
154
# use details if we already have it, otherwise the status description
140
- details = self .details or code .value [1 ]
155
+ details = self ._details or code .value [1 ]
141
156
self ._active_span .set_attribute (
142
157
SpanAttributes .RPC_GRPC_STATUS_CODE , code .value [0 ]
143
158
)
144
159
if code != grpc .StatusCode .OK :
145
160
self ._active_span .set_status (
146
161
Status (
147
162
status_code = StatusCode .ERROR ,
148
- description = f"{ code } :{ details } " ,
163
+ description = f"{ code } : { details } " ,
149
164
)
150
165
)
151
166
return self ._servicer_context .set_code (code )
152
167
168
+ def details (self ):
169
+ if not hasattr (self ._servicer_context , "details" ):
170
+ raise RuntimeError (
171
+ "details() is not supported with the installed version of "
172
+ "grpcio"
173
+ )
174
+ return self ._servicer_context .details ()
175
+
153
176
def set_details (self , details ):
154
- self .details = details
155
- if self .code != grpc .StatusCode .OK :
177
+ self ._details = details
178
+ if self ._code != grpc .StatusCode .OK :
156
179
self ._active_span .set_status (
157
180
Status (
158
181
status_code = StatusCode .ERROR ,
159
- description = f"{ self .code } : { details } " ,
182
+ description = f"{ self ._code } : { details } " ,
160
183
)
161
184
)
162
185
return self ._servicer_context .set_details (details )
0 commit comments