|
| 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 | +} |
0 commit comments