Skip to content

Commit 6b36bb9

Browse files
dmehaladgoffredo
andauthored
chore: enforce codestyle (#60)
Co-authored-by: David Goffredo <[email protected]>
1 parent b2266f6 commit 6b36bb9

30 files changed

+685
-377
lines changed

.circleci/config.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ jobs:
117117
path: test/logs/test.log
118118
destination: test.log
119119

120+
format:
121+
docker:
122+
- image: "datadog/docker-library:dd-trace-cpp-ci"
123+
resource_class: small
124+
steps:
125+
- checkout
126+
- run:
127+
name: Install Python dependencies
128+
command: |
129+
pip install yapf
130+
update-alternatives --install /usr/local/bin/yapf3 yapf3 /usr/local/bin/yapf 100
131+
- run:
132+
name: Lint
133+
command: |
134+
touch nginx-version-info
135+
make lint
136+
120137
shellcheck:
121138
docker:
122139
- image: koalaman/shellcheck-alpine:stable
@@ -128,6 +145,7 @@ jobs:
128145
workflows:
129146
build-and-test:
130147
jobs:
148+
- format
131149
- shellcheck:
132150
name: "run shellcheck on shell scripts"
133151
filters:

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dd-trace-cpp/.clang-format

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
.*.sw?
1111
.vscode/
1212

13-
# clang-format config copied from dd-opentracing-cpp (see `make format`)
14-
/.clang-format
15-
1613
# file used as part of the build configuration
1714
/nginx-version-info
1815

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ nginx-version-info:
4444
dd-trace-cpp/.clang-format: dd-trace-cpp/.git
4545

4646
.clang-format: dd-trace-cpp/.clang-format
47-
cp $< $@
4847

4948
.PHONY: format
5049
format: .clang-format
51-
find src/ -type f \( -name '*.h' -o -name '*.cpp' \) -print0 | xargs -0 clang-format-14 -i --style=file
52-
find bin/ -type f -name '*.py' -print0 | xargs -0 yapf3 -i
53-
test/bin/format
50+
bin/format.sh
51+
52+
.PHONY: lint
53+
lint: .clang-format
54+
bin/lint.sh
5455

5556
.PHONY: clean
5657
clean:

bin/format.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
# Format the codebase to follow this project's style.
3+
4+
if ! [ -e .clang-format ]; then
5+
>&2 echo '.clang-format file is missing. Run "make format".'
6+
exit 1
7+
fi
8+
9+
find src/ -type f \( -name '*.h' -o -name '*.cpp' \) -print0 | xargs -0 clang-format-14 -i --style=file
10+
find bin/ -type f -name '*.py' -print0 | xargs -0 yapf3 -i
11+
12+
yapf3 --recursive --in-place "$@" "test/"

bin/lint.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
# Print any discrepancies between the formatting of the code and the expected
3+
# style.
4+
5+
if ! [ -e .clang-format ]; then
6+
>&2 echo '.clang-format file is missing. Run "make lint".'
7+
exit 1
8+
fi
9+
10+
error_messages=''
11+
12+
find src/ -type f \( -name '*.h' -o -name '*.cpp' \) -print0 | xargs -0 clang-format-14 --Werror --dry-run --style=file
13+
rc=$?
14+
if [ "$rc" -ne 0 ]; then
15+
error_messages=$(printf '%s\nC++ formatter reported formatting differences in src/ and returned error status %d.\n' "$error_messages" "$rc")
16+
fi
17+
18+
find bin/ -type f -name '*.py' -print0 | xargs -0 yapf3 --diff
19+
rc=$?
20+
if [ "$rc" -ne 0 ]; then
21+
error_messages=$(printf '%s\nPython formatter reported formatting differences in bin/ and returned error status %d.\n' "$error_messages" "$rc")
22+
fi
23+
24+
yapf3 --recursive --diff "$@" "test/"
25+
rc=$?
26+
if [ "$rc" -ne 0 ]; then
27+
error_messages=$(printf '%s\nPython formatter reported formatting differences in test/ and returned error status %d.\n' "$error_messages" "$rc")
28+
fi
29+
30+
if [ -n "$error_messages" ]; then
31+
>&2 echo "$error_messages"
32+
exit 1
33+
fi

src/datadog_conf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace nginx {
77

88
bool operator==(const conf_directive_source_location_t& left,
99
const conf_directive_source_location_t& right) {
10-
return str(left.file_name) == str(right.file_name) && left.line == right.line &&
10+
return str(left.file_name) == str(right.file_name) &&
11+
left.line == right.line &&
1112
str(left.directive_name) == str(right.directive_name);
1213
}
1314

src/datadog_conf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ struct datadog_loc_conf_t {
213213
// `allow_sampling_delegation_in_subrequests_directive` is the source location
214214
// of the `datadog_allow_sampling_delegation_in_subrequests` directive that
215215
// applies this location, if any.
216-
conf_directive_source_location_t allow_sampling_delegation_in_subrequests_directive;
216+
conf_directive_source_location_t
217+
allow_sampling_delegation_in_subrequests_directive;
217218
};
218219

219220
} // namespace nginx

src/datadog_conf_handler.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace datadog {
66
namespace nginx {
77
/* The eight fixed arguments */
8-
static ngx_uint_t argument_number[] = {NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_TAKE2,
9-
NGX_CONF_TAKE3, NGX_CONF_TAKE4, NGX_CONF_TAKE5,
10-
NGX_CONF_TAKE6, NGX_CONF_TAKE7};
8+
static ngx_uint_t argument_number[] = {
9+
NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_TAKE2, NGX_CONF_TAKE3,
10+
NGX_CONF_TAKE4, NGX_CONF_TAKE5, NGX_CONF_TAKE6, NGX_CONF_TAKE7};
1111

1212
ngx_int_t datadog_conf_handler(const DatadogConfHandlerConfig &args) noexcept {
1313
ngx_conf_t *const cf = args.conf;
@@ -30,7 +30,8 @@ ngx_int_t datadog_conf_handler(const DatadogConfHandlerConfig &args) noexcept {
3030
continue;
3131
}
3232

33-
if (args.skip_this_module && cf->cycle->modules[i] == &ngx_http_datadog_module) {
33+
if (args.skip_this_module &&
34+
cf->cycle->modules[i] == &ngx_http_datadog_module) {
3435
continue;
3536
}
3637

@@ -57,14 +58,15 @@ ngx_int_t datadog_conf_handler(const DatadogConfHandlerConfig &args) noexcept {
5758
}
5859

5960
if (!(cmd->type & NGX_CONF_BLOCK) && last != NGX_OK) {
60-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "directive \"%s\" is not terminated by \";\"",
61+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
62+
"directive \"%s\" is not terminated by \";\"",
6163
name->data);
6264
return NGX_ERROR;
6365
}
6466

6567
if ((cmd->type & NGX_CONF_BLOCK) && last != NGX_CONF_BLOCK_START) {
66-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "directive \"%s\" has no opening \"{\"",
67-
name->data);
68+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
69+
"directive \"%s\" has no opening \"{\"", name->data);
6870
return NGX_ERROR;
6971
}
7072

@@ -122,25 +124,29 @@ ngx_int_t datadog_conf_handler(const DatadogConfHandlerConfig &args) noexcept {
122124
return NGX_ERROR;
123125
}
124126

125-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"%s\" directive %s", name->data, rv);
127+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"%s\" directive %s",
128+
name->data, rv);
126129

