File: nrelastic.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (176 lines) | stat: -rw-r--r-- 4,433 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// Package nrelasticsearch instruments https://github.com/elastic/go-elasticsearch.
//
// Use this package to instrument your elasticsearch v7 calls without having to
// manually create DatastoreSegments.
package nrelasticsearch

import (
	"net/http"
	"strings"

	"github.com/newrelic/go-agent/v3/internal"
	newrelic "github.com/newrelic/go-agent/v3/newrelic"
)

func init() { internal.TrackUsage("integration", "datastore", "elasticsearch") }

func parseRequest(r *http.Request) (segment newrelic.DatastoreSegment) {

	segment.StartTime = newrelic.FromContext(r.Context()).StartSegmentNow()
	segment.Product = newrelic.DatastoreElasticsearch

	path := strings.TrimPrefix(r.URL.Path, "/")
	method := r.Method

	if "" == path {
		switch method {
		case "GET":
			segment.Operation = "info"
		case "HEAD":
			segment.Operation = "ping"
		}
		return
	}

	segments := strings.Split(path, "/")
	for idx, s := range segments {
		switch s {
		case "_alias",
			"_aliases",
			"_analyze",
			"_bulk",
			"_cache",
			"_cat",
			"_clone",
			"_close",
			"_cluster",
			"_count",
			"_create",
			"_delete_by_query",
			"_explain",
			"_field_caps",
			"_flush",
			"_forcemerge",
			"_ingest",
			"_mapping",
			"_mappings",
			"_mget",
			"_msearch",
			"_mtermvectors",
			"_nodes",
			"_open",
			"_rank_eval",
			"_recovery",
			"_refresh",
			"_reindex",
			"_remote",
			"_render",
			"_rollover",
			"_scripts",
			"_search_shards",
			"_segments",
			"_settings",
			"_shard_stores",
			"_shrink",
			"_snapshot",
			"_source",
			"_split",
			"_stats",
			"_tasks",
			"_template",
			"_termvectors",
			"_update",
			"_update_by_query",
			"_upgrade",
			"_validate":
			segment.Operation = strings.TrimPrefix(s, "_")
			if idx > 0 {
				segment.Collection = segments[0]
			}
			return
		case "_doc":
			switch method {
			case "DELETE":
				segment.Operation = "delete"
			case "HEAD":
				segment.Operation = "exists"
			case "GET":
				segment.Operation = "get"
			case "PUT":
				segment.Operation = "update"
			case "POST":
				segment.Operation = "create"
			}
			if idx > 0 {
				segment.Collection = segments[0]
			}
			return
		case "_search":
			// clear_scroll.json      DELETE   /_search/scroll
			// clear_scroll.json      DELETE   /_search/scroll/{scroll_id}
			// scroll.json            GET      /_search/scroll
			// scroll.json            GET      /_search/scroll/{scroll_id}
			// scroll.json            POST     /_search/scroll
			// scroll.json            POST     /_search/scroll/{scroll_id}
			// search.json            GET      /_search
			// search.json            GET      /{index}/_search
			// search.json            GET      /{index}/{type}/_search
			// search.json            POST     /_search
			// search.json            POST     /{index}/_search
			// search.json            POST     /{index}/{type}/_search
			// search_template.json   GET      /_search/template
			// search_template.json   GET      /{index}/_search/template
			// search_template.json   GET      /{index}/{type}/_search/template
			// search_template.json   POST     /_search/template
			// search_template.json   POST     /{index}/_search/template
			// search_template.json   POST     /{index}/{type}/_search/template
			if method == "DELETE" {
				segment.Operation = "clear_scroll"
				return
			}
			if idx == len(segments)-1 {
				segment.Operation = "search"
				if idx > 0 {
					segment.Collection = segments[0]
				}
				return
			}
			next := segments[idx+1]
			if next == "scroll" {
				segment.Operation = "scroll"
				return
			}
			if next == "template" {
				segment.Operation = "search_template"
				if idx > 0 {
					segment.Collection = segments[0]
				}
				return
			}
			return
		}
	}
	return
}

type roundtripper struct{ original http.RoundTripper }

func (t roundtripper) RoundTrip(r *http.Request) (*http.Response, error) {
	segment := parseRequest(r)
	defer segment.End()

	return t.original.RoundTrip(r)
}

// NewRoundTripper creates a new http.RoundTripper to instrument elasticsearch
// calls.  If an http.RoundTripper parameter is not provided, then the returned
// http.RoundTripper will delegate to http.DefaultTransport.
func NewRoundTripper(original http.RoundTripper) http.RoundTripper {
	if nil == original {
		original = http.DefaultTransport
	}
	return roundtripper{original: original}
}