index_router.go 9.4 KB
Newer Older
7
710leo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
package routes

import (
	"fmt"

	"github.com/didi/nightingale/src/modules/index/cache"
	"github.com/didi/nightingale/src/toolkits/http/render"
	"github.com/didi/nightingale/src/toolkits/stats"

	"github.com/gin-gonic/gin"
	"github.com/toolkits/pkg/errors"
	"github.com/toolkits/pkg/logger"
)

type EndpointsRecv struct {
	Endpoints []string `json:"endpoints"`
}

type MetricList struct {
	Metrics []string `json:"metrics"`
}

func GetMetrics(c *gin.Context) {
	stats.Counter.Set("metric.qp10s", 1)
	recv := EndpointsRecv{}
	errors.Dangerous(c.ShouldBindJSON(&recv))

	m := make(map[string]struct{})
	resp := MetricList{}
	for _, endpoint := range recv.Endpoints {
		metrics := cache.IndexDB.GetMetricsBy(endpoint)
		for _, metric := range metrics {
			if _, exists := m[metric]; !exists {
				m[metric] = struct{}{}
				resp.Metrics = append(resp.Metrics, metric)
			}
		}
	}

	render.Data(c, resp, nil)
}

7
710leo 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
type EndpointRecv struct {
	Endpoints []string `json:"endpoints"`
}

func DelIdxByEndpoint(c *gin.Context) {
	recv := EndpointRecv{}
	errors.Dangerous(c.ShouldBindJSON(&recv))

	for _, endpoint := range recv.Endpoints {
		cache.IndexDB.DelByEndpoint(endpoint)
	}

	render.Data(c, "ok", nil)
}

7
710leo 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
type EndpointMetricRecv struct {
	Endpoints []string `json:"endpoints"`
	Metrics   []string `json:"metrics"`
}

func DelMetrics(c *gin.Context) {
	recv := EndpointMetricRecv{}
	errors.Dangerous(c.ShouldBindJSON(&recv))

	for _, endpoint := range recv.Endpoints {
		if metricIndexMap, exists := cache.IndexDB.GetMetricIndexMap(endpoint); exists {
			for _, metric := range recv.Metrics {
				metricIndexMap.DelMetric(metric)
			}
		}
	}

	render.Data(c, "ok", nil)
}

type IndexTagkvResp struct {
	Endpoints []string         `json:"endpoints"`
	Metric    string           `json:"metric"`
	Tagkv     []*cache.TagPair `json:"tagkv"`
}

func DelCounter(c *gin.Context) {
	recv := IndexTagkvResp{}
	errors.Dangerous(c.ShouldBindJSON(&recv))

	for _, endpoint := range recv.Endpoints {
		metricIndex, exists := cache.IndexDB.GetMetricIndex(endpoint, recv.Metric)
		if !exists {
			continue
		}

		for _, tagPair := range recv.Tagkv {
			for _, v := range tagPair.Values {
7
710leo 已提交
96
				metricIndex.Lock()
7
710leo 已提交
97
				metricIndex.TagkvMap.DelTag(tagPair.Key, v)
7
710leo 已提交
98
				metricIndex.Unlock()
7
710leo 已提交
99 100 101 102 103 104 105 106 107 108 109 110
			}
		}
	}

	render.Data(c, "ok", nil)
}

func GetTagPairs(c *gin.Context) {
	stats.Counter.Set("tag.qp10s", 1)
	recv := EndpointMetricRecv{}
	errors.Dangerous(c.ShouldBindJSON(&recv))

陈键冬 已提交
111
	resp := make([]*IndexTagkvResp, 0)
7
710leo 已提交
112
	for _, metric := range recv.Metrics {
7
710leo 已提交
113
		tagkvFilter := make(map[string]map[string]struct{})
陈键冬 已提交
114
		tagkvs := make([]*cache.TagPair, 0)
7
710leo 已提交
115 116 117 118 119 120 121 122 123

		for _, endpoint := range recv.Endpoints {
			metricIndex, exists := cache.IndexDB.GetMetricIndex(endpoint, metric)
			if !exists {
				logger.Debugf("index not found by %s %s", endpoint, metric)
				stats.Counter.Set("query.tag.miss", 1)
				continue
			}

7
710leo 已提交
124
			metricIndex.RLock()
7
710leo 已提交
125
			tagkvMap := metricIndex.TagkvMap.GetTagkvMap()
7
710leo 已提交
126
			metricIndex.RUnlock()
7
710leo 已提交
127

7
710leo 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			for tagk, tagvs := range tagkvMap {
				tagvFilter, exists := tagkvFilter[tagk]
				if !exists {
					tagvFilter = make(map[string]struct{})
				}

				for _, tagv := range tagvs {
					if _, exists := tagvFilter[tagv]; !exists {
						tagvFilter[tagv] = struct{}{}
					}
				}

				tagkvFilter[tagk] = tagvFilter
			}
		}

		for tagk, tagvFilter := range tagkvFilter {
7
710leo 已提交
145
			var tagvs []string
陈键冬 已提交
146
			for v := range tagvFilter {
7
710leo 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
				tagvs = append(tagvs, v)
			}
			tagkv := &cache.TagPair{
				Key:    tagk,
				Values: tagvs,
			}
			tagkvs = append(tagkvs, tagkv)
		}

		TagkvResp := IndexTagkvResp{
			Endpoints: recv.Endpoints,
			Metric:    metric,
			Tagkv:     tagkvs,
		}
		resp = append(resp, &TagkvResp)
	}
	render.Data(c, resp, nil)
}

