-
由 zhou liang 创作于b313386b
server.js 4.83 KiB
/*
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Tracing code
const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const options = {
serviceName: process.env.SERVICE_NAME ||'currencyservice',
tags: [{key: 'ip', value: process.env.POD_IP},
{key: 'name', value: process.env.POD_NAME},
{key: 'node_name', value: process.env.NODE_NAME}], // optional
host: process.env.JAEGER_HOST,
port: process.env.JAEGER_PORT, // optional
maxPacketSize: 65000 // optional
}
const exporter = new JaegerExporter(options);
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
console.log("jaeger tracing initialized");
const path = require('path');
const grpc = require('@grpc/grpc-js');
const pino = require('pino');
const protoLoader = require('@grpc/proto-loader');
const MAIN_PROTO_PATH = path.join(__dirname, './proto/demo.proto');
const HEALTH_PROTO_PATH = path.join(__dirname, './proto/grpc/health/v1/health.proto');
const PORT = process.env.PORT;
const shopProto = _loadProto(MAIN_PROTO_PATH).hipstershop;
const healthProto = _loadProto(HEALTH_PROTO_PATH).grpc.health.v1;
const logger = pino({
name: 'currencyservice-server',
messageKey: 'message',
changeLevelName: 'severity',
useLevelLabels: true
});
/**
* Helper function that loads a protobuf file.
*/
function _loadProto (path) {
const packageDefinition = protoLoader.loadSync(
path,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true