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

Recommendation Service Update - Python #92

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ services:
# RecommendationService
recommendationservice:
build:
context: ./src/recommendationservice
context: ./
dockerfile: ./src/recommendationservice/Dockerfile
ports:
- "${RECOMMENDATION_SERVICE_PORT}"
- "${RECOMMENDATION_SERVICE_PORT}:${RECOMMENDATION_SERVICE_PORT}"
mic-max marked this conversation as resolved.
Show resolved Hide resolved
depends_on:
- productcatalogservice
environment:
- PORT=${RECOMMENDATION_SERVICE_PORT}
- RECOMMENDATION_SERVICE_PORT
- PRODUCT_CATALOG_SERVICE_ADDR
- OTEL_PYTHON_LOG_CORRELATION=true
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
Expand Down
30 changes: 9 additions & 21 deletions src/recommendationservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,17 @@

FROM python:3.10-slim

# get packages
COPY requirements.txt .
RUN pip install -r requirements.txt
WORKDIR /usr/src/app/

# show python logs as they occur
ENV PYTHONUNBUFFERED=0
COPY ./src/recommendationservice/ ./
COPY ./pb/ ./proto/

RUN apt-get -qq update \
&& apt-get install -y --no-install-recommends \
wget

# download the grpc health probe
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 /recommendationservice
RUN pip install --upgrade pip
RUN pip install -r ./requirements.txt

# add files into working directory
COPY . .

# set listen port
ENV PORT "8080"
EXPOSE 8080
RUN python -m pip install grpcio-tools
RUN python -m grpc_tools.protoc -I=./proto/ --python_out=./ --grpc_python_out=./ ./proto/demo.proto

ENTRYPOINT ["opentelemetry-instrument", "python", "recommendation_server.py"]
EXPOSE ${RECOMMENDATION_SERVICE_PORT}
ENTRYPOINT [ "opentelemetry-instrument", "python", "recommendation_server.py" ]
22 changes: 21 additions & 1 deletion src/recommendationservice/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Read Me

This is a placeholder
To run the `./client.py` you must compile `/pb/demo.proto` into python code.

```shell
python -m grpc_tools.protoc -I./pb/ --python_out=./src/recommendationservice/ --grpc_python_out=./src/recommendationservice/ ./pb/demo.proto
python ./src/recommendationservice/client.py
```

You should see output similar to the following

```json
{
"asctime": "2022-06-02 13:42:44,793",
"levelname": "INFO",
"name": "recommendationservice-server",
"filename": "client.py",
"lineno": 35,
"otelTraceID": "00000000000000000000000000000000",
"otelSpanID": "0000000000000000",
"message": "product_ids: \"6E92ZMYYFZ\"\nproduct_ids: \"OLJCESPC7Z\"\nproduct_ids: \"LS4PSXUNUM\"\nproduct_ids: \"2ZYFJ3GM2N\"\nproduct_ids: \"1YMWWN1N4O\"\n"
}
```
12 changes: 3 additions & 9 deletions src/recommendationservice/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import grpc
import demo_pb2
import demo_pb2_grpc
Expand All @@ -24,18 +23,13 @@
logger = getJSONLogger('recommendationservice-server')

if __name__ == "__main__":
# get port
if len(sys.argv) > 1:
port = sys.argv[1]
else:
port = "8080"

# set up server stub
channel = grpc.insecure_channel('localhost:'+port)

channel = grpc.insecure_channel('localhost:8080')
mic-max marked this conversation as resolved.
Show resolved Hide resolved
stub = demo_pb2_grpc.RecommendationServiceStub(channel)

# form request
request = demo_pb2.ListRecommendationsRequest(user_id="test", product_ids=["test"])

# make call to server
response = stub.ListRecommendations(request)
logger.info(response)
Loading