-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(otlp-exporter-base): implement partial success handling
- Loading branch information
1 parent
7e98761
commit d2e5cd7
Showing
8 changed files
with
360 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
48 changes: 48 additions & 0 deletions
48
experimental/packages/otlp-exporter-base/src/logging-response-handler.ts
This file contains 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,48 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { diag } from '@opentelemetry/api'; | ||
import { IOtlpResponseHandler } from './response-handler'; | ||
|
||
function isPartialSuccessResponse( | ||
response: unknown | ||
): response is { partialSuccess: never } { | ||
return Object.prototype.hasOwnProperty.call(response, 'partialSuccess'); | ||
} | ||
|
||
/** | ||
* Default response handler that logs a partial success to the console. | ||
*/ | ||
export function createLoggingPartialSuccessResponseHandler< | ||
T, | ||
>(): IOtlpResponseHandler<T> { | ||
return { | ||
handleResponse(response: T) { | ||
// Partial success MUST never be an empty object according the specification | ||
// see https://opentelemetry.io/docs/specs/otlp/#partial-success | ||
if ( | ||
response == null || | ||
!isPartialSuccessResponse(response) || | ||
response.partialSuccess == null | ||
) { | ||
return; | ||
} | ||
diag.warn( | ||
'Received Partial Success response:', | ||
JSON.stringify(response.partialSuccess) | ||
); | ||
}, | ||
}; | ||
} |
This file contains 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
27 changes: 27 additions & 0 deletions
27
experimental/packages/otlp-exporter-base/src/response-handler.ts
This file contains 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,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Generic export response handler. Can be implemented to handle export responses like partial success. | ||
*/ | ||
export interface IOtlpResponseHandler<Response> { | ||
/** | ||
* Handles an OTLP export response. | ||
* Implementations MUST NOT throw. | ||
* @param response | ||
*/ | ||
handleResponse(response: Response): void; | ||
} |
90 changes: 90 additions & 0 deletions
90
experimental/packages/otlp-exporter-base/test/common/logging-response-handler.test.ts
This file contains 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,90 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { createLoggingPartialSuccessResponseHandler } from '../../src/logging-response-handler'; | ||
import * as sinon from 'sinon'; | ||
import { IExportTraceServiceResponse } from '@opentelemetry/otlp-transformer'; | ||
import { registerMockDiagLogger } from './test-utils'; | ||
|
||
describe('loggingResponseHandler', function () { | ||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should diag warn if a partial success is passed', function () { | ||
// arrange | ||
const { warn } = registerMockDiagLogger(); | ||
const handler = | ||
createLoggingPartialSuccessResponseHandler<IExportTraceServiceResponse>(); | ||
const partialSuccessResponse: IExportTraceServiceResponse = { | ||
partialSuccess: { | ||
errorMessage: 'error', | ||
rejectedSpans: 10, | ||
}, | ||
}; | ||
|
||
// act | ||
handler.handleResponse(partialSuccessResponse); | ||
|
||
//assert | ||
sinon.assert.calledOnceWithExactly( | ||
warn, | ||
'Received Partial Success response:', | ||
JSON.stringify(partialSuccessResponse.partialSuccess) | ||
); | ||
}); | ||
|
||
it('should not warn when a response is undefined', function () { | ||
// arrange | ||
const { warn } = registerMockDiagLogger(); | ||
const handler = createLoggingPartialSuccessResponseHandler(); | ||
|
||
// act | ||
handler.handleResponse(undefined); | ||
|
||
//assert | ||
sinon.assert.notCalled(warn); | ||
}); | ||
|
||
it('should not warn when a response is defined but partialSuccess is undefined', function () { | ||
// arrange | ||
const { warn } = registerMockDiagLogger(); | ||
const handler = createLoggingPartialSuccessResponseHandler(); | ||
|
||
// act | ||
handler.handleResponse({ partialSuccess: undefined }); | ||
|
||
//assert | ||
sinon.assert.notCalled(warn); | ||
}); | ||
|
||
it('should warn when a response is defined but partialSuccess is empty object', function () { | ||
// note: it is not permitted for the server to return such a response, but it may happen anyway | ||
// arrange | ||
const { warn } = registerMockDiagLogger(); | ||
const handler = createLoggingPartialSuccessResponseHandler(); | ||
const response = { partialSuccess: {} }; | ||
|
||
// act | ||
handler.handleResponse(response); | ||
|
||
//assert | ||
sinon.assert.calledOnceWithExactly( | ||
warn, | ||
'Received Partial Success response:', | ||
JSON.stringify(response.partialSuccess) | ||
); | ||
}); | ||
}); |
This file contains 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
Oops, something went wrong.