Skip to content

Commit

Permalink
Dockerify examples (#592)
Browse files Browse the repository at this point in the history
* Add dotnet Dockerfile

* Add go Dockerfile

* Add javascript Dockerfile

* Add ruby Dockerfile

* Put it all together with docker compose

* Remove dead link
  • Loading branch information
alanwest authored May 13, 2024
1 parent 7ff4ae4 commit 01bfad5
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ OpenTelemetry is a big ecosystem and everything doesn't fit into the goals of th

We encourage your contributions to improve `newrelic-opentelemetry-examples`! Keep in mind that when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.

Generally, we want to focus on the [getting started guides](#getting-started-guides). We're open to additional examples being added which are aligned with the [demo app specification](./getting-started-guides/demo-app-specification.md) and which have a volunteer [codeowner](#codeowners).
Generally, we want to focus on the [getting started guides](#getting-started-guides). We're open to additional examples being added which have a volunteer [codeowner](#codeowners).

We're more selective about additions to [other examples](#other-examples). We use the following criteria to evaluate additions:

Expand Down
34 changes: 34 additions & 0 deletions getting-started-guides/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/build
**/dist
LICENSE
README.md
7 changes: 7 additions & 0 deletions getting-started-guides/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net
OTEL_EXPORTER_OTLP_HEADERS="api-key=YOUR_API_KEY_HERE"

OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
OTEL_EXPORTER_OTLP_COMPRESSION=gzip
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA
28 changes: 21 additions & 7 deletions getting-started-guides/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
# Getting Started Guides
# Getting Started with New Relic using OpenTelemetry

This repo holds the source code for the demo applications used in the [Getting Started Guides](https://docs.newrelic.com/docs/more-integrations/open-source-telemetry-integrations/opentelemetry/get-started/opentelemetry-get-started-intro/).
The examples within this directory demonstrate how to send data to New Relic
using OpenTelemetry.

Each language directory illustrates how to add OpenTelemetry instrumentation to a simple web application, and configure OpenTelemetry for an optimal New Relic experience. This includes exporting over OTLP, limiting attributes according to New Relic ingest limits, and more.
Each language directory illustrates how to add OpenTelemetry instrumentation to
a simple web application and configure OpenTelemetry for an optimal New Relic
experience. This includes exporting over OTLP, limiting attributes according to
New Relic ingest limits, and more.

In order to provide some degree of uniformity, each sample app is written to comply with the [demo app specification](./demo-app-specification.md). Each language contains the following sub-directories:
To get started quickly, you can use Docker Compose to spin up all the example
applications.

* **Uninstrumented:** Contains the uninstrumented version of the app.
* **Instrumented:** Contains the instrumented versions of the app. The uninstrumented app is enhanced with OpenTelemetry based instrumentation to generate metrics, logs, and traces, and SDK configuration aligned with New Relic [best practices](https://docs.newrelic.com/docs/more-integrations/open-source-telemetry-integrations/opentelemetry/best-practices/opentelemetry-best-practices-overview/) to export telemetry to New Relic.
1. First, open the [.env](./.env) file and configure your New Relic API key.
If necessary, also change the
[New Relic OTLP endpoint](https://docs.newrelic.com/docs/more-integrations/open-source-telemetry-integrations/opentelemetry/best-practices/opentelemetry-otlp/#configure-endpoint-port-protocol)
to match your region and needs.

To run, please see the [Getting Started Guides](https://docs.newrelic.com/docs/more-integrations/open-source-telemetry-integrations/opentelemetry/get-started/opentelemetry-get-started-intro/) documentation and follow the instructions in the README of the root of each respective sample app.
2. Then, run Docker Compose

```shell
docker compose up --build
```

3. Lastly, go view your data in New Relic. Running using Docker Compose also
starts a simple load generator, so data should be flowing.
57 changes: 57 additions & 0 deletions getting-started-guides/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
services:
dotnet:
build: dotnet
environment:
- OTEL_SERVICE_NAME=getting-started-dotnet
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_HEADERS
- OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
- OTEL_RESOURCE_ATTRIBUTES=service.instance.id=123
ports:
- 8080
go:
build: go
environment:
- OTEL_SERVICE_NAME=getting-started-go
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_HEADERS
- OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
- OTEL_RESOURCE_ATTRIBUTES=service.instance.id=123
ports:
- 8080
javascript:
build: javascript
environment:
- OTEL_SERVICE_NAME=getting-started-javascript
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_HEADERS
- OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
- OTEL_RESOURCE_ATTRIBUTES=service.instance.id=123
ports:
- 8080
ruby:
build: ruby
environment:
- OTEL_SERVICE_NAME=getting-started-ruby
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_HEADERS
- OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
- OTEL_RESOURCE_ATTRIBUTES=service.instance.id=123
ports:
- 8080
envoy:
image: envoyproxy/envoy:v1.29.4
ports:
- "8080:8080"
volumes:
- ./supporting-files/envoy.yaml:/etc/envoy/envoy.yaml
loadgenerator:
image: python:3.12.3
command: ["python", "/loadgenerator.py"]
volumes:
- ./supporting-files/loadgenerator.py:/loadgenerator.py
depends_on:
- dotnet
- go
- javascript
- ruby
63 changes: 0 additions & 63 deletions getting-started-guides/demo-app-specification.md

This file was deleted.

28 changes: 28 additions & 0 deletions getting-started-guides/dotnet/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build

COPY . /source

WORKDIR /source

ARG TARGETARCH

# Build the application.
# Leverage a cache mount to /root/.nuget/packages so that subsequent builds don't have to re-download packages.
# If TARGETARCH is "amd64", replace it with "x64" - "x64" is .NET's canonical name for this and "amd64" doesn't
# work in .NET 6.0.
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final

WORKDIR /app

COPY --from=build /app .

USER $APP_UID

EXPOSE 8080

ENTRYPOINT ["dotnet", "dotnet.dll"]
2 changes: 1 addition & 1 deletion getting-started-guides/dotnet/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Kestrel": {
"Endpoints": {
"Localhost": {
"Url": "http://localhost:8080"
"Url": "http://*:8080"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion getting-started-guides/dotnet/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Kestrel": {
"Endpoints": {
"Localhost": {
"Url": "http://localhost:8080"
"Url": "http://*:8080"
}
}
},
Expand Down
42 changes: 42 additions & 0 deletions getting-started-guides/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION=1.20
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
WORKDIR /src

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x

ARG TARGETARCH

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server .

FROM alpine:latest AS final

RUN --mount=type=cache,target=/var/cache/apk \
apk --update add \
ca-certificates \
tzdata \
&& \
update-ca-certificates

ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

COPY --from=build /bin/server /bin/

EXPOSE 8080

ENTRYPOINT [ "/bin/server" ]
22 changes: 22 additions & 0 deletions getting-started-guides/javascript/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# syntax=docker/dockerfile:1

ARG NODE_VERSION=20.10.0

FROM node:${NODE_VERSION}-alpine

ENV NODE_ENV production

WORKDIR /usr/src/app

RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev

USER node

COPY . .

EXPOSE 8080

CMD npm start
13 changes: 13 additions & 0 deletions getting-started-guides/ruby/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# syntax=docker/dockerfile:1

FROM ruby:3.2.2 as base

WORKDIR /usr/src/app

COPY . .

RUN bundle install

EXPOSE 8080

CMD bundle exec rackup
2 changes: 1 addition & 1 deletion getting-started-guides/ruby/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require './opentelemetry'
require './fibonacci'
require './app'

Rack::Handler.default.run(App, :Port => 8080)
Rack::Handler.default.run(App, :Host => "0.0.0.0", :Port => 8080)
Loading

0 comments on commit 01bfad5

Please sign in to comment.