type GetIndexByFullTagsRecv struct {
	Endpoints []string         `json:"endpoints"`
	Metric    string           `json:"metric"`
	Tagkv     []*cache.TagPair `json:"tagkv"`
}

type GetIndexByFullTagsResp struct {
	Endpoints []string `json:"endpoints"`
	Metric    string   `json:"metric"`
	Tags      []string `json:"tags"`
	Step      int      `json:"step"`
	DsType    string   `json:"dstype"`
}

func GetIndexByFullTags(c *gin.Context) {
	stats.Counter.Set("counter.qp10s", 1)
陈键冬 已提交
182
	recv := make([]GetIndexByFullTagsRecv, 0)
7
710leo 已提交
183 184 185
	errors.Dangerous(c.ShouldBindJSON(&recv))

	tagFilter := make(map[string]struct{})
陈键冬 已提交
186
	tagsList := make([]string, 0)
7
710leo 已提交
187 188 189 190 191 192 193 194 195 196

	var resp []GetIndexByFullTagsResp
	for _, r := range recv {
		metric := r.Metric
		tagkv := r.Tagkv
		step := 0
		dsType := ""

		for _, endpoint := range r.Endpoints {
			if endpoint == "" {
陈键冬 已提交
197
				logger.Debugf("invalid request: lack of endpoint param:%v\n", r)
7
710leo 已提交
198 199 200 201
				stats.Counter.Set("query.counter.miss", 1)
				continue
			}
			if metric == "" {
陈键冬 已提交
202
				logger.Debugf("invalid request: lack of metric param:%v\n", r)
7
710leo 已提交
203 204 205 206 207 208
				stats.Counter.Set("query.counter.miss", 1)
				continue
			}

			metricIndex, exists := cache.IndexDB.GetMetricIndex(endpoint, metric)
			if !exists {
陈键冬 已提交
209
				logger.Debugf("can't found index by endpoint:%s metric:%v\n", endpoint, metric)
7
710leo 已提交
210 211 212 213
				stats.Counter.Set("query.counter.miss", 1)
				continue
			}

7
710leo 已提交
214
			metricIndex.RLock()
7
710leo 已提交
215 216 217 218 219 220
			if step == 0 || dsType == "" {
				step = metricIndex.Step
				dsType = metricIndex.DsType
			}

			countersMap := metricIndex.CounterMap.GetCounters()
7
710leo 已提交
221
			metricIndex.RUnlock()
7
710leo 已提交
222 223 224 225 226

			tagPairs := cache.GetSortTags(cache.TagPairToMap(tagkv))
			tags := cache.GetAllCounter(tagPairs)

			for _, tag := range tags {
陈键冬 已提交
227 228
				// 校验和 tag 有关的 counter 是否存在
				// 如果一个指标,比如 port.listen 有 name=uic,port=8056 和 name=hsp,port=8002。避免产生 4 个曲线
7
710leo 已提交
229 230
				if _, exists := countersMap[tag]; !exists {
					stats.Counter.Set("query.counter.miss", 1)
陈键冬 已提交
231
					logger.Debugf("can't found counters by endpoint:%s metric:%v tags:%v\n", endpoint, metric, tag)
7
710leo 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
					continue
				}

				if _, exists := tagFilter[tag]; !exists {
					tagsList = append(tagsList, tag)
					tagFilter[tag] = struct{}{}
				}
			}
		}

		resp = append(resp, GetIndexByFullTagsResp{
			Endpoints: r.Endpoints,
			Metric:    r.Metric,
			Tags:      tagsList,
			Step:      step,
			DsType:    dsType,
		})
	}

	render.Data(c, resp, nil)
}

type CludeRecv struct {
	Endpoints []string         `json:"endpoints"`
	Metric    string           `json:"metric"`
	Include   []*cache.TagPair `json:"include"`
	Exclude   []*cache.TagPair `json:"exclude"`
}

