Skip to content

Commit 42b7c99

Browse files
committed
sampler update
1 parent b520d04 commit 42b7c99

20 files changed

+1362
-225
lines changed

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

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

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)