Skip to content

Commit 3317b75

Browse files
authored
sanitize query name label (#12746)
1 parent db42db4 commit 3317b75

File tree

11 files changed

+60
-30
lines changed

11 files changed

+60
-30
lines changed

ydb/core/fq/libs/actors/pending_fetcher.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <ydb/core/fq/libs/control_plane_storage/control_plane_storage.h>
5454
#include <ydb/core/fq/libs/control_plane_storage/events/events.h>
5555
#include <ydb/core/fq/libs/events/events.h>
56+
#include <ydb/core/fq/libs/metrics/sanitize_label.h>
5657
#include <ydb/core/fq/libs/private_client/internal_service.h>
5758

5859
#include <ydb/library/actors/core/log.h>
@@ -360,8 +361,7 @@ class TPendingFetcher : public NActors::TActorBootstrapped<TPendingFetcher> {
360361
const TString queryId = task.query_id().value();
361362
const bool isStreaming = task.query_type() == FederatedQuery::QueryContent::STREAMING;
362363
TString queryIdLabel;
363-
// todo: sanitize query name
364-
TString queryNameLabel = task.query_name();
364+
TString queryNameLabel = SanitizeLabel(task.query_name());
365365
if (task.automatic()) {
366366
queryIdLabel = isStreaming ? "streaming" : "analytics";
367367
} else if (isStreaming) {

ydb/core/fq/libs/actors/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PEERDIR(
4444
ydb/core/fq/libs/db_schema
4545
ydb/core/fq/libs/events
4646
ydb/core/fq/libs/grpc
47+
ydb/core/fq/libs/metrics
4748
ydb/core/fq/libs/private_client
4849
ydb/core/fq/libs/rate_limiter/utils
4950
ydb/core/fq/libs/result_formatter
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
#include "common.h"
1+
#include "sanitize_label.h"
22

33
#include <util/generic/string.h>
44

55
namespace NFq {
66

7-
TString CleanupCounterValueString(const TString& value) {
8-
TString clean;
9-
constexpr auto valueLenghtLimit = 200;
7+
TString SanitizeLabel(const TString& value) {
8+
TString result;
9+
result.reserve(value.size());
10+
constexpr auto labelLengthLimit = 200;
1011

1112
for (auto c : value) {
1213
switch (c) {
@@ -19,13 +20,13 @@ TString CleanupCounterValueString(const TString& value) {
1920
case '\\':
2021
continue;
2122
default:
22-
clean.push_back(c);
23-
if (clean.size() == valueLenghtLimit) {
24-
break;
23+
result.push_back(c);
24+
if (result.size() == labelLengthLimit) {
25+
return result;
2526
}
2627
}
2728
}
28-
return clean;
29+
return result;
2930
}
3031

3132
} // namespace NFq
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <util/generic/fwd.h>
4+
5+
namespace NFq {
6+
7+
TString SanitizeLabel(const TString& value);
8+
9+
} // namespace NFq
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <ydb/core/fq/libs/metrics/sanitize_label.h>
2+
3+
#include <library/cpp/testing/unittest/registar.h>
4+
5+
Y_UNIT_TEST_SUITE(SanitizeLable) {
6+
Y_UNIT_TEST(Empty) {
7+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel(""), "");
8+
}
9+
10+
Y_UNIT_TEST(SkipSingleBadSymbol) {
11+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("|"), "");
12+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("*"), "");
13+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("?"), "");
14+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("\""), "");
15+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("'"), "");
16+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("`"), "");
17+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("\\"), "");
18+
}
19+
20+
Y_UNIT_TEST(SkipBadSymbols) {
21+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel("a|b*c?d\"e'f`g\\h"), "abcdefgh");
22+
}
23+
24+
Y_UNIT_TEST(Truncate200) {
25+
TString s1(400, 'a');
26+
TString s2(200, 'a');
27+
UNIT_ASSERT_VALUES_EQUAL(NFq::SanitizeLabel(s1), s2);
28+
}
29+
}

ydb/core/fq/libs/metrics/ut/ya.make

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
UNITTEST_FOR(ydb/core/fq/libs/metrics)
22

3-
FORK_SUBTESTS()
4-
53
IF (SANITIZER_TYPE OR WITH_VALGRIND)
64
SIZE(MEDIUM)
75
ENDIF()
86

97
SRCS(
108
metrics_ut.cpp
9+
sanitize_label_ut.cpp
1110
)
1211

1312
YQL_LAST_ABI_VERSION()

ydb/core/fq/libs/metrics/ya.make

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
LIBRARY()
22

33
SRCS(
4+
sanitize_label.cpp
45
status_code_counters.cpp
56
)
67

78
PEERDIR(
89
library/cpp/monlib/dynamic_counters
9-
ydb/library/yql/dq/actors/protos
1010
yql/essentials/public/issue
11+
ydb/library/yql/dq/actors/protos
1112
)
1213

