Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency Service using C++ #189

Merged
merged 20 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ release.
([#82](https://github.com/open-telemetry/opentelemetry-demo-webstore/pull/82))
* Added feature flag service implementation
([#141](https://github.com/open-telemetry/opentelemetry-demo-webstore/pull/141))
* Re-implemented currency service using C++
([#189](https://github.com/open-telemetry/opentelemetry-demo-webstore/pull/189))
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ services:
image: ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice
build:
context: ./src/currencyservice
args:
- GRPC_VERSION=1.46.0
- OPENTELEMETRY_VERSION=1.4.0
ports:
- "${CURRENCY_SERVICE_PORT}"
environment:
Expand Down
73 changes: 73 additions & 0 deletions src/currencyservice/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.1)
project(currency-service)

find_package(Protobuf)
find_package(gRPC)

set(PROTO_PATH "${CMAKE_CURRENT_SOURCE_DIR}/proto")
set(GENERATED_PROTOBUF_PATH "${CMAKE_BINARY_DIR}/generated/proto")
set(GENERATED_HEALTH_PROTOBUF_PATH "${GENERATED_PROTOBUF_PATH}/grpc/health/v1")

file(MAKE_DIRECTORY "${GENERATED_PROTOBUF_PATH}")

set(DEMO_PROTO "${PROTO_PATH}/demo.proto")
set(DEMO_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/demo.pb.cc")
set(DEMO_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/demo.pb.h")
set(DEMO_GRPC_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/demo.grpc.pb.cc")
set(DEMO_GRPC_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/demo.grpc.pb.h")
set(HEALTH_PROTO "${PROTO_PATH}/grpc/health/v1/health.proto")
set(HEALTH_PB_CPP_FILE "${GENERATED_HEALTH_PROTOBUF_PATH}/health.pb.cc")
set(HEALTH_PB_H_FILE "${GENERATED_HEALTH_PROTOBUF_PATH}/health.pb.h")
set(HEALTH_GRPC_PB_CPP_FILE "${GENERATED_HEALTH_PROTOBUF_PATH}/health.grpc.pb.cc")
set(HEALTH_GRPC_PB_H_FILE "${GENERATED_HEALTH_PROTOBUF_PATH}/health.grpc.pb.h")

foreach(IMPORT_DIR ${PROTOBUF_IMPORT_DIRS})
list(APPEND PROTOBUF_INCLUDE_FLAGS "-I${IMPORT_DIR}")
endforeach()

find_program(gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)

add_custom_command(
OUTPUT ${DEMO_PB_H_FILE}
${DEMO_PB_CPP_FILE}
${DEMO_GRPC_PB_CPP_FILE}
${DEMO_GRPC_PB_H_FILE}
${HEALTH_PB_H_FILE}
${HEALTH_PB_CPP_FILE}
${HEALTH_GRPC_PB_CPP_FILE}
${HEALTH_GRPC_PB_H_FILE}

COMMAND
${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--experimental_allow_proto3_optional"
"--proto_path=${PROTO_PATH}" ${PROTOBUF_INCLUDE_FLAGS}
"--cpp_out=${GENERATED_PROTOBUF_PATH}"
"--grpc_out=generate_mock_code=true:${GENERATED_PROTOBUF_PATH}"
--plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" ${DEMO_PROTO} ${HEALTH_PROTO})

message(STATUS "gRPC_CPP_PLUGIN_EXECUTABLE=${gRPC_CPP_PLUGIN_EXECUTABLE}")

add_library(demo-proto ${DEMO_PB_H_FILE}
${DEMO_PB_CPP_FILE}
${DEMO_GRPC_PB_CPP_FILE}
${DEMO_GRPC_PB_H_FILE}
${HEALTH_PB_H_FILE}
${HEALTH_PB_CPP_FILE}
${HEALTH_GRPC_PB_CPP_FILE}
${HEALTH_GRPC_PB_H_FILE})

target_link_libraries(demo-proto gRPC::grpc++ protobuf::libprotobuf)
include_directories("${GENERATED_PROTOBUF_PATH}")

add_executable(currencyservice src/server.cpp)
add_dependencies(currencyservice demo-proto)
target_link_libraries(
currencyservice demo-proto protobuf::libprotobuf
opentelemetry_resources opentelemetry_trace opentelemetry_common opentelemetry_exporter_otlp_grpc opentelemetry_proto opentelemetry_otlp_recordable gRPC::grpc++)

add_executable(currencyclient src/client.cpp)
add_dependencies(currencyclient demo-proto)
target_link_libraries(
currencyclient demo-proto protobuf::libprotobuf gRPC::grpc++)

install(TARGETS currencyservice DESTINATION bin)
install(TARGETS currencyclient DESTINATION bin)
103 changes: 57 additions & 46 deletions src/currencyservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
# Copyright 2020 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.

FROM node:16-alpine as base

FROM base as builder

# Some packages (e.g. @google-cloud/profiler) require additional
# deps for post-install scripts
RUN apk add --update --no-cache \
python3 \
make \
g++

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=production

FROM base

RUN GRPC_HEALTH_PROBE_VERSION=v0.4.7 && \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe

WORKDIR /usr/src/app

COPY --from=builder /usr/src/app/node_modules ./node_modules

COPY . .

EXPOSE 7000

ENTRYPOINT [ "node", "--require", "./tracing.js", "server.js" ]
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get -y update
RUN apt-get -y upgrade && apt-get -y dist-upgrade
RUN apt-get install -qq -y --ignore-missing \
build-essential \
git \
make \
pkg-config \
protobuf-compiler \
libprotobuf-dev \
cmake

# The following arguments would be passed from docker-compose.yml
ARG GRPC_VERSION
ARG OPENTELEMETRY_VERSION

# Install GRPC
RUN git clone --shallow-submodules --depth 1 --recurse-submodules -b v${GRPC_VERSION} \
https://github.com/grpc/grpc \
&& cd grpc \
&& mkdir -p cmake/build \
&& cd cmake/build \
&& cmake \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_BUILD_GRPC_NODE_PLUGIN=OFF \
-DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=OFF \
-DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF \
-DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF \
-DgRPC_BUILD_GRPC_PYTHON_PLUGIN=OFF \
-DgRPC_BUILD_GRPC_RUBY_PLUGIN=OFF \
../.. \
&& make -j2 \
&& make install \
&& cd ../../.. \
&& rm -rf grpc

# install opentelemetry
RUN git clone https://github.com/open-telemetry/opentelemetry-cpp \
&& cd opentelemetry-cpp/ \
&& git checkout tags/v${OPENTELEMETRY_VERSION} -b v${OPENTELEMETRY_VERSION} \
&& git submodule update --init --recursive \
&& mkdir build \
&& cd build \
&& cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF -DWITH_OTLP=ON \
&& make -j install && cd ../.. && rm -rf opentelemetry-cpp

COPY . /currencyservice

RUN cd /currencyservice \
&& mkdir -p build && cd build \
&& cmake .. && make -j install

ENTRYPOINT currencyservice ${PORT} ${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT} ${OTEL_RESOURCE_ATTRIBUTES}
33 changes: 31 additions & 2 deletions src/currencyservice/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# Read Me
# Currency Service

This is a placeholder
The Currency Service does the conversion from one currency to another.
It is a C++ based service.

## Building docker image

To build the currency service, run the following from root directory
of opentelemetry-demo-webstore

```sh
docker-compose build currencyservice
```

## Run the service

Execute the below command to run the service.

```sh
docker-compose up currencyservice
```

## Run the client

currencyclient is a sample client which sends some request to currency
service. To run the client, execute the below command.

```sh
docker exec -it <container_name> currencyclient 7000
```

'7000' is port where currencyservice listens to.
65 changes: 0 additions & 65 deletions src/currencyservice/client.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/currencyservice/package.json

This file was deleted.

Loading