Skip to content

Commit 560055b

Browse files
committed
added buttercup payment to payment
1 parent 9069dca commit 560055b

File tree

12 files changed

+1055
-1227
lines changed

12 files changed

+1055
-1227
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ src/payment/charge.js.original
6363
src/payment/logger.js.not sure
6464
src/payment/logger.js.original
6565
src/payment.zip
66+
src/payment-old
67+
src/payment/*.bak

kubernetes/opentelemetry-demo.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,14 @@ spec:
18771877
fieldPath: status.hostIP
18781878
- name: OTEL_COLLECTOR_NAME
18791879
value: $(NODE_IP)
1880+
- name: OTEL_TRACES_EXPORTER
1881+
value: otlp
1882+
- name: OTEL_METRICS_EXPORTER
1883+
value: otlp
1884+
- name: OTEL_LOGS_EXPORTER
1885+
value: otlp
1886+
- name: SPLUNK_OTEL_LOG_CORRELATION_ENABLED
1887+
value: "true"
18801888
- name: OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
18811889
value: cumulative
18821890
- name: PAYMENT_PORT

src/payment/.dockerignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
node_modules/
22
.dockerignore
3-
Dockerfile
4-
package.json
5-
package-lock.json
3+
Dockerfiledok
64
README.md

src/payment/Dockerfile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,40 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44

5-
FROM docker.io/library/node:22-slim AS builder
5+
FROM docker.io/library/node:22 AS builder
66

77
WORKDIR /usr/src/app/
88

99
COPY ./src/payment/package.json package.json
1010
COPY ./src/payment/package-lock.json package-lock.json
1111

12-
RUN npm ci --omit=dev
12+
RUN npm ci --omit=dev && chmod -R go+r node_modules/@splunk/otel
1313

1414
# -----------------------------------------------------------------------------
1515

16-
FROM gcr.io/distroless/nodejs22-debian12:nonroot
16+
FROM docker.io/library/node:22
1717

1818
WORKDIR /usr/src/app/
1919

20+
# Create non-root user for security
21+
RUN groupadd -r appuser && useradd -r -g appuser appuser
22+
2023
COPY --from=builder /usr/src/app/node_modules/ node_modules/
2124

2225
COPY ./pb/demo.proto demo.proto
2326

2427
COPY ./src/payment/charge.js charge.js
2528
COPY ./src/payment/index.js index.js
2629
COPY ./src/payment/logger.js logger.js
27-
#COPY ./src/payment/opentelemetry.js opentelemetry.js
30+
COPY ./src/payment/opentelemetry.js opentelemetry.js
31+
32+
# Set ownership for non-root user
33+
RUN chown -R appuser:appuser /usr/src/app
2834

2935
EXPOSE ${PAYMENT_PORT}
3036

31-
CMD ["-r", "@splunk/otel/instrument", "index.js"]
37+
# Switch to non-root user
38+
USER appuser
39+
40+
#CMD ["node", "-r @splunk/otel/instrument index.js"]
41+
CMD ["--require=./opentelemetry.js", "index.js"]

src/payment/build-payment.sh

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/bash -e
22

33
# Usage:
4-
# ./build.sh <version> [-cc]
4+
# ./build-payment.sh <version> [-cc]
55

66
if [ -z "$1" ]; then
77
echo "❌ Error: No version provided."
8-
echo "Usage: $0 <version> [-cc] "
8+
echo "Usage: $0 <version> [-cc]"
9+
exit 1
910
fi
1011

1112
VERSION="$1"
@@ -14,28 +15,47 @@ CACHE_OPTION=""
1415
# Check if the second arg is -cc
1516
if [[ "$2" == "-cc" ]]; then
1617
CACHE_OPTION="--no-cache"
17-
# Shift all remaining args so $2 becomes RUM_TOKEN
18-
shift
1918
fi
20-
URL_PREFIX="$6"
2119

22-
23-
# Move two directories up from src/recomndation
20+
# Move two directories up from src/payment
2421
cd "$(dirname "$0")/../.."
2522

26-
# Build command
23+
echo "Building payment service version: $VERSION"
24+
echo "Build options: ${CACHE_OPTION:-default caching enabled}"
25+
26+
# Build without push first
2727
DOCKER_CMD=(
2828
docker buildx build
2929
--platform=linux/amd64,linux/arm64
3030
$CACHE_OPTION
3131
--build-arg VERSION="$VERSION"
3232
-t ghcr.io/splunk/opentelemetry-demo/otel-payment:"$VERSION"
33-
--push
33+
--load
3434
-f src/payment/Dockerfile
35+
.
3536
)
3637

37-
# Add build context
38-
DOCKER_CMD+=( . )
38+
echo "Executing build command..."
39+
if ! "${DOCKER_CMD[@]}"; then
40+
echo "❌ Error: Docker build failed"
41+
exit 1
42+
fi
43+
44+
echo "✅ Build successful"
45+
46+
# Verify image exists locally
47+
if ! docker image inspect ghcr.io/splunk/opentelemetry-demo/otel-payment:"$VERSION" > /dev/null 2>&1; then
48+
echo "❌ Error: Built image not found locally"
49+
exit 1
50+
fi
51+
52+
echo "✅ Image verified locally"
53+
54+
# Push the image
55+
echo "Pushing image to registry..."
56+
if ! docker push ghcr.io/splunk/opentelemetry-demo/otel-payment:"$VERSION"; then
57+
echo "❌ Error: Push failed. Check your registry authentication."
58+
exit 1
59+
fi
3960

40-
# Execute the build
41-
"${DOCKER_CMD[@]}"
61+
echo "✅ Successfully pushed ghcr.io/splunk/opentelemetry-demo/otel-payment:$VERSION"

src/payment/charge.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
3-
///Update Tghis !!!
43
const { context, propagation, trace, metrics, SpanKind, SpanStatusCode } = require('@opentelemetry/api');
54
const cardValidator = require('simple-card-validator');
65
const { v4: uuidv4 } = require('uuid');
@@ -295,7 +294,7 @@ module.exports.charge = async request => {
295294
'http.status_code': finalCode,
296295
});
297296

298-
// set explicit error status on the root span so it doesnt show as unknown
297+
// set explicit error status on the root span so it doesn't show as "unknown"
299298
span.setStatus({ code: SpanStatusCode.ERROR, message: String(finalCode) });
300299

301300
// keep baggage handling as you have it

src/payment/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,24 @@ server.addService(health.service, new health.Implementation({
4444

4545
server.addService(otelDemoPackage.oteldemo.PaymentService.service, { charge: chargeServiceHandler })
4646

47-
server.bindAsync(`0.0.0.0:${process.env['PAYMENT_PORT']}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
47+
48+
let ip = "0.0.0.0";
49+
50+
const ipv6_enabled = process.env.IPV6_ENABLED;
51+
52+
if (ipv6_enabled == "true") {
53+
ip = "[::]";
54+
logger.info(`Overwriting Localhost IP: ${ip}`)
55+
}
56+
57+
const address = ip + `:${process.env['PAYMENT_PORT']}`;
58+
59+
server.bindAsync(address, grpc.ServerCredentials.createInsecure(), (err, port) => {
4860
if (err) {
4961
return logger.error({ err })
5062
}
5163

52-
logger.info(`payment gRPC server started on port ${port}`)
64+
logger.info(`payment gRPC server started on ${address}`)
5365
})
5466

5567
process.once('SIGINT', closeGracefully)

src/payment/logger.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
const pino = require('pino');
5-
const { context, trace } = require('@opentelemetry/api');
4+
const pino = require('pino')
65

7-
const logger = pino({
8-
// Match desired base fields
9-
base: { name: 'paymentservice' }, // shows up as "name"
10-
pid: process.pid,
11-
hostname: require('os').hostname(),
12-
messageKey: 'message', // your examples use "message"
13-
timestamp: pino.stdTimeFunctions.unixTime, // Unix seconds in "time"
14-
// Map level -> { severity: "<level>" }
15-
formatters: {
16-
level: (label) => ({ severity: label }),
17-
},
18-
// Inject OTEL fields when a span is active
19-
mixin() {
20-
const span = trace.getSpan(context.active());
21-
const svc = process.env.OTEL_SERVICE_NAME || 'paymentservice';
22-
const base = { 'service.name': svc };
23-
if (span && typeof span.spanContext === 'function') {
24-
const sc = span.spanContext();
25-
if (sc) {
26-
// slice low 64-bits of 128-bit trace id to match your example format
27-
base.trace_id = (sc.traceId || '').slice(-16);
28-
base.span_id = sc.spanId;
6+
const transport = pino.transport({
7+
target: 'pino-opentelemetry-transport',
8+
options: {
9+
logRecordProcessorOptions: [
10+
{
11+
recordProcessorType: 'batch',
12+
exporterOptions: {
13+
protocol: 'grpc',
14+
}
15+
},
16+
{
17+
recordProcessorType: 'simple',
18+
exporterOptions: { protocol: 'console' }
2919
}
20+
],
21+
loggerName: 'payment-logger',
22+
serviceVersion: '1.0.0'
23+
}
24+
})
25+
26+
const logger = pino(transport, {
27+
mixin() {
28+
return {
29+
'service.name': process.env['OTEL_SERVICE_NAME'],
3030
}
31-
return base;
31+
},
32+
formatters: {
33+
level: (label) => {
34+
return { 'level': label };
35+
},
3236
},
3337
});
3438

src/payment/opentelemetry.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// const { start } = require('@splunk/otel');
5+
6+
// start({
7+
// serviceName: 'my-node-service',
8+
// endpoint: 'http://localhost:4317'
9+
// });
10+
// const opentelemetry = require("@opentelemetry/sdk-node")
11+
// const {getNodeAutoInstrumentations} = require("@opentelemetry/auto-instrumentations-node")
12+
// const {OTLPTraceExporter} = require('@opentelemetry/exporter-trace-otlp-grpc')
13+
// const {OTLPMetricExporter} = require('@opentelemetry/exporter-metrics-otlp-grpc')
14+
// const {PeriodicExportingMetricReader} = require('@opentelemetry/sdk-metrics')
15+
// const {alibabaCloudEcsDetector} = require('@opentelemetry/resource-detector-alibaba-cloud')
16+
// const {awsEc2Detector, awsEksDetector} = require('@opentelemetry/resource-detector-aws')
17+
// const {containerDetector} = require('@opentelemetry/resource-detector-container')
18+
// const {gcpDetector} = require('@opentelemetry/resource-detector-gcp')
19+
// const {envDetector, hostDetector, osDetector, processDetector} = require('@opentelemetry/resources')
20+
// const {RuntimeNodeInstrumentation} = require('@opentelemetry/instrumentation-runtime-node')
21+
22+
// const sdk = new opentelemetry.NodeSDK({
23+
// traceExporter: new OTLPTraceExporter(),
24+
// instrumentations: [
25+
// getNodeAutoInstrumentations({
26+
// // only instrument fs if it is part of another trace
27+
// '@opentelemetry/instrumentation-fs': {
28+
// requireParentSpan: true,
29+
// },
30+
// }),
31+
// new RuntimeNodeInstrumentation({
32+
// monitoringPrecision: 5000,
33+
// })
34+
// ],
35+
// metricReader: new PeriodicExportingMetricReader({
36+
// exporter: new OTLPMetricExporter()
37+
// }),
38+
// resourceDetectors: [
39+
// containerDetector,
40+
// envDetector,
41+
// hostDetector,
42+
// osDetector,
43+
// processDetector,
44+
// alibabaCloudEcsDetector,
45+
// awsEksDetector,
46+
// awsEc2Detector,
47+
// gcpDetector
48+
// ],
49+
// })
50+
51+
// sdk.start();
52+
const { start } = require('@splunk/otel');
53+
// Print relevant OTEL/Splunk environment variables
54+
console.log('=== OpenTelemetry Environment Variables ===');
55+
[
56+
'OTEL_SERVICE_NAME',
57+
'OTEL_EXPORTER_OTLP_ENDPOINT',
58+
'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT',
59+
//'OTEL_EXPORTER_OTLP_METRICS_ENDPOINT',
60+
'OTEL_RESOURCE_ATTRIBUTES',
61+
'SPLUNK_PROFILER_ENABLED',
62+
'SPLUNK_PROFILER_MEMORY_ENABLED',
63+
'SPLUNK_PROFILER_CALL_STACK_INTERVAL',
64+
'OTEL_PROFILER_LOGS_ENDPOINT',
65+
'OTEL_LOG_LEVEL'
66+
].forEach((key) => {
67+
if (process.env[key]) {
68+
console.log(`${key}: ${process.env[key]}`);
69+
} else {
70+
console.log(`${key}: (not set)`);
71+
}
72+
});
73+
74+
console.log('===========================================');
75+
76+
start();

src/payment/opentelemetry.js.not

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)