Skip to content

Commit

Permalink
Add status code to OTLP grpc trace log (#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Nov 25, 2022
1 parent 8bfb9a3 commit b13e3e2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
19 changes: 19 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ cc_library(
],
)

cc_library(
name = "otlp_grpc_utils",
srcs = [
"src/otlp_grpc_utils.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_grpc_utils.h",
],
strip_include_prefix = "include",
tags = ["otlp"],
deps = [
"//api",
"//sdk:headers",
"@com_github_grpc_grpc//:grpc++",
],
)

cc_library(
name = "otlp_grpc_client",
srcs = [
Expand Down Expand Up @@ -82,6 +99,7 @@ cc_library(
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_utils.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
Expand All @@ -93,6 +111,7 @@ cc_library(
deps = [
":otlp_recordable",
":otlp_grpc_client",
":otlp_grpc_utils",
"//ext:headers",
"//sdk/src/trace",

Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_library(
opentelemetry_otlp_recordable
src/otlp_log_recordable.cc src/otlp_recordable.cc
src/otlp_populate_attribute_utils.cc src/otlp_recordable_utils.cc
src/otlp_metric_utils.cc)
src/otlp_metric_utils.cc src/otlp_grpc_utils.cc)
set_target_properties(opentelemetry_otlp_recordable PROPERTIES EXPORT_NAME
otlp_recordable)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <grpcpp/grpcpp.h>

#include "opentelemetry/sdk/version/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE

namespace exporter
{
namespace otlp
{
namespace grpc_utils
{

const char *grpc_status_code_to_string(::grpc::StatusCode status_code);

} // namespace grpc_utils
} // namespace otlp
} // namespace exporter

OPENTELEMETRY_END_NAMESPACE
8 changes: 4 additions & 4 deletions exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "opentelemetry/exporters/otlp/otlp_recordable_utils.h"
#include "opentelemetry/sdk_config.h"

#include <grpcpp/grpcpp.h>
#include "opentelemetry/exporters/otlp/otlp_grpc_utils.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
Expand Down Expand Up @@ -64,9 +64,9 @@ sdk::common::ExportResult OtlpGrpcExporter::Export(

if (!status.ok())
{

OTEL_INTERNAL_LOG_ERROR(
"[OTLP TRACE GRPC Exporter] Export() failed: " << status.error_message());
OTEL_INTERNAL_LOG_ERROR("[OTLP TRACE GRPC Exporter] Export() failed with status_code: \""
<< grpc_utils::grpc_status_code_to_string(status.error_code())
<< "\" error_message: \"" << status.error_message() << "\"");
return sdk::common::ExportResult::kFailure;
}
return sdk::common::ExportResult::kSuccess;
Expand Down
62 changes: 62 additions & 0 deletions exporters/otlp/src/otlp_grpc_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_grpc_utils.h"

OPENTELEMETRY_BEGIN_NAMESPACE

namespace exporter
{
namespace otlp
{
namespace grpc_utils
{

const char *grpc_status_code_to_string(::grpc::StatusCode status_code)
{
switch (status_code)
{
case GRPC_STATUS_OK:
return "OK";
case GRPC_STATUS_CANCELLED:
return "CANCELLED";
case GRPC_STATUS_UNKNOWN:
return "UNKNOWN";
case GRPC_STATUS_INVALID_ARGUMENT:
return "INVALID_ARGUMENT";
case GRPC_STATUS_DEADLINE_EXCEEDED:
return "DEADLINE_EXCEEDED";
case GRPC_STATUS_NOT_FOUND:
return "NOT_FOUND";
case GRPC_STATUS_ALREADY_EXISTS:
return "ALREADY_EXISTS";
case GRPC_STATUS_PERMISSION_DENIED:
return "PERMISSION_DENIED";
case GRPC_STATUS_UNAUTHENTICATED:
return "UNAUTHENTICATED";
case GRPC_STATUS_RESOURCE_EXHAUSTED:
return "RESOURCE_EXHAUSTED";
case GRPC_STATUS_FAILED_PRECONDITION:
return "FAILED_PRECONDITION";
case GRPC_STATUS_ABORTED:
return "ABORTED";
case GRPC_STATUS_OUT_OF_RANGE:
return "OUT_OF_RANGE";
case GRPC_STATUS_UNIMPLEMENTED:
return "UNIMPLEMENTED";
case GRPC_STATUS_INTERNAL:
return "INTERNAL";
case GRPC_STATUS_UNAVAILABLE:
return "UNAVAILABLE";
case GRPC_STATUS_DATA_LOSS:
return "DATA_LOSS";
default:
return "UNKNOWN";
}
}

} // namespace grpc_utils
} // namespace otlp
} // namespace exporter

OPENTELEMETRY_END_NAMESPACE

0 comments on commit b13e3e2

Please sign in to comment.