Skip to content

Commit

Permalink
[productcatalogservice]add basic metric support (#674)
Browse files Browse the repository at this point in the history
* add basic metric support

* fix lint

* fix lint

* fix lint

* Update productcatalogservice.md

* Update Dockerfile

Co-authored-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
Co-authored-by: Juliano Costa <juliano.costa@dynatrace.com>
  • Loading branch information
4 people authored Jan 11, 2023
1 parent e427c9a commit 6b2cd9e
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 49 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,7 @@ significant modifications will be credited to OpenTelemetry Authors.
([#664](https://github.com/open-telemetry/opentelemetry-demo/pull/664))
* Update loadgenerator python base image and dependencies
([#669](https://github.com/open-telemetry/opentelemetry-demo/pull/669))
* Add basic metric support to productcatalog service
([#674](https://github.com/open-telemetry/opentelemetry-demo/pull/674))
* Add resource detectors to accounting service
([#676](https://github.com/open-telemetry/opentelemetry-demo/pull/676))
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ services:
- PRODUCT_CATALOG_SERVICE_PORT
- FEATURE_FLAG_GRPC_SERVICE_ADDR
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_RESOURCE_ATTRIBUTES
- OTEL_SERVICE_NAME=productcatalogservice
depends_on:
Expand Down
6 changes: 3 additions & 3 deletions docs/metric_service_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Emoji Legend
- Not Present (Yet): :construction:

| Service | Language | Auto Instrumentation | Manual Metric Creation | Collector Agent Metric Transformation | Push Metrics | SLO Metrics | Multiple Manual Metric Instruments |
|-----------------|-----------------|----------------------|------------------------|---------------------------------------|----------------|----------------|------------------------------------|
| --------------- | --------------- | -------------------- | ---------------------- | ------------------------------------- | -------------- | -------------- | ---------------------------------- |
| Ad | Java | :100: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Cart | .NET | :100: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Checkout | Go | :100: | :construction: | :construction: | :construction: | :construction: | :construction: |
Expand All @@ -16,6 +16,6 @@ Emoji Legend
| Feature Flag | Erlang / Elixir | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Frontend | JavaScript | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Payment | JavaScript | :construction: | :100: | :construction: | :construction: | :construction: | :construction: |
| Product Catalog | Go | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Recommendation | Python | :100: | :100: | :construction: | :construction: | :construction: | :construction: |
| Product Catalog | Go | :100: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Recommendation | Python | :100: | :100: | :construction: | :construction: | :construction: | :construction: |
| Shipping | Rust | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
46 changes: 44 additions & 2 deletions docs/services/productcatalogservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ as part of the gRPC server creation.
```

This service will issue outgoing gRPC calls, which are all instrumented by
wrapping the gRPC client with instrumentation
wrapping the gRPC client with instrumentation.

```go
func createClient(ctx context.Context, svcAddr string) (*grpc.ClientConn, error) {
Expand Down Expand Up @@ -108,7 +108,49 @@ or when a product is successfully found.

## Metrics

TBD
### Initializing Metrics

The OpenTelemetry SDK is initialized from `main` using the `initMeterProvider`
function.

```go
func initMeterProvider() *sdkmetric.MeterProvider {
ctx := context.Background()

exporter, err := otlpmetricgrpc.New(ctx)
if err != nil {
log.Fatalf("new otlp metric grpc exporter failed: %v", err)
}

mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)))
global.SetMeterProvider(mp)
return mp
}
```

You should call `initMeterProvider.Shutdown()` when your service is shutdown to
ensure all records are exported. This service makes that call as part of a
deferred function in main.

```go
mp := initMeterProvider()
defer func() {
if err := mp.Shutdown(context.Background()); err != nil {
log.Fatalf("Error shutting down meter provider: %v", err)
}
}()
```

### Adding golang runtime auto-instrumentation

Golang runtime is instrumented in the main function

```go
err := runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second))
if err != nil {
log.Fatal(err)
}
```

## Logs

Expand Down
6 changes: 3 additions & 3 deletions src/productcatalogservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.17.7-alpine AS builder
FROM golang:1.19.4-alpine AS builder

WORKDIR /usr/src/app/

Expand All @@ -23,8 +23,8 @@ COPY ./src/productcatalogservice/ ./
COPY ./pb/ ./proto/

RUN go mod download
RUN go get google.golang.org/protobuf/cmd/protoc-gen-go
RUN go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

# Build executable
RUN protoc -I ./proto/ ./proto/demo.proto --go_out=./ --go-grpc_out=./
Expand Down
34 changes: 19 additions & 15 deletions src/productcatalogservice/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@ go 1.17

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7
github.com/sirupsen/logrus v1.8.1
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
google.golang.org/grpc v1.44.0
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
google.golang.org/grpc v1.51.0
)

require (
go.opentelemetry.io/otel/sdk v1.4.1
google.golang.org/protobuf v1.27.1
go.opentelemetry.io/contrib/instrumentation/runtime v0.37.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.34.0
go.opentelemetry.io/otel/metric v0.34.0
go.opentelemetry.io/otel/sdk v1.11.2
go.opentelemetry.io/otel/sdk/metric v0.34.0
go.opentelemetry.io/otel/trace v1.11.2
google.golang.org/protobuf v1.28.1
)

require (
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.4.1 // indirect
go.opentelemetry.io/otel/trace v1.4.1 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.34.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.2 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
)

require (
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.29.0
go.opentelemetry.io/otel v1.4.1
go.opentelemetry.io/otel v1.11.2
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.4.1
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20220126215142-9970aeb2e350 // indirect
)
Loading

0 comments on commit 6b2cd9e

Please sign in to comment.