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

chore: create/update .net and node.js observability docs #533

Merged
merged 2 commits into from
Nov 29, 2023
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
68 changes: 68 additions & 0 deletions docs/cache/develop/sdks/dotnet/cheat-sheet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
sidebar_position: 1
sidebar_label: Cache Cheat Sheet
sidebar_class_name: sidebar-item-dotnet
title: Cheat sheet for .NET SDK
description: Get started really quickly coding using the .NET SDK with Momento Cache
---

import { SdkExampleCodeBlock } from "@site/src/components/SdkExampleCodeBlock";
// This import is necessary even though it looks like it's un-used; The inject-example-code-snippet
// plugin will transform instances of SdkExampleCodeBlock to SdkExampleCodeBlockImpl
import { SdkExampleCodeBlockImpl } from "@site/src/components/SdkExampleCodeBlockImpl";

# Cheat sheet for .NET with Momento Cache

If you need to get going quickly with .NET and Momento Cache, this page contains the basic API calls you'll need. [Check the .NET SDK examples](https://github.com/momentohq/client-sdk-dotnet/blob/main/examples) for complete, working examples including build configuration files.

## Install the Momento client library

Install the client library in an existing .NET project:

```cli
dotnet add package Momento.Sdk
```

## Set up your API key

You'll need a Momento API key to authenticate with Momento. You can get one from the [Momento Web Console](https://console.gomomento.com/caches).
Once you have your API key, store it in an environment variable so that the Momento client can consume it:

```
export MOMENTO_API_KEY=<your Momento API key here>
```

**Note**: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

