Skip to content

Commit e0d2cb2

Browse files
authored
feat(opentelemetry-sampler-aws-xray): Update Rules Poller Implementation (#2750)
1 parent 27f5c66 commit e0d2cb2

20 files changed

+1369
-226
lines changed

incubator/opentelemetry-sampler-aws-xray/package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@
4141
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json"
4242
},
4343
"peerDependencies": {
44-
"@opentelemetry/api": "^1.3.0"
44+
"@opentelemetry/api": "^1.9.0"
4545
},
4646
"dependencies": {
47-
"@opentelemetry/core": "^1.8.0",
48-
"@opentelemetry/sdk-trace-base": "^1.8.0",
49-
"axios": "^1.3.5"
47+
"@opentelemetry/core": "^2.0.0",
48+
"@opentelemetry/resources": "^2.0.0",
49+
"@opentelemetry/sdk-trace-base": "^2.0.0",
50+
"@opentelemetry/semantic-conventions": "^1.27.0"
5051
},
5152
"devDependencies": {
52-
"@opentelemetry/api": "^1.3.0",
53-
"@opentelemetry/contrib-test-utils": "^0.35.0",
53+
"@opentelemetry/api": "^1.9.0",
54+
"@opentelemetry/sdk-trace-node": "^2.0.0",
5455
"@types/mocha": "10.0.10",
5556
"@types/node": "18.18.14",
5657
"@types/sinon": "17.0.4",
@@ -64,6 +65,6 @@
6465
"typescript": "5.0.4"
6566
},
6667
"engines": {
67-
"node": ">=14"
68+
"node": "^18.19.0 || >=20.6.0"
6869
}
6970
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
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+
* https://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+
// Includes work from:
18+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
19+
// SPDX-License-Identifier: Apache-2.0
20+
21+
import { DiagLogFunction, DiagLogger, context } from '@opentelemetry/api';
22+
import { suppressTracing } from '@opentelemetry/core';
23+
import * as http from 'http';
24+
import {
25+
GetSamplingRulesResponse,
26+
GetSamplingTargetsBody,
27+
GetSamplingTargetsResponse,
28+
} from './types';
29+
30+
export class AWSXRaySamplingClient {
31+
private getSamplingRulesEndpoint: string;
32+
private samplingTargetsEndpoint: string;
33+
private samplerDiag: DiagLogger;
34+
35+
constructor(endpoint: string, samplerDiag: DiagLogger) {
36+
this.getSamplingRulesEndpoint = endpoint + '/GetSamplingRules';
37+
this.samplingTargetsEndpoint = endpoint + '/SamplingTargets';
38+
this.samplerDiag = samplerDiag;
39+
}
40+
41+
public fetchSamplingTargets(
42+
requestBody: GetSamplingTargetsBody,
43+
callback: (responseObject: GetSamplingTargetsResponse) => void
44+
) {
45+
this.makeSamplingRequest<GetSamplingTargetsResponse>(
46+
this.samplingTargetsEndpoint,
47+
callback,
48+
this.samplerDiag.debug,
49+
JSON.stringify(requestBody)
50+
);
51+
}
52+
53+
public fetchSamplingRules(
54+
callback: (responseObject: GetSamplingRulesResponse) => void
55+
) {
56+
this.makeSamplingRequest<GetSamplingRulesResponse>(
57+
this.getSamplingRulesEndpoint,
58+
callback,
59+
this.samplerDiag.error
60+
);
61+
}
62+
63+
private makeSamplingRequest<T>(
64+
url: string,
65+
callback: (responseObject: T) => void,
66+
logger: DiagLogFunction,
67+
requestBodyJsonString?: string
68+
): void {
69+
const options: http.RequestOptions = {
70+
method: 'POST',
71+
headers: {},
72+
};
73+
74+
if (requestBodyJsonString) {
75+
options.headers = {
76+
'Content-Type': 'application/json',
77+
'Content-Length': Buffer.byteLength(requestBodyJsonString),
78+
};
79+
}
80+
81+
// Ensure AWS X-Ray Sampler does not generate traces itself
82+
context.with(suppressTracing(context.active()), () => {
83+
const req: http.ClientRequest = http
84+
.request(url, options, response => {
85+
response.setEncoding('utf-8');
86+
let responseData = '';
87+
response.on('data', dataChunk => (responseData += dataChunk));
88+
response.on('end', () => {
89+
if (response.statusCode === 200 && responseData.length > 0) {
90+
let responseObject: T | undefined = undefined;
91+
try {
92+
responseObject = JSON.parse(responseData) as T;
93+
} catch (e: unknown) {
94+
logger(`Error occurred when parsing responseData from ${url}`);
95+
}
96+
97+
if (responseObject) {
98+
callback(responseObject);
99+
}
100+
} else {
101+
this.samplerDiag.debug(
102+
`${url} Response Code is: ${response.statusCode}`
103+
);
104+
this.samplerDiag.debug(`${url} responseData is: ${responseData}`);
105+
}
106+
});
107+
})
108+
.on('error', (error: unknown) => {
109+
logger(`Error occurred when making an HTTP POST to ${url}: ${error}`);
110+
});
111+
if (requestBodyJsonString) {
112+
req.end(requestBodyJsonString);
113+
} else {
114+
req.end();
115+
}
116+
});
117+
}
118+
}

incubator/opentelemetry-sampler-aws-xray/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
* limitations under the License.
1515
*/
1616
export * from './remote-sampler';
17-
export { AWSXRaySamplerConfig } from './types';
17+
export { AWSXRayRemoteSamplerConfig } from './types';

0 commit comments

Comments
 (0)