Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit d7fb797

Browse files
author
Yaniv Davidi
authored
Elasticsearch (#81)
* chore: update readme
1 parent ff988d2 commit d7fb797

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

packages/instrumentation-elasticsearch/README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ registerInstrumentations({
2121
traceProvider,
2222
instrumentations: [
2323
new ElasticsearchInstrumentation({
24-
// see under for available configuration
24+
// Config example (all optional)
25+
suppressInternalInstrumentation: false,
26+
moduleVersionAttributeName: 'elasticsearchClient.version',
27+
responseHook: (span, result) => {
28+
span.setAttribute('db.response', JSON.stringify(result));
29+
},
30+
dbStatementSerializer: (operation, params, options) => {
31+
return JSON.stringify(params);
32+
}
2533
})
2634
]
2735
});
@@ -31,15 +39,26 @@ registerInstrumentations({
3139

3240
Elasticsearch instrumentation has few options available to choose from. You can set the following (all optional):
3341

34-
| Options | Type | Description |
35-
| -------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------- |
36-
| `suppressInternalInstrumentation` | `boolean` | Elasticsearch operation use http/https under the hood. Setting this to true will hide the underlying request spans (if instrumented). |
37-
| `responseHook` | `ElasticsearchResponseCustomAttributesFunction` | Hook called before response is returned, which allows to add custom attributes to span. |
38-
| `dbStatementSerializer` | `DbStatementSerializer` | Elasticsearch instrumentation will serialize `db.statement` using the specified function.
39-
| `moduleVersionAttributeName` | `string` | If passed, a span attribute will be added to all spans with key of the provided `moduleVersionAttributeName` and value of the patched module version |
42+
| Options | Type | Default | Description |
43+
| --- | --- | --- | --- |
44+
| `suppressInternalInstrumentation` | `boolean` | `false` | Elasticsearch operation use http/https under the hood. Setting this to true will hide the underlying request spans (if instrumented). |
45+
| `responseHook` | `ResponseHook` (function) | `undefined` | Hook called before response is returned, which allows to add custom attributes to span.<br>Function receive params: `span`<br>`result` (object) |
46+
| `dbStatementSerializer` | `DbStatementSerializer` (function) | `JSON.stringify({params, options})` | Elasticsearch instrumentation will serialize `db.statement` using this function response.<br>Function receive params: `operation` (string)<br>`params` (object)<br>`options` (object)<br>Function response must be a `string`
47+
| `moduleVersionAttributeName` | `string` | `undefined` | If passed, a span attribute will be added to all spans with key of the provided `moduleVersionAttributeName` and value of the `@elastic/elasticsearch` version |
4048

4149
Please make sure `dbStatementSerializer` is error proof, as errors are not handled while executing this function.
4250

51+
### `db.operation` attribute
52+
`db.operation` contain the API function called.
53+
For the full list see [API reference](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html).
54+
55+
Few examples:
56+
* `client.bulk`
57+
* `client.search`
58+
* `client.index`
59+
* `cat.shards`
60+
* `cluster.health`
61+
4362
---
4463

4564
This extension (and many others) was developed by [Aspecto](https://www.aspecto.io/) with ❤️

packages/instrumentation-elasticsearch/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InstrumentationConfig } from '@opentelemetry/instrumentation';
33

44
export type DbStatementSerializer = (operation?: string, params?: object, options?: object) => string;
55

6-
export type ElasticsearchResponseCustomAttributesFunction = (span: Span, response: any) => void;
6+
export type ResponseHook = (span: Span, response: any) => void;
77

88
export interface ElasticsearchInstrumentationConfig extends InstrumentationConfig {
99
/**
@@ -19,7 +19,7 @@ export interface ElasticsearchInstrumentationConfig extends InstrumentationConfi
1919
dbStatementSerializer?: DbStatementSerializer;
2020

2121
/** hook for adding custom attributes using the response payload */
22-
responseHook?: ElasticsearchResponseCustomAttributesFunction;
22+
responseHook?: ResponseHook;
2323

2424
/**
2525
* If passed, a span attribute will be added to all spans with key of the provided "moduleVersionAttributeName"

packages/instrumentation-elasticsearch/src/utils.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Tracer, SpanAttributes, SpanStatusCode, diag, Span, SpanKind } from '@opentelemetry/api';
2-
import { DbStatementSerializer, ElasticsearchResponseCustomAttributesFunction } from './types';
2+
import { DbStatementSerializer, ResponseHook } from './types';
33
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
44
import { DatabaseAttribute, GeneralAttribute } from '@opentelemetry/semantic-conventions';
55
import { ApiResponse } from '@elastic/elasticsearch/lib/Transport';
@@ -66,11 +66,7 @@ export function getNetAttributes(url: string): SpanAttributes {
6666
};
6767
}
6868

69-
export function onResponse(
70-
span: Span,
71-
result: ApiResponse,
72-
responseHook?: ElasticsearchResponseCustomAttributesFunction
73-
) {
69+
export function onResponse(span: Span, result: ApiResponse, responseHook?: ResponseHook) {
7470
span.setAttributes({
7571
...getNetAttributes(result.meta.connection.url.toString()),
7672
});

packages/instrumentation-elasticsearch/test/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('elasticsearch utils', () => {
175175
const tracerMock = {
176176
startSpan: (name, options?, context?): any => {},
177177
};
178-
it('should start span with client kink', () => {
178+
it('should start span with client kind', () => {
179179
const startSpanStub = stub(tracerMock, 'startSpan');
180180

181181
Utils.startSpan({

0 commit comments

Comments
 (0)