## Import libraries and create a CacheClient object
[This example file](https://github.com/momentohq/client-sdk-dotnet/blob/main/examples/MomentoUsage/Program.cs) pulls in the necessary imports, reads the API key from an environment variable, and instantiates the CacheClient that is used to interact with a cache.

## Create a new cache in Momento Cache
Use this function to create a new cache in your account.

<SdkExampleCodeBlock language={'csharp'} snippetId={'API_CreateCache'} />

## List the names of existing caches in your account
A simple list of the names of caches for the account.

<SdkExampleCodeBlock language={'csharp'} snippetId={'API_ListCaches'} />

## Write an item to a cache
A simple example of doing a set operation. In the client.set call, the TTL is optional. If you did pass it in, this would override the default TTL value set with the client connection object.

<SdkExampleCodeBlock language={'csharp'} snippetId={'API_Set'} />

## Read an item from a cache
This is an example of a simple read operation to get an item from a cache.

<SdkExampleCodeBlock language={'csharp'} snippetId={'API_Get'} />

## Running the code

You can find complete, working examples in the [csharp SDK GitHub repo examples directory](https://github.com/momentohq/client-sdk-dotnet/blob/main/examples).

:::info
Beyond these basic API calls check out the [API reference page](/cache/develop/api-reference/index.mdx) for more information on the full assortment of Momento API calls.

Check out this example code for [more advanced calls](https://github.com/momentohq/client-sdk-dotnet/blob/main/examples/MomentoApplication/Program.cs).
:::
4 changes: 2 additions & 2 deletions docs/cache/develop/sdks/dotnet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ The source code can be found on GitHub: [momentohq/client-sdk-dotnet](https://gi

## Resources

- COMING SOON: .NET SDK Cheat Sheet
- [.NET SDK Cheat Sheet](./cheat-sheet.mdx)
- [.NET SDK Examples](https://github.com/momentohq/client-sdk-dotnet/blob/main/examples/README.md): working example projects that illustrate how to use the .NET SDK
- COMING SOON: Observability: Logging and Client-side Metrics with the .NET SDK
- [.NET SDK Observability](./observability.mdx): Logging and Client-side Metrics with the .NET SDK
- COMING SOON: Taking your code to prod: Configuration and Error handling in the .NET SDK

## Integrations
Expand Down
68 changes: 68 additions & 0 deletions docs/cache/develop/sdks/dotnet/observability.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
sidebar_position: 2
sidebar_label: Observability
sidebar_class_name: sidebar-item-dotnet
title: Observability for .NET SDK
description: Instrument your Momento-enabled .NET code with Logs, Metrics, and Traces
---

import { SdkExampleCodeBlock } from "@site/src/components/SdkExampleCodeBlock";
// This import is necessary even though it looks like it's un-used; The inject-example-code-snippet
// plugin will transform instances of SdkExampleCodeBlock to SdkExampleCodeBlockImpl
import { SdkExampleCodeBlockImpl } from "@site/src/components/SdkExampleCodeBlockImpl";

# Observability with Momento in .NET

## Logging

Our goal for all of the Momento SDKs is to make sure that developers can direct Momento log output to the same destination that they are using for the rest of their application logs; therefore, we aim to be compatible with all of the popular logging frameworks for a given programming language.

The .NET SDK uses the [`ILoggerFactory` and `ILogger` interfaces](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line) so that logs can be written to the desired destination by configuring the appropriate logging providers.

As long as your preferred logger implements the `ILogger` interface, you can pass the logger into your Momento configuration objects. You can also use your preferred logger in any custom middleware implementation you write ([example here](https://github.com/momentohq/client-sdk-dotnet/blob/main/src/Momento.Sdk/Config/Middleware/LoggingMiddleware.cs)).

Once configured, you should see log messages in your logging environment that look like this:
```
[1685649962168] INFO (CacheClient/4386 on mycomputer.local): Creating Momento CacheClient
[1685649962168] INFO (ControlClient/4386 on mycomputer.local): Creating cache: test-cache
```

## Metrics
Metrics are measurements that provide quantitative information about system performance and behavior. They are numerical values captured and recorded over regular intervals, providing statistical data to aid in understanding the trends and patterns in a system.

For Momento, specifically, you might want to capture client-side metrics on the number of requests made, their duration, request or response size, or failure rates.

The most straightforward way to emit these metrics is to use one of the `ExperimentalMetricsMiddleware` classes. These classes emit metrics in JSON format:

```
(Momento: _ExperimentalMetricsLoggingMiddleware):
{
"momento": {
"numActiveRequestsAtStart": 1,
"numActiveRequestsAtFinish": 1,
"requestType": "_GetRequest",
"status": 0,
"startTime": 1697663118489,
"requestBodyTime": 1697663118489,
"endTime": 1697663118492,
"duration": 3,
"requestSize": 32,
"responseSize": 2,
"connectionID": "0"
}
}
```

The metrics format is currently considered experimental; in a future release, once the format is considered stable, this class will be renamed to remove the `Experimental` prefix. The two middleware classes available are:

- [`ExperimentalMetricsLoggingMiddleware`](https://github.com/momentohq/client-sdk-dotnet/blob/main/src/Momento.Sdk/Config/Middleware/ExperimentalMetricsLoggingMiddleware.cs): will emit metrics to your chosen logger. WARNING: depending on your request volume, this middleware will produce a high volume of log output. If you are writing logs directly to local disk, be aware of disk usage and make sure you have log rotation / compression enabled via a tool such as `logrotate`.
- [`ExperimentalMetricsCsvMiddleware`](https://github.com/momentohq/client-sdk-dotnet/blob/main/src/Momento.Sdk/Config/Middleware/ExperimentalMetricsCsvMiddleware.cs): will emit metrics to a CSV file. WARNING: enabling this middleware may have minor performance implications, so enable with caution. Depending on your request volume, the CSV file size may grow quickly, and neither sampling nor file compression / rotation are included at this time.

Log files and CSVs can be analyzed or shared with Momento to diagnose performance issues. You may also direct your logs to an AWS CloudWatch Log Group and create a CloudWatch dashboard to monitor your Momento requests; [an example of launching a Momento metrics dashboard](https://github.com/momentohq/client-sdk-javascript/tree/main/examples/nodejs/lambda-examples/cloudwatch-metrics) and optional example application is available in the Node.js SDK. The example Node.js Lambda and Fargate applications utilize the `ExperimentalMetricsLoggingMiddleware` class and CloudWatch metric filters to populate a dashboard like the one shown below, but you can deploy just the CloudWatch dashboard and populate the graphs with your own .NET application.

![An image of a CloudWatch dashboard with nine graphs populated by Momento metrics](@site/static/img/cloudwatch-dashboard.png)





10 changes: 5 additions & 5 deletions docs/cache/develop/sdks/nodejs/cheat-sheet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ Install the client library in an existing Node.js project:
npm install @gomomento/sdk
```

## Set up your auth token
## Set up your API key

You'll need a Momento auth token to authenticate with Momento. You can get one from the [Momento Web Console](https://console.gomomento.com/caches).
Once you have your token, store it in an environment variable so that the Momento client can consume it:
You'll need a Momento API key to authenticate with Momento. You can get one from the [Momento Web Console](https://console.gomomento.com/caches).
Once you have your API key, store it in an environment variable so that the Momento client can consume it:

```
export MOMENTO_AUTH_TOKEN=<your Momento token here>
export MOMENTO_API_KEY=<your Momento API key here>
```

**Note**: it is best practice to put the token into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.
**Note**: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

## Import libraries and create a CacheClient object
This code sets up the main function, pulls in the necessary imports, and instantiates the CacheClient that will be used to interact with your cache.
Expand Down
4 changes: 2 additions & 2 deletions docs/cache/develop/sdks/nodejs/observability.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ The most straightforward way to emit these metrics is to use one of the `Experim

The metrics format is currently considered experimental; in a future release, once the format is considered stable, this class will be renamed to remove the `Experimental` prefix. The two middleware classes available are:

- `ExperimentalMetricsLoggingMiddleware`: will emit metrics to your chosen logger. WARNING: depending on your request volume, this middleware will produce a high volume of log output. If you are writing logs directly to local disk, be aware of disk usage and make sure you have log rotation / compression enabled via a tool such as `logrotate`.
- `ExperimentalMetricsCsvMiddleware`: will emit metrics to a CSV file. WARNING: enabling this middleware may have minor performance implications, so enable with caution. Depending on your request volume, the CSV file size may grow quickly, and neither sampling nor file compression / rotation are included at this time.
- [`ExperimentalMetricsLoggingMiddleware`](https://github.com/momentohq/client-sdk-javascript/blob/main/packages/client-sdk-nodejs/src/config/middleware/experimental-metrics-logging-middleware.ts): will emit metrics to your chosen logger. WARNING: depending on your request volume, this middleware will produce a high volume of log output. If you are writing logs directly to local disk, be aware of disk usage and make sure you have log rotation / compression enabled via a tool such as `logrotate`.
- [`ExperimentalMetricsCsvMiddleware`](https://github.com/momentohq/client-sdk-javascript/blob/main/packages/client-sdk-nodejs/src/config/middleware/experimental-metrics-csv-middleware.ts): will emit metrics to a CSV file. WARNING: enabling this middleware may have minor performance implications, so enable with caution. Depending on your request volume, the CSV file size may grow quickly, and neither sampling nor file compression / rotation are included at this time.

Log files and CSVs can be analyzed or shared with Momento to diagnose performance issues. You may also direct your logs to an AWS CloudWatch Log Group and create a CloudWatch dashboard to monitor your Momento requests; [an example of launching a Momento metrics dashboard](https://github.com/momentohq/client-sdk-javascript/tree/main/examples/nodejs/lambda-examples/cloudwatch-metrics) and optional example application is available in the Node.js SDK. The example Lambda and Fargate applications utilize the `ExperimentalMetricsLoggingMiddleware` class and CloudWatch metric filters to populate a dashboard like the one shown below.

Expand Down
Loading