Skip to content

Commit

Permalink
Add telemetry field truncation (Azure#26880)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Feb 5, 2022
1 parent 998e4dc commit 1fe7ae6
Show file tree
Hide file tree
Showing 30 changed files with 1,596 additions and 919 deletions.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.monitor.opentelemetry.exporter.implementation.builders;

import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorBase;
import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorDomain;
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractTelemetryBuilder {

private static final int MAX_PROPERTY_KEY_LENGTH = 150;
private static final int MAX_PROPERTY_VALUE_LENGTH = 8192;

protected static final int MAX_MEASUREMENT_KEY_LENGTH = 150;

protected static final int MAX_NAME_LENGTH = 1024;
protected static final int MAX_ID_LENGTH = 512;

private final TelemetryItem telemetryItem;

protected AbstractTelemetryBuilder(MonitorDomain data, String telemetryName, String baseType) {

telemetryItem = new TelemetryItem();
telemetryItem.setVersion(1);
telemetryItem.setName(telemetryName);

data.setVersion(2);

MonitorBase monitorBase = new MonitorBase();
telemetryItem.setData(monitorBase);
monitorBase.setBaseType(baseType);
monitorBase.setBaseData(data);
}

public void setTime(OffsetDateTime time) {
telemetryItem.setTime(time);
}

public void setSampleRate(float sampleRate) {
telemetryItem.setSampleRate(sampleRate);
}

public void setInstrumentationKey(String instrumentationKey) {
telemetryItem.setInstrumentationKey(instrumentationKey);
}

public void addTag(String key, String value) {
Map<String, String> tags = telemetryItem.getTags();
if (tags == null) {
tags = new HashMap<>();
telemetryItem.setTags(tags);
}
tags.put(key, value);
}

public void addProperty(String key, String value) {
if (key == null || key.isEmpty() || key.length() > MAX_PROPERTY_KEY_LENGTH || value == null) {
// TODO (trask) log
return;
}
getProperties().put(key, TelemetryTruncation.truncatePropertyValue(value, MAX_PROPERTY_VALUE_LENGTH, key));
}

public TelemetryItem build() {
return telemetryItem;
}

protected abstract Map<String, String> getProperties();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.monitor.opentelemetry.exporter.implementation.builders;

import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryEventData;

import java.util.HashMap;
import java.util.Map;

public final class EventTelemetryBuilder extends AbstractTelemetryBuilder {

private static final int MAX_EVENT_NAME_LENGTH = 512;

private final TelemetryEventData data;

public static EventTelemetryBuilder create() {
return new EventTelemetryBuilder(new TelemetryEventData());
}

private EventTelemetryBuilder(TelemetryEventData data) {
super(data, "Event", "EventData");
this.data = data;
}

public void setName(String name) {
data.setName(TelemetryTruncation.truncateTelemetry(name, MAX_EVENT_NAME_LENGTH, "Event.name"));
}

public void addMeasurement(String key, Double value) {
if (key == null || key.isEmpty() || key.length() > MAX_MEASUREMENT_KEY_LENGTH) {
// TODO (trask) log
return;
}
Map<String, Double> measurements = data.getMeasurements();
if (measurements == null) {
measurements = new HashMap<>();
data.setMeasurements(measurements);
}
measurements.put(key, value);
}

@Override
protected Map<String, String> getProperties() {
Map<String, String> properties = data.getProperties();
if (properties == null) {
properties = new HashMap<>();
data.setProperties(properties);
}
return properties;
}
}
Loading

0 comments on commit 1fe7ae6

Please sign in to comment.