1314
YQL_LAST_ABI_VERSION()

ydb/core/fq/libs/row_dispatcher/common.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

ydb/core/fq/libs/row_dispatcher/row_dispatcher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "row_dispatcher.h"
22

33
#include "actors_factory.h"
4-
#include "common.h"
54
#include "coordinator.h"
65
#include "leader_election.h"
76

@@ -15,6 +14,7 @@
1514

1615
#include <ydb/core/fq/libs/actors/logging/log.h>
1716
#include <ydb/core/fq/libs/events/events.h>
17+
#include <ydb/core/fq/libs/metrics/sanitize_label.h>
1818
#include <ydb/core/mon/mon.h>
1919

2020
#include <ydb/core/fq/libs/row_dispatcher/events/data_plane.h>
@@ -598,7 +598,7 @@ void TRowDispatcher::UpdateMetrics() {
598598

599599
void TRowDispatcher::SetQueryMetrics(const TQueryStatKey& queryKey, ui64 unreadBytesMax, ui64 unreadBytesAvg, i64 readLagMessagesMax) {
600600
auto queryGroup = Metrics.Counters->GetSubgroup("query_id", queryKey.QueryId);
601-
auto topicGroup = queryGroup->GetSubgroup("read_group", CleanupCounterValueString(queryKey.ReadGroup));
601+
auto topicGroup = queryGroup->GetSubgroup("read_group", SanitizeLabel(queryKey.ReadGroup));
602602
topicGroup->GetCounter("MaxUnreadBytes")->Set(unreadBytesMax);
603603
topicGroup->GetCounter("AvgUnreadBytes")->Set(unreadBytesAvg);
604604
topicGroup->GetCounter("MaxReadLag")->Set(readLagMessagesMax);
@@ -740,7 +740,7 @@ void TRowDispatcher::Handle(NFq::TEvRowDispatcher::TEvStartSession::TPtr& ev) {
740740
LOG_ROW_DISPATCHER_DEBUG("Received TEvStartSession from " << ev->Sender << ", read group " << ev->Get()->Record.GetSource().GetReadGroup() << ", topicPath " << ev->Get()->Record.GetSource().GetTopicPath() <<
741741
" part id " << ev->Get()->Record.GetPartitionId() << " query id " << ev->Get()->Record.GetQueryId() << " cookie " << ev->Cookie);
742742
auto queryGroup = Metrics.Counters->GetSubgroup("query_id", ev->Get()->Record.GetQueryId());
743-
auto topicGroup = queryGroup->GetSubgroup("read_group", CleanupCounterValueString(ev->Get()->Record.GetSource().GetReadGroup()));
743+
auto topicGroup = queryGroup->GetSubgroup("read_group", SanitizeLabel(ev->Get()->Record.GetSource().GetReadGroup()));
744744
topicGroup->GetCounter("StartSession", true)->Inc();
745745

746746
NodesTracker.AddNode(ev->Sender.NodeId());

ydb/core/fq/libs/row_dispatcher/topic_session.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "topic_session.h"
22

3-
#include "common.h"
4-
53
#include <ydb/core/fq/libs/actors/logging/log.h>
4+
#include <ydb/core/fq/libs/metrics/sanitize_label.h>
65
#include <ydb/core/fq/libs/row_dispatcher/events/data_plane.h>
76
#include <ydb/core/fq/libs/row_dispatcher/format_handler/format_handler.h>
87

@@ -25,7 +24,7 @@ namespace {
2524

2625
struct TTopicSessionMetrics {
2726
void Init(const ::NMonitoring::TDynamicCounterPtr& counters, const TString& topicPath, ui32 partitionId) {
28-
TopicGroup = counters->GetSubgroup("topic", CleanupCounterValueString(topicPath));
27+
TopicGroup = counters->GetSubgroup("topic", SanitizeLabel(topicPath));
2928
AllSessionsDataRate = counters->GetCounter("AllSessionsDataRate", true);
3029

3130
PartitionGroup = TopicGroup->GetSubgroup("partition", ToString(partitionId));
@@ -107,7 +106,7 @@ class TTopicSession : public TActorBootstrapped<TTopicSession> {
107106
}
108107
Y_UNUSED(TDuration::TryParse(Settings.GetSource().GetReconnectPeriod(), ReconnectPeriod));
109108
auto queryGroup = Counters->GetSubgroup("query_id", ev->Get()->Record.GetQueryId());
110-
auto topicGroup = queryGroup->GetSubgroup("read_group", CleanupCounterValueString(readGroup));
109+
auto topicGroup = queryGroup->GetSubgroup("read_group", SanitizeLabel(readGroup));
111110
FilteredDataRate = topicGroup->GetCounter("FilteredDataRate", true);
112111
RestartSessionByOffsetsByQuery = counters->GetCounter("RestartSessionByOffsetsByQuery", true);
113112
}

0 commit comments

Comments
 (0)