|
| 1 | +/* |
| 2 | + * Copyright 2013-2020 The Kamon Project <https://kamon.io> |
| 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 | + |
| 17 | +package kamon.instrumentation.opensearch |
| 18 | + |
| 19 | +import kamon.Kamon |
| 20 | +import kamon.trace.Span |
| 21 | +import kanela.agent.api.instrumentation.InstrumentationBuilder |
| 22 | +import org.apache.http.HttpEntity |
| 23 | +import org.opensearch.client.{Response, ResponseListener} |
| 24 | + |
| 25 | +class OSInstrumentation extends InstrumentationBuilder { |
| 26 | + onType("org.opensearch.client.RestClient") |
| 27 | + .advise(method("performRequestAsync").and(takesArguments(2)), classOf[AsyncOpensearchRestClientInstrumentation]) |
| 28 | + .advise(method("performRequest").and(takesArguments(1)), classOf[SyncOpensearchRestClientInstrumentation]) |
| 29 | + |
| 30 | + onType("org.opensearch.client.RestHighLevelClient") |
| 31 | + .advise(method("internalPerformRequest").and(takesArguments(5)), classOf[HighLevelOpensearchClientInstrumentation]) |
| 32 | + .advise(method("internalPerformRequestAsync").and(takesArguments(6)), classOf[HighLevelOpensearchClientInstrumentation]) |
| 33 | +} |
| 34 | + |
| 35 | +class InstrumentedListener(inner: ResponseListener, span: Span) extends ResponseListener { |
| 36 | + override def onSuccess(response: Response): Unit = { |
| 37 | + span.finish() |
| 38 | + inner.onSuccess(response) |
| 39 | + } |
| 40 | + |
| 41 | + override def onFailure(exception: Exception): Unit = { |
| 42 | + span.fail(exception) |
| 43 | + inner.onFailure(exception) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +object RequestSizeHistogram { |
| 48 | + private val histogram = Kamon.histogram("opensearch.request.size").withoutTags() |
| 49 | + |
| 50 | + def record(entity: HttpEntity): Unit = { |
| 51 | + Option(entity) |
| 52 | + .map(_.getContentLength) |
| 53 | + .filter(_ >= 0) |
| 54 | + .foreach(histogram.record) |
| 55 | + } |
| 56 | +} |
0 commit comments