Skip to content

Commit

Permalink
adding tilt/basic metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
austinlparker committed Feb 1, 2021
1 parent 264e95e commit c218d82
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ pkg/
.vs/
.idea
.skaffold-*.yaml
.kubernetes-manifests-*/
.kubernetes-manifests-*/
.ls_key
tilt_modules/
5 changes: 5 additions & 0 deletions README.tilt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# tilt.dev usage

- Install tilt (https://docs.tilt.dev/)
- `tilt up` to create/start services and watches.
- `tilt down` to destroy services.
34 changes: 34 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load('ext://secret', 'secret_create_generic')

k8s_yaml([
'kubernetes-manifests/otel-collector.yaml',
'kubernetes-manifests/adservice.yaml',
'kubernetes-manifests/cartservice.yaml',
'kubernetes-manifests/checkoutservice.yaml',
'kubernetes-manifests/currencyservice.yaml',
'kubernetes-manifests/emailservice.yaml',
'kubernetes-manifests/frontend.yaml',
'kubernetes-manifests/k8s-service-account.yaml',
'kubernetes-manifests/lightstep-configmap.yaml',
'kubernetes-manifests/loadgenerator.yaml',
'kubernetes-manifests/otel-collector-config.yaml',
'kubernetes-manifests/paymentservice.yaml',
'kubernetes-manifests/productcatalogservice.yaml',
'kubernetes-manifests/recommendationservice.yaml',
'kubernetes-manifests/redis.yaml',
'kubernetes-manifests/shippingservice.yaml',
])

secret_create_generic('lightstep-credentials', from_file='accessToken=./.ls_key')

docker_build('adservice', 'src/adservice')
docker_build('emailservice', 'src/emailservice')
docker_build('productcatalogservice', 'src/productcatalogservice')
docker_build('recommendationservice', 'src/recommendationservice')
docker_build('shippingservice', 'src/shippingservice')
docker_build('checkoutservice', 'src/checkoutservice')
docker_build('paymentservice', 'src/paymentservice')
docker_build('currencyservice', 'src/currencyservice')
docker_build('cartservice', 'src/cartservice')
docker_build('frontend', 'src/frontend')
docker_build('loadgenerator', 'src/loadgenerator')
15 changes: 15 additions & 0 deletions kubernetes-manifests/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ spec:
port: 80
targetPort: 8080
---
---
apiVersion: v1
kind: Service
metadata:
name: frontend-np
spec:
type: NodePort
selector:
app: frontend
ports:
- name: http-np
port: 8080
targetPort: 8080
nodePort: 30000
---
apiVersion: v1
kind: Service
metadata:
Expand Down
15 changes: 14 additions & 1 deletion src/checkoutservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"github.com/lightstep/otel-launcher-go/launcher"
"github.com/sirupsen/logrus"
grpcotel "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
Expand All @@ -38,7 +41,11 @@ const (
usdCurrency = "USD"
)

var log *logrus.Logger
var (
log *logrus.Logger
meter = otel.Meter("checkoutservice/metrics")
orderCount = metric.Must(meter).NewInt64Counter("checkoutservice.order")
)

func init() {
log = logrus.New()
Expand Down Expand Up @@ -130,15 +137,18 @@ func (cs *checkoutService) Watch(req *healthpb.HealthCheckRequest, ws healthpb.H
}

func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderRequest) (*pb.PlaceOrderResponse, error) {
// count orders
log.Infof("[PlaceOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)

orderID, err := uuid.NewUUID()
if err != nil {
orderCount.Add(ctx, 1, label.String("status", "internalError"))
return nil, status.Errorf(codes.Internal, "failed to generate order uuid")
}

prep, err := cs.prepareOrderItemsAndShippingQuoteFromCart(ctx, req.UserId, req.UserCurrency, req.Address)
if err != nil {
orderCount.Add(ctx, 1, label.String("status", "internalError"))
return nil, status.Errorf(codes.Internal, err.Error())
}

Expand All @@ -152,12 +162,14 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq

txID, err := cs.chargeCard(ctx, &total, req.CreditCard)
if err != nil {
orderCount.Add(ctx, 1, label.String("status", "chargeError"))
return nil, status.Errorf(codes.Internal, "failed to charge card: %+v", err)
}
log.Infof("payment went through (transaction_id: %s)", txID)

shippingTrackingID, err := cs.shipOrder(ctx, req.Address, prep.cartItems)
if err != nil {
orderCount.Add(ctx, 1, label.String("status", "shippingError"))
return nil, status.Errorf(codes.Unavailable, "shipping error: %+v", err)
}

Expand All @@ -177,6 +189,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
log.Infof("order confirmation email sent to %q", req.Email)
}
resp := &pb.PlaceOrderResponse{Order: orderResult}
orderCount.Add(ctx, 1, label.String("status", "ok"))
return resp, nil
}

Expand Down
19 changes: 19 additions & 0 deletions src/loadgenerator/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def checkout(l):
},
)

