Hosted Graphite Docs
Get StartedBook a Demo
  • Welcome to Hosted Graphite
  • Getting Started
  • HG-CLI
  • Sending Metrics
    • Supported Protocols
    • Graphite Tag Support
    • Metric Management
    • Metric APIs
  • Language Guide
    • Metric Libraries
    • .NET
    • Go
    • Java
    • Javascript
    • Node.js
    • PHP
    • Postman
    • Python 2.x
    • Python 3.x
    • Python Pickle
    • Ruby
    • Shell
    • TypeScript
  • Dashboard and Graphs
    • Primary Dashboards
    • Dashboard Library
    • Local Dashboard Integration
    • Worldmap Panel
    • Graphite Dashboard Guide
    • Graphite Graph Menu Reference
    • Other Dashboard Options
  • Alerting Guide
    • Alerting Overview
    • Alerts API
    • Notification Channels API
    • Scheduled Mutes API
    • Using Your Own Alerting
  • Agents Guide
    • The Hosted Graphite Agent
      • Base Metrics
      • System Layout
    • Telegraf
    • K8 Telegraf Daemon
    • OpenTelemetry
    • collectd Agent
    • StatsD Agent
    • Diamond
  • Add-Ons and Integrations Guide
    • AWS CloudWatch
    • Azure Monitor Metrics
    • GCP Metrics
    • Carbon-C-Relay
    • Circle CI
    • Cloudbees
    • Collectd Add Ons
    • GitHub
    • GitLab
    • Heroku
    • Hosted StatsD
    • New Relic
    • Papertrail
    • Pingdom
    • Sentry
    • Sitespeed
    • StatsD Add-on
    • Statuspage
  • Account Management
    • Access Keys
    • Account Diagnostics
    • Account Settings
    • Team Access: Limited Access Groups
    • SAML Authentication
    • Team Access
  • Additional Features
    • Aggregation Rules
    • Data Views
  • API Guides
    • Metrics API
    • Tag API
    • Graphite Render API
    • Render Variables API
    • Dashboard API
    • Annotations and Events API
    • Aggregation Rules API
    • Alerts APIs
  • FAQ
    • General
    • Business
    • Technical
    • Account Metrics and Limiting
    • Customization
    • Troubleshooting
    • Support
    • Changelog
Powered by GitBook
On this page
  • Sending a metric via TCP
  • Sending a metric via UDP
  • Sending a metric via HTTP POST
  • Sending a metric via StatsD

Was this helpful?

  1. Language Guide

TypeScript

Send custom metrics using TypeScript

Sending a metric via TCP

import * as net from 'net';

const socket = net.createConnection({ host: 'carbon.hostedgraphite.com', port: 2003 }, () => {
    socket.write('YOUR-API-KEY.foo.ts-tcp 1.2\n');
    socket.end();
});

Sending a metric via UDP

import * as dgram from 'dgram';

const socket = dgram.createSocket('udp4');

socket.send('YOUR-API-KEY.foo.ts-udp 1.2\n', 2003, 'carbon.hostedgraphite.com', () => {
    socket.close();
});

Sending a metric via HTTP POST

import * as https from 'https';

const apiKey = 'YOUR-API-KEY';
const metric = 'foo.ts-http';
const value = 1.2;

const metricData = `${metric} ${value}`;

const basicAuthHeader = 'Basic ' + Buffer.from(apiKey).toString('base64');

https.request({
    hostname: 'www.hostedgraphite.com',
    path: '/api/v1/sink',
    method: 'POST',
    headers: {
        'Content-Type': 'text/plain',
        'Content-Length': metricData.length,
        'Authorization': basicAuthHeader
    }
}, () => {}).end(metricData);

Sending a metric via StatsD

import * as dgram from 'dgram';

const socket = dgram.createSocket('udp4');

const apiKey = 'YOUR-API-KEY';
const metricName = 'test.testing-typescript-statsd';
const metricValue = 1.2;

const message = `${apiKey}.${metricName}:${metricValue}|<g/c/ms>`;

socket.send(message, 8125, 'statsd.hostedgraphite.com', () => {
    socket.close();
});

As with any TypeScript (.ts) file, compile it to JavaScript using tsc: tsc filename.ts and then execute the compiled script using Node.js: node filename.js.

PreviousShellNextDashboard and Graphs

Last updated 1 year ago

Was this helpful?