Skip to content

Commit

Permalink
Add Trace::Status.from_http_status
Browse files Browse the repository at this point in the history
* based on google apis docs:
  https://cloud.google.com/apis/design/errors#handling_errors

There's also https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
which mentions:
"Servers must not use this table to determine an HTTP status
code to use; the mappings are neither symmetric nor 1-to-1.", so...
  • Loading branch information
duonoid committed Nov 14, 2019
1 parent 468d9d4 commit 1f3d458
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
51 changes: 49 additions & 2 deletions api/lib/opentelemetry/trace/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,56 @@ class Status
# @return [String]
attr_reader :description

# Implemented according to
# https://cloud.google.com/apis/design/errors#handling_errors
#
# Note that some HTTP status do not map 1-to-1 to a gRPC status.
#
# @param code Numeric HTTP status
#
# @return Status
def self.from_http_status(code) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
case code.to_i
when 200
new(OK)
when 400
new(INVALID_ARGUMENT)
# or, possibly (no one-to-one mapping):
# new(FAILED_PRECONDITION)
# new(OUT_OF_RANGE)
when 401
new(UNAUTHENTICATED)
when 403
new(PERMISSION_DENIED)
when 404
new(NOT_FOUND)
when 409
new(ABORTED)
# or, possibly (no one-to-one mapping):
# new(ALREADY_EXISTS)
when 429
new(RESOURCE_EXHAUSTED)
when 499
new(CANCELLED)
when 500
new(DATA_LOSS)
# or, possibly (no one-to-one mapping):
# new(UNKNOWN_ERROR)
# new(INTERNAL_ERROR)
when 501
new(UNIMPLEMENTED)
when 503
new(UNAVAILABLE)
when 504
new(DEADLINE_EXCEEDED)
else
new(UNKNOWN_ERROR)
end
end

# Initialize a Status.
#
# @param [Integer] canonical_code One of the standard GRPC codes: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
# @param [Integer] canonical_code One of the standard gRPC codes: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
# @param [String] description
def initialize(canonical_code, description: '')
@canonical_code = canonical_code
Expand All @@ -36,7 +83,7 @@ def ok?
end

# The following represents the canonical set of status codes of a
# finished {Span}, following the standard GRPC codes:
# finished {Span}, following the standard gRPC codes:
# https://github.com/grpc/grpc/blob/master/doc/statuscodes.md

# The operation completed successfully.
Expand Down
34 changes: 34 additions & 0 deletions api/test/opentelemetry/trace/status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@
require 'test_helper'

describe OpenTelemetry::Trace::Status do
let(:trace_status) { OpenTelemetry::Trace::Status }

describe '.from_http_status' do
it 'returns Status' do
_(trace_status.from_http_status(400)).must_be_kind_of trace_status
end

def assert_http_to_status(http_code, trace_status_code)
_(trace_status.from_http_status(http_code).canonical_code).must_equal trace_status_code
end

it 'maps http 200' do
assert_http_to_status(200, trace_status::OK)
end

it 'maps common 4xx http codes' do
assert_http_to_status(400, trace_status::INVALID_ARGUMENT)
assert_http_to_status(401, trace_status::UNAUTHENTICATED)
assert_http_to_status(403, trace_status::PERMISSION_DENIED)
assert_http_to_status(404, trace_status::NOT_FOUND)
assert_http_to_status(409, trace_status::ABORTED)
assert_http_to_status(429, trace_status::RESOURCE_EXHAUSTED)
assert_http_to_status(499, trace_status::CANCELLED)
end

it 'maps common 5xx http codes' do
assert_http_to_status(500, trace_status::DATA_LOSS)
assert_http_to_status(501, trace_status::UNIMPLEMENTED)
assert_http_to_status(502, trace_status::UNKNOWN_ERROR)
assert_http_to_status(503, trace_status::UNAVAILABLE)
assert_http_to_status(504, trace_status::DEADLINE_EXCEEDED)
end
end

describe '.canonical_code' do
it 'reflects the value passed in' do
status = OpenTelemetry::Trace::Status.new(0)
Expand Down

0 comments on commit 1f3d458

Please sign in to comment.