1+ /*
2+ * Copyright 2024 NAVER Corp.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .navercorp .pinpoint .collector .applicationmap .redis ;
17+
18+ import com .navercorp .pinpoint .collector .applicationmap .dao .InboundDao ;
19+ import com .navercorp .pinpoint .collector .applicationmap .redis .schema .ApplicationMapTable ;
20+ import com .navercorp .pinpoint .collector .applicationmap .redis .schema .TimeSeriesKey ;
21+ import com .navercorp .pinpoint .collector .applicationmap .redis .schema .TimeSeriesValue ;
22+ import com .navercorp .pinpoint .collector .dao .hbase .IgnoreStatFilter ;
23+ import com .navercorp .pinpoint .collector .dao .hbase .statistics .MapLinkConfiguration ;
24+ import com .navercorp .pinpoint .collector .applicationmap .redis .statistics .RedisBulkWriter ;
25+ import com .navercorp .pinpoint .common .server .util .AcceptedTimeService ;
26+ import com .navercorp .pinpoint .common .server .util .ApplicationMapStatisticsUtils ;
27+ import com .navercorp .pinpoint .common .server .util .TimeSlot ;
28+ import com .navercorp .pinpoint .common .trace .HistogramSchema ;
29+ import com .navercorp .pinpoint .common .trace .ServiceType ;
30+ import org .apache .logging .log4j .LogManager ;
31+ import org .apache .logging .log4j .Logger ;
32+ import org .springframework .beans .factory .annotation .Qualifier ;
33+ import org .springframework .stereotype .Repository ;
34+
35+ import java .util .Objects ;
36+
37+ /**
38+ * @author intr3p1d
39+ */
40+ @ Repository
41+ public class RedisInboundDao implements InboundDao {
42+
43+ private final Logger logger = LogManager .getLogger (this .getClass ());
44+
45+ private final AcceptedTimeService acceptedTimeService ;
46+ private final IgnoreStatFilter ignoreStatFilter ;
47+ private final RedisBulkWriter bulkWriter ;
48+ private final MapLinkConfiguration mapLinkConfiguration ;
49+
50+ public RedisInboundDao (
51+ MapLinkConfiguration mapLinkConfiguration ,
52+ AcceptedTimeService acceptedTimeService ,
53+ IgnoreStatFilter ignoreStatFilter ,
54+ @ Qualifier ("inboundBulkWriter" ) RedisBulkWriter bulkWriter
55+ ) {
56+ this .mapLinkConfiguration = Objects .requireNonNull (mapLinkConfiguration , "mapLinkConfiguration" );
57+ this .acceptedTimeService = Objects .requireNonNull (acceptedTimeService , "acceptedTimeService" );
58+ this .ignoreStatFilter = Objects .requireNonNull (ignoreStatFilter , "ignoreStatFilter" );
59+ this .bulkWriter = Objects .requireNonNull (bulkWriter , "inboundBulkWriter" );
60+ }
61+
62+
63+ @ Override
64+ public void update (
65+ String srcServiceName , String srcApplicationName , ServiceType srcApplicationType ,
66+ String destServiceName , String destApplicationName , ServiceType destApplicationType ,
67+ String srcHost , int elapsed , boolean isError
68+ ) {
69+ Objects .requireNonNull (srcServiceName , "srcServiceName" );
70+ Objects .requireNonNull (destServiceName , "destServiceName" );
71+ Objects .requireNonNull (srcApplicationName , "srcApplicationName" );
72+ Objects .requireNonNull (destServiceName , "destApplicationName" );
73+
74+ if (logger .isDebugEnabled ()) {
75+ logger .debug ("[Inbound] {} {}({}) <- {} {}({})[{}]" ,
76+ destServiceName , destApplicationName , destApplicationType ,
77+ srcServiceName , srcApplicationName , srcApplicationType , srcHost
78+ );
79+ }
80+
81+ if (ignoreStatFilter .filter (srcApplicationType , srcHost )) {
82+ logger .debug ("[Ignore-Inbound] {} {}({}) <- {} {}({})[{}]" ,
83+ destServiceName , destApplicationName , destApplicationType ,
84+ srcServiceName , srcApplicationName , srcApplicationType , srcHost
85+ );
86+ return ;
87+ }
88+
89+ final short srcSlotNumber = ApplicationMapStatisticsUtils .getSlotNumber (srcApplicationType , elapsed , isError );
90+ HistogramSchema histogramSchema = srcApplicationType .getHistogramSchema ();
91+ final long acceptedTime = acceptedTimeService .getAcceptedTime ();
92+
93+ // for inbound, main is destination
94+ // and sub is source
95+ final TimeSeriesKey applicationTypeKey = new TimeSeriesKey (
96+ ApplicationMapTable .Inbound , "tenantId" ,
97+ destServiceName , destApplicationName ,
98+ srcServiceName , srcApplicationName , srcSlotNumber
99+ );
100+ TimeSeriesValue addOne = new TimeSeriesValue (acceptedTime );
101+ this .bulkWriter .increment (applicationTypeKey , addOne );
102+
103+ if (mapLinkConfiguration .isEnableAvg ()) {
104+ final TimeSeriesKey sumStatKey = new TimeSeriesKey (
105+ ApplicationMapTable .Inbound , "tenantId" ,
106+ destServiceName , destApplicationName ,
107+ srcServiceName , srcApplicationName ,
108+ histogramSchema .getSumStatSlot ().getSlotTime ()
109+ );
110+ final TimeSeriesValue sumValue = new TimeSeriesValue (acceptedTime );
111+ this .bulkWriter .increment (sumStatKey , sumValue , elapsed );
112+ }
113+ if (mapLinkConfiguration .isEnableMax ()) {
114+ final TimeSeriesKey maxStatKey = new TimeSeriesKey (
115+ ApplicationMapTable .Inbound , "tenantId" ,
116+ destServiceName , destApplicationName ,
117+ srcServiceName , srcApplicationName ,
118+ histogramSchema .getMaxStatSlot ().getSlotTime ()
119+ );
120+ final TimeSeriesValue maxValue = new TimeSeriesValue (acceptedTime );
121+ this .bulkWriter .updateMax (maxStatKey , maxValue , elapsed );
122+ }
123+
124+ }
125+
126+ @ Override
127+ public void flushLink () {
128+ this .bulkWriter .flushLink ();
129+ }
130+
131+ @ Override
132+ public void flushAvgMax () {
133+ this .bulkWriter .flushAvgMax ();
134+ }
135+
136+ }
0 commit comments