Skip to content

Commit b13e3e2

Browse files
authored
Add status code to OTLP grpc trace log (#1792)
1 parent 8bfb9a3 commit b13e3e2

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

exporters/otlp/BUILD

+19
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ cc_library(
4646
],
4747
)
4848

49+
cc_library(
50+
name = "otlp_grpc_utils",
51+
srcs = [
52+
"src/otlp_grpc_utils.cc",
53+
],
54+
hdrs = [
55+
"include/opentelemetry/exporters/otlp/otlp_grpc_utils.h",
56+
],
57+
strip_include_prefix = "include",
58+
tags = ["otlp"],
59+
deps = [
60+
"//api",
61+
"//sdk:headers",
62+
"@com_github_grpc_grpc//:grpc++",
63+
],
64+
)
65+
4966
cc_library(
5067
name = "otlp_grpc_client",
5168
srcs = [
@@ -82,6 +99,7 @@ cc_library(
8299
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h",
83100
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h",
84101
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
102+
"include/opentelemetry/exporters/otlp/otlp_grpc_utils.h",
85103
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
86104
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
87105
],
@@ -93,6 +111,7 @@ cc_library(
93111
deps = [
94112
":otlp_recordable",
95113
":otlp_grpc_client",
114+
":otlp_grpc_utils",
96115
"//ext:headers",
97116
"//sdk/src/trace",
98117

exporters/otlp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_library(
22
opentelemetry_otlp_recordable
33
src/otlp_log_recordable.cc src/otlp_recordable.cc
44
src/otlp_populate_attribute_utils.cc src/otlp_recordable_utils.cc
5-
src/otlp_metric_utils.cc)
5+
src/otlp_metric_utils.cc src/otlp_grpc_utils.cc)
66
set_target_properties(opentelemetry_otlp_recordable PROPERTIES EXPORT_NAME
77
otlp_recordable)
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <grpcpp/grpcpp.h>
7+
8+
#include "opentelemetry/sdk/version/version.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
12+
namespace exporter
13+
{
14+
namespace otlp
15+
{
16+
namespace grpc_utils
17+
{
18+
19+
const char *grpc_status_code_to_string(::grpc::StatusCode status_code);
20+
21+
} // namespace grpc_utils
22+
} // namespace otlp
23+
} // namespace exporter
24+
25+
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/src/otlp_grpc_exporter.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "opentelemetry/exporters/otlp/otlp_recordable_utils.h"
1212
#include "opentelemetry/sdk_config.h"
1313

14-
#include <grpcpp/grpcpp.h>
14+
#include "opentelemetry/exporters/otlp/otlp_grpc_utils.h"
1515

1616
OPENTELEMETRY_BEGIN_NAMESPACE
1717
namespace exporter
@@ -64,9 +64,9 @@ sdk::common::ExportResult OtlpGrpcExporter::Export(
6464

6565
if (!status.ok())
6666
{
67-
68-
OTEL_INTERNAL_LOG_ERROR(
69-
"[OTLP TRACE GRPC Exporter] Export() failed: " << status.error_message());
67+
OTEL_INTERNAL_LOG_ERROR("[OTLP TRACE GRPC Exporter] Export() failed with status_code: \""
68+
<< grpc_utils::grpc_status_code_to_string(status.error_code())
69+
<< "\" error_message: \"" << status.error_message() << "\"");
7070
return sdk::common::ExportResult::kFailure;
7171
}
7272
return sdk::common::ExportResult::kSuccess;

exporters/otlp/src/otlp_grpc_utils.cc

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "opentelemetry/exporters/otlp/otlp_grpc_utils.h"
5+
6+
OPENTELEMETRY_BEGIN_NAMESPACE
7+
8+
namespace exporter
9+
{
10+
namespace otlp
11+
{
12+
namespace grpc_utils
13+
{
14+
15+
const char *grpc_status_code_to_string(::grpc::StatusCode status_code)
16+
{
17+
switch (status_code)
18+
{
19+
case GRPC_STATUS_OK:
20+
return "OK";
21+
case GRPC_STATUS_CANCELLED:
22+
return "CANCELLED";
23+
case GRPC_STATUS_UNKNOWN:
24+
return "UNKNOWN";
25+
case GRPC_STATUS_INVALID_ARGUMENT:
26+
return "INVALID_ARGUMENT";
27+
case GRPC_STATUS_DEADLINE_EXCEEDED:
28+
return "DEADLINE_EXCEEDED";
29+
case GRPC_STATUS_NOT_FOUND:
30+
return "NOT_FOUND";
31+
case GRPC_STATUS_ALREADY_EXISTS:
32+
return "ALREADY_EXISTS";
33+
case GRPC_STATUS_PERMISSION_DENIED:
34+
return "PERMISSION_DENIED";
35+
case GRPC_STATUS_UNAUTHENTICATED:
36+
return "UNAUTHENTICATED";
37+
case GRPC_STATUS_RESOURCE_EXHAUSTED:
38+
return "RESOURCE_EXHAUSTED";
39+
case GRPC_STATUS_FAILED_PRECONDITION:
40+
return "FAILED_PRECONDITION";
41+
case GRPC_STATUS_ABORTED:
42+
return "ABORTED";
43+
case GRPC_STATUS_OUT_OF_RANGE:
44+
return "OUT_OF_RANGE";
45+
case GRPC_STATUS_UNIMPLEMENTED:
46+
return "UNIMPLEMENTED";
47+
case GRPC_STATUS_INTERNAL:
48+
return "INTERNAL";
49+
case GRPC_STATUS_UNAVAILABLE:
50+
return "UNAVAILABLE";
51+
case GRPC_STATUS_DATA_LOSS:
52+
return "DATA_LOSS";
53+
default:
54+
return "UNKNOWN";
55+
}
56+
}
57+
58+
} // namespace grpc_utils
59+
} // namespace otlp
60+
} // namespace exporter
61+
62+
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)