Skip to content

feat(opentelemetry-sampler-aws-xray): Update Rules Poller Implementation #2750

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 10 commits into from
May 12, 2025
15 changes: 8 additions & 7 deletions incubator/opentelemetry-sampler-aws-xray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
"@opentelemetry/api": "^1.9.0"
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"axios": "^1.3.5"
"@opentelemetry/core": "^2.0.0",
"@opentelemetry/resources": "^2.0.0",
"@opentelemetry/sdk-trace-base": "^2.0.0",
"@opentelemetry/semantic-conventions": "^1.27.0"
},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/contrib-test-utils": "^0.35.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/sdk-trace-node": "^2.0.0",
"@types/mocha": "10.0.10",
"@types/node": "18.18.14",
"@types/sinon": "17.0.4",
Expand All @@ -64,6 +65,6 @@
"typescript": "5.0.4"
},
"engines": {
"node": ">=14"
"node": "^18.19.0 || >=20.6.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.
*/

// Includes work from:
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { DiagLogFunction, DiagLogger, context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import * as http from 'http';
import {
GetSamplingRulesResponse,
GetSamplingTargetsBody,
GetSamplingTargetsResponse,
} from './types';

export class AWSXRaySamplingClient {
private getSamplingRulesEndpoint: string;
private samplingTargetsEndpoint: string;
private samplerDiag: DiagLogger;

constructor(endpoint: string, samplerDiag: DiagLogger) {
this.getSamplingRulesEndpoint = endpoint + '/GetSamplingRules';
this.samplingTargetsEndpoint = endpoint + '/SamplingTargets';
this.samplerDiag = samplerDiag;
}

public fetchSamplingTargets(
requestBody: GetSamplingTargetsBody,
callback: (responseObject: GetSamplingTargetsResponse) => void
) {
this.makeSamplingRequest<GetSamplingTargetsResponse>(
this.samplingTargetsEndpoint,
callback,
this.samplerDiag.debug,
JSON.stringify(requestBody)
);
}

public fetchSamplingRules(
callback: (responseObject: GetSamplingRulesResponse) => void
) {
this.makeSamplingRequest<GetSamplingRulesResponse>(
this.getSamplingRulesEndpoint,
callback,
this.samplerDiag.error
);
}

private makeSamplingRequest<T>(
url: string,
callback: (responseObject: T) => void,
logger: DiagLogFunction,
requestBodyJsonString?: string
): void {
const options: http.RequestOptions = {
method: 'POST',
headers: {},
};

if (requestBodyJsonString) {
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBodyJsonString),
};
}

// Ensure AWS X-Ray Sampler does not generate traces itself
context.with(suppressTracing(context.active()), () => {
const req: http.ClientRequest = http
.request(url, options, response => {
response.setEncoding('utf-8');
let responseData = '';
response.on('data', dataChunk => (responseData += dataChunk));
response.on('end', () => {
if (response.statusCode === 200 && responseData.length > 0) {
let responseObject: T | undefined = undefined;
try {
responseObject = JSON.parse(responseData) as T;
} catch (e: unknown) {
logger(`Error occurred when parsing responseData from ${url}`);
}

if (responseObject) {
callback(responseObject);
}
} else {
this.samplerDiag.debug(
`${url} Response Code is: ${response.statusCode}`
);
this.samplerDiag.debug(`${url} responseData is: ${responseData}`);
}
});
})
.on('error', (error: unknown) => {
logger(`Error occurred when making an HTTP POST to ${url}: ${error}`);
});
if (requestBodyJsonString) {
req.end(requestBodyJsonString);
} else {
req.end();
}
});
}
}
2 changes: 1 addition & 1 deletion incubator/opentelemetry-sampler-aws-xray/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/
export * from './remote-sampler';
export { AWSXRaySamplerConfig } from './types';
export { AWSXRayRemoteSamplerConfig } from './types';
Loading
Loading