-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Agent builder exporter #265290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Agent builder exporter #265290
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2d834b8
Agent builder exporter
machadoum c680215
Implement send_to_self config
machadoum 84d88e6
Refactor should track span
machadoum 984057b
Update agent builder exporter to support send_to_self and force_sampl…
machadoum c4b5386
Massive refactor to move config and code to agent builder ownership
machadoum cd8c3d4
Improve agent builder plugin tracing code
machadoum 4aed9c6
Does small refactor and add test
machadoum 336546b
Merge remote-tracking branch 'kibana/main' into ea-ws-traces-exporter
machadoum a7b89d5
Fix oops
machadoum 29bde84
Changes from node scripts/lint_ts_projects --fix
kibanamachine 65c2e85
Changes from security: 3rd-party dependencies
kibanamachine f1c8ca5
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine f9a1d9a
Merge remote-tracking branch 'kibana/main' into ea-ws-traces-exporter
machadoum 734bd85
Code review improvements
machadoum f35789b
Undo changes made by mistake
machadoum 02e7931
Code review improvement and pushing untracked file
machadoum 0133bb8
Update exporter config to allow multiple exporters
machadoum 7817a27
Fix CI
machadoum 3981cbe
Merge remote-tracking branch 'kibana/main' into ea-ws-traces-exporter
machadoum a354514
Merge branch 'main' into ea-ws-traces-exporter
machadoum 834e2f3
Merge branch 'main' into ea-ws-traces-exporter
machadoum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
src/platform/packages/shared/kbn-tracing/src/elasticsearch_otlp_exporter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { core } from '@elastic/opentelemetry-node/sdk'; | ||
| import type { tracing } from '@elastic/opentelemetry-node/sdk'; | ||
| import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer'; | ||
| import { ElasticsearchOtlpExporter } from './elasticsearch_otlp_exporter'; | ||
| import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
|
|
||
| jest.mock('@opentelemetry/otlp-transformer', () => ({ | ||
| ProtobufTraceSerializer: { | ||
| serializeRequest: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const mockedSerializeRequest = ProtobufTraceSerializer.serializeRequest as jest.MockedFunction< | ||
| typeof ProtobufTraceSerializer.serializeRequest | ||
| >; | ||
|
|
||
| describe('ElasticsearchOtlpExporter', () => { | ||
| const spans = [] as tracing.ReadableSpan[]; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('calls transport.request with correct path, method, and headers on success', (done) => { | ||
| const serialized = new Uint8Array([1, 2, 3]); | ||
| mockedSerializeRequest.mockReturnValue(serialized); | ||
|
|
||
| const request = jest.fn().mockResolvedValue({}); | ||
| const client = { | ||
| transport: { request }, | ||
| } as unknown as ElasticsearchClient; | ||
|
|
||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
|
|
||
| exporter.export(spans, (result) => { | ||
| try { | ||
| expect(result.code).toBe(core.ExportResultCode.SUCCESS); | ||
| expect(request).toHaveBeenCalledWith( | ||
| { | ||
| method: 'POST', | ||
| path: '/_otlp/v1/traces', | ||
| body: Buffer.from(serialized), | ||
| }, | ||
| { | ||
| headers: { 'Content-Type': 'application/x-protobuf' }, | ||
| maxRetries: 3, | ||
| } | ||
| ); | ||
| done(); | ||
| } catch (err) { | ||
| done(err); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('returns FAILED when serialization fails', (done) => { | ||
| mockedSerializeRequest.mockImplementation(() => undefined); | ||
|
|
||
| const request = jest.fn(); | ||
| const client = { | ||
| transport: { request }, | ||
| } as unknown as ElasticsearchClient; | ||
|
|
||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
|
|
||
| exporter.export(spans, (result) => { | ||
| try { | ||
| expect(result.code).toBe(core.ExportResultCode.FAILED); | ||
| expect(result.error).toEqual(new Error('Serialization failed')); | ||
| expect(request).not.toHaveBeenCalled(); | ||
| done(); | ||
| } catch (err) { | ||
| done(err); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('returns FAILED when transport.request rejects', (done) => { | ||
| const serialized = new Uint8Array([9]); | ||
| mockedSerializeRequest.mockReturnValue(serialized); | ||
|
|
||
| const transportError = new Error('connection reset'); | ||
| const request = jest.fn().mockRejectedValue(transportError); | ||
| const client = { | ||
| transport: { request }, | ||
| } as unknown as ElasticsearchClient; | ||
|
|
||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
|
|
||
| exporter.export(spans, (result) => { | ||
| try { | ||
| expect(result.code).toBe(core.ExportResultCode.FAILED); | ||
| expect(result.error).toBe(transportError); | ||
| done(); | ||
| } catch (err) { | ||
| done(err); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('shutdown waits for in-flight exports to complete before returning', async () => { | ||
| const serialized = new Uint8Array([1, 2, 3]); | ||
| mockedSerializeRequest.mockReturnValue(serialized); | ||
|
|
||
| const request = jest.fn().mockResolvedValue({}); | ||
| const client = { | ||
| transport: { request }, | ||
| } as unknown as ElasticsearchClient; | ||
|
|
||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
| const results: core.ExportResult[] = []; | ||
| exporter.export(spans, (result) => results.push(result)); | ||
|
|
||
| await exporter.shutdown(); | ||
|
|
||
| expect(results).toHaveLength(1); | ||
| expect(results[0].code).toBe(core.ExportResultCode.SUCCESS); | ||
| }); | ||
|
|
||
| it('export after shutdown returns FAILED immediately', (done) => { | ||
| const client = { | ||
| transport: { request: jest.fn() }, | ||
| } as unknown as ElasticsearchClient; | ||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
|
|
||
| exporter.shutdown().then(() => { | ||
| exporter.export(spans, (result) => { | ||
| try { | ||
| expect(result.code).toBe(core.ExportResultCode.FAILED); | ||
| expect(result.error?.message).toBe('Exporter has been shut down'); | ||
| expect(client.transport.request).not.toHaveBeenCalled(); | ||
| done(); | ||
| } catch (err) { | ||
| done(err); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('forceFlush waits for in-flight exports', async () => { | ||
| const serialized = new Uint8Array([4, 5]); | ||
| mockedSerializeRequest.mockReturnValue(serialized); | ||
|
|
||
| let resolveRequest: () => void; | ||
| const request = jest.fn().mockImplementation( | ||
| () => | ||
| new Promise<void>((resolve) => { | ||
| resolveRequest = resolve; | ||
| }) | ||
| ); | ||
| const client = { | ||
| transport: { request }, | ||
| } as unknown as ElasticsearchClient; | ||
|
|
||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
| const results: core.ExportResult[] = []; | ||
| exporter.export(spans, (result) => results.push(result)); | ||
|
|
||
| const flushPromise = exporter.forceFlush(); | ||
|
|
||
| expect(results).toHaveLength(0); | ||
| resolveRequest!(); | ||
| await flushPromise; | ||
|
|
||
| expect(results).toHaveLength(1); | ||
| expect(results[0].code).toBe(core.ExportResultCode.SUCCESS); | ||
| }); | ||
|
|
||
| it('forceFlush resolves immediately when no exports are pending', async () => { | ||
| const client = { | ||
| transport: { request: jest.fn() }, | ||
| } as unknown as ElasticsearchClient; | ||
| const exporter = new ElasticsearchOtlpExporter(client); | ||
|
|
||
| await expect(exporter.forceFlush()).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
src/platform/packages/shared/kbn-tracing/src/elasticsearch_otlp_exporter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { tracing } from '@elastic/opentelemetry-node/sdk'; | ||
| import { core } from '@elastic/opentelemetry-node/sdk'; | ||
| import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer'; | ||
| import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
|
|
||
| const ES_OTLP_TRACES_PATH = '/_otlp/v1/traces'; | ||
| const CONTENT_TYPE_PROTOBUF = 'application/x-protobuf'; | ||
|
|
||
| /** | ||
| * A {@link tracing.SpanExporter} that ships OTLP-protobuf encoded spans | ||
| * to Elasticsearch's native `/_otlp/v1/traces` endpoint via the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I knew about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was recently merged together with logs elastic/elasticsearch#147811 |
||
| * ES client transport. This reuses the same connection, auth, and TLS | ||
| * settings that Kibana already has for talking to Elasticsearch. | ||
| */ | ||
| export class ElasticsearchOtlpExporter implements tracing.SpanExporter { | ||
| private readonly sendingPromises = new Set<Promise<void>>(); | ||
| private isShutdown = false; | ||
|
|
||
| constructor(private readonly client: ElasticsearchClient) {} | ||
|
|
||
| export(spans: tracing.ReadableSpan[], resultCallback: (result: core.ExportResult) => void): void { | ||
| if (this.isShutdown) { | ||
| resultCallback({ | ||
| code: core.ExportResultCode.FAILED, | ||
| error: new Error('Exporter has been shut down'), | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const serialized = ProtobufTraceSerializer.serializeRequest(spans); | ||
| if (!serialized) { | ||
| resultCallback({ | ||
| code: core.ExportResultCode.FAILED, | ||
| error: new Error('Serialization failed'), | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const exportPromise = this.client.transport | ||
| .request( | ||
| { | ||
| method: 'POST', | ||
| path: ES_OTLP_TRACES_PATH, | ||
| body: Buffer.from(serialized), | ||
| }, | ||
| { | ||
| headers: { 'Content-Type': CONTENT_TYPE_PROTOBUF }, | ||
| maxRetries: 3, | ||
| } | ||
| ) | ||
| .then(() => resultCallback({ code: core.ExportResultCode.SUCCESS })) | ||
| .catch((error) => resultCallback({ code: core.ExportResultCode.FAILED, error })) | ||
| .finally(() => this.sendingPromises.delete(exportPromise)); | ||
|
|
||
| this.sendingPromises.add(exportPromise); | ||
| } | ||
|
|
||
| async forceFlush(): Promise<void> { | ||
| await Promise.all(this.sendingPromises); | ||
| } | ||
|
|
||
| async shutdown(): Promise<void> { | ||
| this.isShutdown = true; | ||
| await this.forceFlush(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.