127130
return NGX_ERROR;
128131
}
129132
}
130133

131134
if (found) {
132-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"%s\" directive is not allowed here", name->data);
135+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
136+
"\"%s\" directive is not allowed here", name->data);
133137

134138
return NGX_ERROR;
135139
}
136140

137-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unknown directive \"%s\"", name->data);
141+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unknown directive \"%s\"",
142+
name->data);
138143

139144
return NGX_ERROR;
140145

141146
invalid:
142147

143-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid number of arguments in \"%s\" directive",
148+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
149+
"invalid number of arguments in \"%s\" directive",
144150
name->data);
145151

146152
return NGX_ERROR;

src/datadog_context.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ void DatadogContext::on_change_block(ngx_http_request_t *request,
2828

2929
// This is a new subrequest, so add a RequestTracing for it.
3030
// TODO: Should `active_span` be `request_span` instead?
31-
traces_.emplace_back(request, core_loc_conf, loc_conf, &traces_[0].active_span());
31+
traces_.emplace_back(request, core_loc_conf, loc_conf,
32+
&traces_[0].active_span());
3233
}
3334

3435
void DatadogContext::on_log_request(ngx_http_request_t *request) {
3536
auto trace = find_trace(request);
3637
if (trace == nullptr) {
37-
throw std::runtime_error{"on_log_request failed: could not find request trace"};
38+
throw std::runtime_error{
39+
"on_log_request failed: could not find request trace"};
3840
}
3941
trace->on_log_request();
4042
}
4143

