> ## Documentation Index
> Fetch the complete documentation index at: https://mw-docs.middleware.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Deno

> Deno APM - Setup & Installation Docs | Middleware

| Traces | Metrics | App Logs | Custom Logs | Profiling |
| :----: | :-----: | :------: | :---------: | :-------: |
|    ✅   |    ✖️   |    ✖️    |      ✅      |     ✖️    |

This guide walks you through setting up Application Performance Monitoring (APM) on a Deno application. These instructions can also be found on the Installation page in your Middleware Account. View example code [here](https://github.com/middleware-labs/demo-apm/blob/master/deno/README.md).

# Prerequisites

<Steps>
  <Step title="MW Host Agent">
    Middleware Host Agent (MW Agent). To install the MW Agent, see our <a href="https://docs.middleware.io/docs/agent-installation/overview"> installation guide</a>.
  </Step>

  <Step title="Deno Version">
    `Deno version 1.41.2` or above. Check your Deno version with the following command:

    ```javascript Shell theme={null}
    deno --version
    ```
  </Step>
</Steps>

# Install

### Step 1: Import dependencies

Import necessary packages into your Deno project from the Deno standard library and middlewareio library:

```typescript TypeScript theme={null}
import { serve } from "https://deno.land/std@0.120.0/http/server.ts";
import { track, httpTracer, info, warn, error, debug } from "https://deno.land/x/middlewareio@1.1.0/mod.ts";
```

### Step 2: Configure Tracing

Trace network requests with the following `track()` function:

```typescript TypeScript theme={null}
track({
serviceName: '{SERVICE_NAME}',
target: 'https://{ACCOUNT-UID}.middleware.io:443',
accessToken: '{ACCOUNT_KEY}',
})
```

### Step 3: Define Request Handler

Define how your server responds to incoming HTTP requests with the following function:

```typescript TypeScript theme={null}
function handler(_req: Request): Response {
   const data = {
       message: `Hello world!`,
   };
   return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } });
}
```

### Step 4: Start Your Server

Start an HTTP server with the `serve` function:

```typescript TypeScript theme={null}
await serve(httpTracer(handler));
```

### Step 5: Logging \[Optional]

Add additional logging information with the following functions:

```typescript TypeScript theme={null}
info("info");
warn("warn");
error("error");
debug("debug");
```

# Hono Framework

To use Middleware with the Hono framework, follow the example below:

```typescript TypeScript theme={null}
import { Hono } from "https://deno.land/x/hono@v4.2.9/mod.ts";
import {
  debug,
  error,
  httpTracer,
  info,
  track,
  warn,
} from "https://deno.land/x/middlewareio@1.1.0/mod.ts";

// Initialize the Middleware tracker with the name of your service, your account URL, and API key
track({
  serviceName: "hono-service",
  target: "https://<ACCOUNT_URL>:443",
  accessToken: "<MIDDLEWARE_API_KEY>",
});

const app = new Hono();

const rootHandler = (c) => {
  const infoMessage = "Hello Hono!";
  // Log an info message
  info(infoMessage);
  return c.text(`${infoMessage}\n`);
};

const greetHandler = (c) => {
  const name = c.req.param("name");
  if (name.length < 2) {
    const warnMessage = "A name cannot be a single letter";
    // Log a warning message
    warn(warnMessage);
    return c.text(`${warnMessage}\n`);
  }

  const debugMessage = `Hello ${name}`;
  // Log a debug message
  debug(debugMessage);
  return c.text(`${debugMessage}\n`);
};

const errorHandler = (c) => {
  const errorMessage = "An error occurred";
  // Log an error message
  error(errorMessage);
  throw new Error();
};

app.get("/", rootHandler);
app.get("/greet/:name", greetHandler);
app.get("/error", errorHandler);

const server = Deno.serve(
  {
    port: 8000,
    onListen({ port, hostname }) {
      console.log(`Server started at ${`http://${hostname}:${port}`}`);
    },
  },
  // Wrap the app.fetch method with the httpTracer to begin instrumenting
  httpTracer(app.fetch),
);
```

<Note> Need assistance or want to learn more about Middleware? Contact us at support\[at]middleware.io.</Note>