type XcludeResp struct {
	Endpoint string   `json:"endpoint"`
	Metric   string   `json:"metric"`
	Tags     []string `json:"tags"`
	Step     int      `json:"step"`
	DsType   string   `json:"dstype"`
}

func GetIndexByClude(c *gin.Context) {
	stats.Counter.Set("xclude.qp10s", 1)

陈键冬 已提交
272
	recv := make([]CludeRecv, 0)
7
710leo 已提交
273 274 275 276 277 278 279 280 281 282 283 284
	errors.Dangerous(c.ShouldBindJSON(&recv))

	var resp []XcludeResp

	for _, r := range recv {
		metric := r.Metric
		includeList := r.Include
		excludeList := r.Exclude
		step := 0
		dsType := ""

		for _, endpoint := range r.Endpoints {
7
710leo 已提交
285 286 287
			tagList := make([]string, 0)
			tagFilter := make(map[string]struct{})

7
710leo 已提交
288
			if endpoint == "" {
陈键冬 已提交
289
				logger.Debugf("invalid request: lack of endpoint param:%v\n", r)
7
710leo 已提交
290 291 292 293
				stats.Counter.Set("xclude.miss", 1)
				continue
			}
			if metric == "" {
陈键冬 已提交
294
				logger.Debugf("invalid request: lack of metric param:%v\n", r)
7
710leo 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308
				stats.Counter.Set("xclude.miss", 1)
				continue
			}

			metricIndex, exists := cache.IndexDB.GetMetricIndex(endpoint, metric)
			if !exists {

				resp = append(resp, XcludeResp{
					Endpoint: endpoint,
					Metric:   metric,
					Tags:     tagList,
					Step:     step,
					DsType:   dsType,
				})
陈键冬 已提交
309
				logger.Debugf("can't found index by endpoint:%s metric:%v\n", endpoint, metric)
7
710leo 已提交
310 311 312 313 314
				stats.Counter.Set("xclude.miss", 1)

				continue
			}

7
710leo 已提交
315
			metricIndex.RLock()
7
710leo 已提交
316 317 318 319 320
			if step == 0 || dsType == "" {
				step = metricIndex.Step
				dsType = metricIndex.DsType
			}

陈键冬 已提交
321 322
			// 校验和 tag 有关的 counter 是否存在
			// 如果一个指标,比如 port.listen 有 name=uic,port=8056 和 name=hsp,port=8002。避免产生 4 个曲线
7
710leo 已提交
323
			counterMap := metricIndex.CounterMap.GetCounters()
7
710leo 已提交
324
			metricIndex.RUnlock()
7
710leo 已提交
325 326 327 328

			var err error
			var tags []string
			if len(includeList) == 0 && len(excludeList) == 0 {
陈键冬 已提交
329
				for counter := range counterMap {
7
710leo 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
					tagList = append(tagList, counter)
				}
				resp = append(resp, XcludeResp{
					Endpoint: endpoint,
					Metric:   metric,
					Tags:     tagList,
					Step:     step,
					DsType:   dsType,
				})
				continue
			} else {
				tags, err = cache.IndexDB.GetIndexByClude(endpoint, metric, includeList, excludeList)
				if err != nil {
					logger.Warning(err)
					continue
				}
			}

			for _, tag := range tags {
陈键冬 已提交
349 350
				//过滤掉空字符串
				if tag == "" {
7
710leo 已提交
351 352 353
					continue
				}

陈键冬 已提交
354 355
				// 校验和 tag 有关的 counter 是否存在
				// 如果一个指标,比如 port.listen 有 name=uic,port=8056 和 name=hsp,port=8002。避免产生 4 个曲线
7
710leo 已提交
356
				if _, exists := counterMap[tag]; !exists {
陈键冬 已提交
357
					logger.Debugf("can't found counters by endpoint:%s metric:%v tags:%v\n", endpoint, metric, tag)
7
710leo 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
					stats.Counter.Set("xclude.miss", 1)
					continue
				}

				if _, exists := tagFilter[tag]; !exists {
					tagList = append(tagList, tag)
					tagFilter[tag] = struct{}{}
				}
			}

			resp = append(resp, XcludeResp{
				Endpoint: endpoint,
				Metric:   metric,
				Tags:     tagList,
				Step:     step,
				DsType:   dsType,
			})
		}
	}

	render.Data(c, resp, nil)
}

func DumpIndex(c *gin.Context) {
	err := cache.Persist("normal")
	errors.Dangerous(err)

	render.Data(c, "ok", nil)
}

func GetIdxFile(c *gin.Context) {
	err := cache.Persist("download")
	errors.Dangerous(err)

	traGz := fmt.Sprintf("%s/%s", cache.Config.PersistDir, "db.tar.gz")
	c.File(traGz)
}