def badCheckout(l):
addToCart(l)
l.client.post(
"/cart/checkout",
{
"email": "amex@example.com",
"street_address": "1600 Amphitheatre Parkway",
"zip_code": "94043",
"city": "Mountain View",
"state": "CA",
"country": "United States",
"credit_card_number": "347572753801901",
"credit_card_expiration_month": "1",
"credit_card_expiration_year": "2026",
"credit_card_cvv": "528",
},
)


class UserBehavior(TaskSet):
def on_start(self):
Expand All @@ -85,6 +103,7 @@ def on_start(self):
addToCart: 2,
viewCart: 3,
checkout: 1,
badCheckout: 1,
}


Expand Down
9 changes: 8 additions & 1 deletion src/paymentservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ const sdk = lightstep.configureOpenTelemetry({
serviceVersion: VERSION,
logLevel: 'debug',
spanEndpoint: `https://${process.env.LIGHTSTEP_HOST}/traces/otlp/v0.6`,
metricEndpoint: `https://${process.env.LIGHTSTEP_HOST}/metrics/otlp/v0.6`
metricEndpoint: `https://${process.env.LIGHTSTEP_HOST}/metrics/otlp/v0.6`,
plugins: {
grpc: {
enabled: true,
// You may use a package name or absolute path to the file.
path: '@opentelemetry/plugin-grpc',
},
},
});

const path = require('path');
Expand Down
3 changes: 2 additions & 1 deletion src/paymentservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"pino": "^5.6.2",
"lightstep-opentelemetry-launcher-node": "v0.13.1",
"simple-card-validator": "^1.1.0",
"uuid": "^3.2.1"
"uuid": "^3.2.1",
"@opentelemetry/plugins-node-core-and-contrib": "v0.13.0"
},
"devDependencies": {
"semistandard": "^12.0.1"
Expand Down
26 changes: 9 additions & 17 deletions src/paymentservice/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const { opentelemetry } = require('lightstep-opentelemetry-launcher-node');
const path = require('path');
const grpc = require('grpc');
const pino = require('pino');
const protoLoader = require('@grpc/proto-loader');
const { opentelemetry } = require('lightstep-opentelemetry-launcher-node');

const charge = require('./charge');

Expand Down Expand Up @@ -48,22 +48,14 @@ class HipsterShopServer {
* @param {*} callback fn(err, ChargeResponse)
*/
static ChargeServiceHandler(call, callback) {
tracer.withSpan(tracer.getCurrentSpan(), () => {
try {
logger.info(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);
const response = charge(call.request);
callback(null, response);
} catch (err) {
console.warn(err);
span.setAttributes('error', true);
span.addEvent(`conversion request failed: ${err}`, {
'error.object': err,
message: err.message,
stack: err.stack
});
callback(err);
}
});
try {
logger.info(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);
const response = charge(call.request);
callback(null, response);
} catch (err) {
console.warn(err);
callback(err);
}
}

static CheckHandler(call, callback) {
Expand Down
23 changes: 22 additions & 1 deletion src/productcatalogservice/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
)

var (
Expand All @@ -49,7 +53,18 @@ var (

port = "3550"

reloadCatalog bool
reloadCatalog bool
meter = otel.Meter("productcatalogservice/metrics")
gpLock = new(sync.RWMutex)
gpValue = new(float64)
gpLabels = new([]label.KeyValue)
getProductsObserver = metric.Must(meter).NewFloat64ValueObserver("catalog.getProducts.time", func(ctx context.Context, result metric.Float64ObserverResult) {
(*gpLock).RLock()
value := *gpValue
labels := *gpLabels
(*gpLock).RUnlock()
result.Observe(value, labels...)
})
)

func init() {
Expand Down Expand Up @@ -180,6 +195,7 @@ func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProdu
}

func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.Product, error) {
ts := time.Now()
time.Sleep(extraLatency)
var found *pb.Product
for i := 0; i < len(parseCatalog()); i++ {
Expand All @@ -190,6 +206,11 @@ func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductReque
if found == nil {
return nil, status.Errorf(codes.NotFound, "no product with ID %s", req.Id)
}
elapsed := time.Since(ts)
(*gpLock).Lock()
*gpValue = elapsed.Seconds()
*gpLabels = []label.KeyValue{label.String("productId", found.Id)}
(*gpLock).Unlock()
return found, nil
}

Expand Down
12 changes: 12 additions & 0 deletions wsl2-kind.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: ctlptl.dev/v1alpha1
kind: Cluster
product: kind
registry: ctlptl-registry
kindV1Alpha4Cluster:
name: my-cluster
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 30000
protocol: TCP

0 comments on commit c218d82

Please sign in to comment.