42-
ngx_str_t DatadogContext::lookup_propagation_header_variable_value(ngx_http_request_t *request,
43-
std::string_view key) {
44+
ngx_str_t DatadogContext::lookup_propagation_header_variable_value(
45+
ngx_http_request_t *request, std::string_view key) {
4446
auto trace = find_trace(request);
4547
if (trace == nullptr) {
4648
throw std::runtime_error{
@@ -50,11 +52,12 @@ ngx_str_t DatadogContext::lookup_propagation_header_variable_value(ngx_http_requ
5052
return trace->lookup_propagation_header_variable_value(key);
5153
}
5254

53-
ngx_str_t DatadogContext::lookup_span_variable_value(ngx_http_request_t *request,
54-
std::string_view key) {
55+
ngx_str_t DatadogContext::lookup_span_variable_value(
56+
ngx_http_request_t *request, std::string_view key) {
5557
auto trace = find_trace(request);
5658
if (trace == nullptr) {
57-
throw std::runtime_error{"lookup_span_variable_value failed: could not find request trace"};
59+
throw std::runtime_error{
60+
"lookup_span_variable_value failed: could not find request trace"};
5861
}
5962
return trace->lookup_span_variable_value(key);
6063
}
@@ -64,21 +67,24 @@ ngx_str_t DatadogContext::lookup_sampling_delegation_response_variable_value(
6467
auto trace = find_trace(request);
6568
if (trace == nullptr) {
6669
throw std::runtime_error{
67-
"lookup_sampling_delegation_response_variable_value failed: could not find request trace"};
70+
"lookup_sampling_delegation_response_variable_value failed: could not "
71+
"find request trace"};
6872
}
6973
return trace->lookup_sampling_delegation_response_variable_value();
7074
}
7175

7276
RequestTracing *DatadogContext::find_trace(ngx_http_request_t *request) {
73-
const auto found = std::find_if(traces_.begin(), traces_.end(),
74-
[=](const auto &trace) { return trace.request() == request; });
77+
const auto found = std::find_if(
78+
traces_.begin(), traces_.end(),
79+
[=](const auto &trace) { return trace.request() == request; });
7580
if (found != traces_.end()) {
7681
return &*found;
7782
}
7883
return nullptr;
7984
}
8085

81-
const RequestTracing *DatadogContext::find_trace(ngx_http_request_t *request) const {
86+
const RequestTracing *DatadogContext::find_trace(
87+
ngx_http_request_t *request) const {
8288
return const_cast<DatadogContext *>(this)->find_trace(request);
8389
}
8490

@@ -87,7 +93,8 @@ static void cleanup_datadog_context(void *data) noexcept {
8793
}
8894

8995
static ngx_pool_cleanup_t *find_datadog_cleanup(ngx_http_request_t *request) {
90-
for (auto cleanup = request->pool->cleanup; cleanup; cleanup = cleanup->next) {
96+
for (auto cleanup = request->pool->cleanup; cleanup;
97+
cleanup = cleanup->next) {
9198
if (cleanup->handler == cleanup_datadog_context) {
9299
return cleanup;
93100
}
@@ -96,8 +103,8 @@ static ngx_pool_cleanup_t *find_datadog_cleanup(ngx_http_request_t *request) {
96103
}
97104

98105
DatadogContext *get_datadog_context(ngx_http_request_t *request) noexcept {
99-
auto context =
100-
static_cast<DatadogContext *>(ngx_http_get_module_ctx(request, ngx_http_datadog_module));
106+
auto context = static_cast<DatadogContext *>(
107+
ngx_http_get_module_ctx(request, ngx_http_datadog_module));
101108
if (context != nullptr || !request->internal) {
102109
return context;
103110
}
@@ -114,7 +121,8 @@ DatadogContext *get_datadog_context(ngx_http_request_t *request) noexcept {
114121
// If we found a context, attach with ngx_http_set_ctx so that we don't have
115122
// to loop through the cleanup handlers again.
116123
if (context != nullptr) {
117-
ngx_http_set_ctx(request, static_cast<void *>(context), ngx_http_datadog_module);
124+
ngx_http_set_ctx(request, static_cast<void *>(context),
125+
ngx_http_datadog_module);
118126
}
119127

120128
return context;
@@ -138,7 +146,8 @@ void set_datadog_context(ngx_http_request_t *request, DatadogContext *context) {
138146
}
139147
cleanup->data = static_cast<void *>(context);
140148
cleanup->handler = cleanup_datadog_context;
141-
ngx_http_set_ctx(request, static_cast<void *>(context), ngx_http_datadog_module);
149+
ngx_http_set_ctx(request, static_cast<void *>(context),
150+
ngx_http_datadog_module);
142151
}
143152

144153
// Supports early destruction of the DatadogContext (in case of an
@@ -147,7 +156,8 @@ void destroy_datadog_context(ngx_http_request_t *request) noexcept {
147156
auto cleanup = find_datadog_cleanup(request);
148157
if (cleanup == nullptr) {
149158
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
150-
"Unable to find Datadog cleanup handler for request %p", request);
159+
"Unable to find Datadog cleanup handler for request %p",
160+
request);
151161
return;
152162
}
153163
delete static_cast<DatadogContext *>(cleanup->data);

0 commit comments

Comments
 (0)