|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -// TODO validate Policy properties |
| 3 | +var logPrefix = 'TraceContextRequest: '; |
| 4 | +// <!-- OPTIONAL |
| 5 | +// The odds of a Trace being sampled, 0-100. 0 = tracing off, 100 = trace everything. |
| 6 | +// Default: 100 --> |
| 7 | +// <Property name="SampleOddsPercentage">100</Property> |
| 8 | +var SampleOddsPercentage = contextGetVariable(properties.SampleOddsPercentage, 100); |
| 9 | +// <!-- REQUIRED |
| 10 | +// The name of the Request/Response pair. Must be unique within a Proxy. |
| 11 | +// Default: no default value --> |
| 12 | +// <Property name="RequestResponsePairName">Target-Trace-Context-Flow</Property> |
4 | 13 |
|
5 | | -var startTime = contextGetVariable(properties.TraceStartTimestamp, Date.now()); |
6 | 14 | var RequestResponsePairName = properties.RequestResponsePairName; |
7 | | -var logPrefix = RequestResponsePairName + '.TraceContextRequest: '; |
| 15 | +if(isNil(RequestResponsePairName)){ |
| 16 | + print(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName); |
| 17 | + logMsg(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName); |
| 18 | + throw (logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName); |
| 19 | +} |
| 20 | +logPrefix = RequestResponsePairName + '.TraceContextRequest: '; |
| 21 | + |
| 22 | +// <!-- OPTIONAL |
| 23 | +// How to determine the start time for this Trace. Any Flow Variable (https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference) ending in '.timestamp' is valid. |
| 24 | +// Default:`Date.now()'--> |
| 25 | +// <Property name="TraceStartTimestamp"></Property> |
| 26 | +var startTime = contextGetVariable(properties.TraceStartTimestamp, Date.now()); |
| 27 | + |
| 28 | +// <!-- OPTIONAL |
| 29 | +// A comma separated list of key=value pairs propagated in the tracestate header. |
| 30 | +// https://www.w3.org/TR/trace-context/#tracestate-header |
| 31 | +// Default: no default value --> |
| 32 | +// <Property name="AdditionalTracestateValues"></Property> |
| 33 | +var additionalStates = properties.AdditionalTracestateValues; |
| 34 | + |
8 | 35 | logMsg(logPrefix + 'enter'); |
9 | 36 | var ctxPrefix = 'traceContext.' + RequestResponsePairName; |
10 | 37 |
|
@@ -63,15 +90,14 @@ function generateSampled() { |
63 | 90 | // Give us a number 0 - 99 inclusive |
64 | 91 | var n = Math.floor(Math.random() * 100); |
65 | 92 | n = n + 1; |
66 | | - if (n <= properties.SampleOddsPercentage) { |
| 93 | + if (n <= SampleOddsPercentage) { |
67 | 94 | return '01'; |
68 | 95 | } |
69 | 96 | return '00'; |
70 | 97 | } |
71 | 98 |
|
72 | 99 | var traceparent = context.getVariable('request.header.traceparent'); |
73 | 100 | var tracestate = context.getVariable('request.header.tracestate.values.string'); |
74 | | -var additionalStates = properties.AdditionalTracestateValues; |
75 | 101 | logMsg(logPrefix + 'traceparent: ' + traceparent + ' type: ' + typeof (traceparent)); |
76 | 102 | logMsg(logPrefix + 'tracestate: ' + tracestate + ' type: ' + typeof (tracestate)); |
77 | 103 | logMsg(logPrefix + 'additionalStates: ' + additionalStates + ' type: ' + typeof (additionalStates)); |
|
0 commit comments